summaryrefslogtreecommitdiffstats
path: root/bridge/whatsapp/whatsapp.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2022-03-13 01:01:42 +0100
committerWim <wim@42.be>2022-03-20 14:57:48 +0100
commit320c996a217e5b48d62c9a0277e07a4c5f8775e6 (patch)
tree2761ea70c169f6048991a78a51084285c70562b9 /bridge/whatsapp/whatsapp.go
parent69c74be7bb9433373b5168f59fa9b0939be0004e (diff)
downloadmatterbridge-msglm-320c996a217e5b48d62c9a0277e07a4c5f8775e6.tar.gz
matterbridge-msglm-320c996a217e5b48d62c9a0277e07a4c5f8775e6.tar.bz2
matterbridge-msglm-320c996a217e5b48d62c9a0277e07a4c5f8775e6.zip
Refactor login logic (whatsapp)
Diffstat (limited to 'bridge/whatsapp/whatsapp.go')
-rw-r--r--bridge/whatsapp/whatsapp.go44
1 files changed, 30 insertions, 14 deletions
diff --git a/bridge/whatsapp/whatsapp.go b/bridge/whatsapp/whatsapp.go
index c28b3c89..ee18f457 100644
--- a/bridge/whatsapp/whatsapp.go
+++ b/bridge/whatsapp/whatsapp.go
@@ -74,11 +74,14 @@ func (b *Bwhatsapp) Connect() error {
b.wc = whatsmeow.NewClient(device, waLog.Stdout("Client", "INFO", true))
b.wc.AddEventHandler(b.eventHandler)
- // No ID stored, new login
- qrChan, err := b.wc.GetQRChannel(context.Background())
- // This error means that we're already logged in, so ignore it.
- if err != nil && !errors.Is(err, whatsmeow.ErrQRStoreContainsID) {
- return errors.New("failed to to get QR channel:" + err.Error())
+ firstlogin := false
+ var qrChan <-chan whatsmeow.QRChannelItem
+ if b.wc.Store.ID == nil {
+ firstlogin = true
+ qrChan, err = b.wc.GetQRChannel(context.Background())
+ if err != nil && !errors.Is(err, whatsmeow.ErrQRStoreContainsID) {
+ return errors.New("failed to to get QR channel:" + err.Error())
+ }
}
err = b.wc.Connect()
@@ -86,12 +89,25 @@ func (b *Bwhatsapp) Connect() error {
return errors.New("failed to connect to WhatsApp: " + err.Error())
}
- for evt := range qrChan {
- if evt.Event == "code" {
- // Render the QR code here
- qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
- } else {
- b.Log.Infof("QR channel result: %s", evt.Event)
+ if b.wc.Store.ID == nil {
+ for evt := range qrChan {
+ if evt.Event == "code" {
+ qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
+ } else {
+ b.Log.Infof("QR channel result: %s", evt.Event)
+ }
+ }
+ }
+
+ // disconnect and reconnect on our first login/pairing
+ // for some reason the GetJoinedGroups in JoinChannel doesn't work on first login
+ if firstlogin {
+ b.wc.Disconnect()
+ time.Sleep(time.Second)
+
+ err = b.wc.Connect()
+ if err != nil {
+ return errors.New("failed to connect to WhatsApp: " + err.Error())
}
}
@@ -113,7 +129,6 @@ func (b *Bwhatsapp) Connect() error {
}
// get user avatar asynchronously
- // go func() {
b.Log.Info("Getting user avatars..")
for jid := range b.users {
@@ -122,13 +137,14 @@ func (b *Bwhatsapp) Connect() error {
b.Log.Warnf("Could not get profile photo of %s: %v", jid, err)
} else {
b.Lock()
- b.userAvatars[jid] = info.URL
+ if info != nil {
+ b.userAvatars[jid] = info.URL
+ }
b.Unlock()
}
}
b.Log.Info("Finished getting avatars..")
- // }()
return nil
}