summaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorAvinash Reddy <cassilasreddythemostwanted3108@gmail.com>2021-05-21 20:50:47 +0000
committerGitHub <noreply@github.com>2021-05-21 22:50:47 +0200
commita189298ab0d1d8172cfbab9bee1c27c988e0af10 (patch)
treebd957cb4ab8da37efca7822442f1844d2c98afc0 /bridge
parent714a2ad7303f0d7b51b8b7f6c5f86f02a99c3524 (diff)
downloadmatterbridge-msglm-a189298ab0d1d8172cfbab9bee1c27c988e0af10.tar.gz
matterbridge-msglm-a189298ab0d1d8172cfbab9bee1c27c988e0af10.tar.bz2
matterbridge-msglm-a189298ab0d1d8172cfbab9bee1c27c988e0af10.zip
Handle document messages (whatsapp) (#1475)
* [Whatsapp] Add DocumentMessage handler * Fix typo Thanks @42wim :) Co-authored-by: Wim <wim@42.be> Co-authored-by: Wim <wim@42.be>
Diffstat (limited to 'bridge')
-rw-r--r--bridge/whatsapp/handlers.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/bridge/whatsapp/handlers.go b/bridge/whatsapp/handlers.go
index 411676e2..55aaf868 100644
--- a/bridge/whatsapp/handlers.go
+++ b/bridge/whatsapp/handlers.go
@@ -312,3 +312,60 @@ func (b *Bwhatsapp) HandleAudioMessage(message whatsapp.AudioMessage) {
b.Remote <- rmsg
}
+
+// HandleDocumentMessage downloads documents
+func (b *Bwhatsapp) HandleDocumentMessage(message whatsapp.DocumentMessage) {
+ if message.Info.FromMe || message.Info.Timestamp < b.startedAt {
+ return
+ }
+
+ senderJID := message.Info.SenderJid
+ if len(message.Info.SenderJid) == 0 && message.Info.Source != nil && message.Info.Source.Participant != nil {
+ senderJID = *message.Info.Source.Participant
+ }
+
+ senderName := b.getSenderName(message.Info.SenderJid)
+ if senderName == "" {
+ senderName = "Someone" // don't expose telephone number
+ }
+
+ rmsg := config.Message{
+ UserID: senderJID,
+ Username: senderName,
+ Channel: message.Info.RemoteJid,
+ Account: b.Account,
+ Protocol: b.Protocol,
+ Extra: make(map[string][]interface{}),
+ ID: message.Info.Id,
+ }
+
+ if avatarURL, exists := b.userAvatars[senderJID]; exists {
+ rmsg.Avatar = avatarURL
+ }
+
+ fileExt, err := mime.ExtensionsByType(message.Type)
+ if err != nil {
+ b.Log.Errorf("Mimetype detection error: %s", err)
+
+ return
+ }
+
+ filename := fmt.Sprintf("%v%v", message.Info.Id, fileExt[0])
+
+ b.Log.Debugf("Trying to download %s with type %s", filename, message.Type)
+
+ data, err := message.Download()
+ if err != nil {
+ b.Log.Errorf("Download document message failed: %s", err)
+
+ return
+ }
+
+ // Move file to bridge storage
+ helper.HandleDownloadData(b.Log, &rmsg, filename, "document", "", &data, b.General)
+
+ b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
+ b.Log.Debugf("<= Message is %#v", rmsg)
+
+ b.Remote <- rmsg
+}