diff options
author | Krzysiek Madejski <krzysztof.madejski@epf.org.pl> | 2019-02-21 20:28:13 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2019-02-21 20:28:13 +0100 |
commit | 55e79063d6edbbf4560fd14edc45ce9558afaf7a (patch) | |
tree | 9ee9470119066556210a9226ae05b2c73ebda5e6 /bridge/whatsapp/helpers.go | |
parent | 46f4bbb3b5e93ff489c0125c66b1c29fcb001e22 (diff) | |
download | matterbridge-msglm-55e79063d6edbbf4560fd14edc45ce9558afaf7a.tar.gz matterbridge-msglm-55e79063d6edbbf4560fd14edc45ce9558afaf7a.tar.bz2 matterbridge-msglm-55e79063d6edbbf4560fd14edc45ce9558afaf7a.zip |
Add initial WhatsApp support (#711)
Diffstat (limited to 'bridge/whatsapp/helpers.go')
-rw-r--r-- | bridge/whatsapp/helpers.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/bridge/whatsapp/helpers.go b/bridge/whatsapp/helpers.go new file mode 100644 index 00000000..5268ba3e --- /dev/null +++ b/bridge/whatsapp/helpers.go @@ -0,0 +1,84 @@ +package bwhatsapp + +import ( + "encoding/gob" + "errors" + "os" + + qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go" + "github.com/Rhymen/go-whatsapp" +) + +func qrFromTerminal(invert bool) chan string { + qr := make(chan string) + go func() { + terminal := qrcodeTerminal.New() + if invert { + terminal = qrcodeTerminal.New2(qrcodeTerminal.ConsoleColors.BrightWhite, qrcodeTerminal.ConsoleColors.BrightBlack, qrcodeTerminal.QRCodeRecoveryLevels.Medium) + } + + terminal.Get(<-qr).Print() + }() + + return qr +} + +func (b *Bwhatsapp) readSession() (whatsapp.Session, error) { + session := whatsapp.Session{} + sessionFile := b.Config.GetString(sessionFile) + + if sessionFile == "" { + return session, errors.New("if you won't set SessionFile then you will need to scan QR code on every restart") + } + + file, err := os.Open(sessionFile) + if err != nil { + return session, err + } + defer file.Close() + decoder := gob.NewDecoder(file) + err = decoder.Decode(&session) + if err != nil { + return session, err + } + return session, nil +} + +func (b *Bwhatsapp) writeSession(session whatsapp.Session) error { + sessionFile := b.Config.GetString(sessionFile) + + if sessionFile == "" { + // we already sent a warning while starting the bridge, so let's be quiet here + return nil + } + + file, err := os.Create(sessionFile) + if err != nil { + return err + } + defer file.Close() + encoder := gob.NewEncoder(file) + err = encoder.Encode(session) + + return err +} + +func (b *Bwhatsapp) getSenderName(senderJid string) string { + if sender, exists := b.users[senderJid]; exists { + if sender.Name != "" { + return sender.Name + } + // if user is not in phone contacts + // it is the most obvious scenario unless you sync your phone contacts with some remote updated source + // users can change it in their WhatsApp settings -> profile -> click on Avatar + return sender.Notify + } + return "" +} + +func (b *Bwhatsapp) getSenderNotify(senderJid string) string { + if sender, exists := b.users[senderJid]; exists { + return sender.Notify + } + return "" +} |