summaryrefslogtreecommitdiffstats
path: root/bridge/whatsapp/handlers.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2020-11-27 00:34:44 +0100
committerWim <wim@42.be>2020-11-29 15:37:20 +0100
commit3e865708d6dc51290d6431efd21d4f9a3e9dfee7 (patch)
treefa3e6b136b983675e810e3109c0aa6edd31ce161 /bridge/whatsapp/handlers.go
parentc3bcbd63c012f57fa81832cf4b2812dcd15c8a8f (diff)
downloadmatterbridge-msglm-3e865708d6dc51290d6431efd21d4f9a3e9dfee7.tar.gz
matterbridge-msglm-3e865708d6dc51290d6431efd21d4f9a3e9dfee7.tar.bz2
matterbridge-msglm-3e865708d6dc51290d6431efd21d4f9a3e9dfee7.zip
Refactor/cleanup code (whatsapp)
Diffstat (limited to 'bridge/whatsapp/handlers.go')
-rw-r--r--bridge/whatsapp/handlers.go26
1 files changed, 18 insertions, 8 deletions
diff --git a/bridge/whatsapp/handlers.go b/bridge/whatsapp/handlers.go
index 4d1c9a13..8b25c146 100644
--- a/bridge/whatsapp/handlers.go
+++ b/bridge/whatsapp/handlers.go
@@ -24,7 +24,8 @@ Check:
func (b *Bwhatsapp) HandleError(err error) {
// ignore received invalid data errors. https://github.com/42wim/matterbridge/issues/843
// ignore tag 174 errors. https://github.com/42wim/matterbridge/issues/1094
- if strings.Contains(err.Error(), "error processing data: received invalid data") || strings.Contains(err.Error(), "invalid string with tag 174") {
+ if strings.Contains(err.Error(), "error processing data: received invalid data") ||
+ strings.Contains(err.Error(), "invalid string with tag 174") {
return
}
@@ -47,16 +48,22 @@ func (b *Bwhatsapp) reconnect(err error) {
Max: 5 * time.Minute,
Jitter: true,
}
+
for {
d := bf.Duration()
+
b.Log.Errorf("Connection failed, underlying error: %v", err)
b.Log.Infof("Waiting %s...", d)
+
time.Sleep(d)
+
b.Log.Info("Reconnecting...")
+
err := b.conn.Restore()
if err == nil {
bf.Reset()
b.startedAt = uint64(time.Now().Unix())
+
return
}
}
@@ -64,7 +71,7 @@ func (b *Bwhatsapp) reconnect(err error) {
// HandleTextMessage sent from WhatsApp, relay it to the brige
func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
- if message.Info.FromMe { // || !strings.Contains(strings.ToLower(message.Text), "@echo") {
+ if message.Info.FromMe {
return
}
// whatsapp sends last messages to show context , cut them
@@ -101,11 +108,13 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
if mention == "" {
mention = "someone"
}
+
message.Text = strings.Replace(message.Text, "@"+numberAndSuffix[0], "@"+mention, 1)
}
}
b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
+
rmsg := config.Message{
UserID: senderJID,
Username: senderName,
@@ -116,15 +125,15 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
// ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
- // Event string `json:"event"`
- // Gateway string // will be added during message processing
- ID: message.Info.Id}
+ ID: message.Info.Id,
+ }
if avatarURL, exists := b.userAvatars[senderJID]; exists {
rmsg.Avatar = avatarURL
}
b.Log.Debugf("<= Message is %#v", rmsg)
+
b.Remote <- rmsg
}
@@ -157,6 +166,7 @@ func (b *Bwhatsapp) HandleImageMessage(message whatsapp.ImageMessage) {
}
b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
+
rmsg := config.Message{
UserID: senderJID,
Username: senderName,
@@ -166,9 +176,8 @@ func (b *Bwhatsapp) HandleImageMessage(message whatsapp.ImageMessage) {
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
// ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
- // Event string `json:"event"`
- // Gateway string // will be added during message processing
- ID: message.Info.Id}
+ ID: message.Info.Id,
+ }
if avatarURL, exists := b.userAvatars[senderJID]; exists {
rmsg.Avatar = avatarURL
@@ -196,6 +205,7 @@ func (b *Bwhatsapp) HandleImageMessage(message whatsapp.ImageMessage) {
helper.HandleDownloadData(b.Log, &rmsg, filename, message.Caption, "", &data, b.General)
b.Log.Debugf("<= Image Message is %#v", rmsg)
+
b.Remote <- rmsg
}