summaryrefslogtreecommitdiffstats
path: root/bridge/whatsapp/handlers.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2020-11-29 15:05:57 +0100
committerWim <wim@42.be>2020-11-29 15:37:20 +0100
commit5ad788e7683f689abab9fdb1a86a0a1e2dac3f56 (patch)
tree99b91b4adbaedf243db3e76103237d57663e6011 /bridge/whatsapp/handlers.go
parented98c586c6bd9dadc01bfa33d975ac10752f97ed (diff)
downloadmatterbridge-msglm-5ad788e7683f689abab9fdb1a86a0a1e2dac3f56.tar.gz
matterbridge-msglm-5ad788e7683f689abab9fdb1a86a0a1e2dac3f56.tar.bz2
matterbridge-msglm-5ad788e7683f689abab9fdb1a86a0a1e2dac3f56.zip
Handle video downloads (whatsapp)
Diffstat (limited to 'bridge/whatsapp/handlers.go')
-rw-r--r--bridge/whatsapp/handlers.go60
1 files changed, 57 insertions, 3 deletions
diff --git a/bridge/whatsapp/handlers.go b/bridge/whatsapp/handlers.go
index 8b25c146..73e2848f 100644
--- a/bridge/whatsapp/handlers.go
+++ b/bridge/whatsapp/handlers.go
@@ -209,9 +209,63 @@ func (b *Bwhatsapp) HandleImageMessage(message whatsapp.ImageMessage) {
b.Remote <- rmsg
}
-//func (b *Bwhatsapp) HandleVideoMessage(message whatsapp.VideoMessage) {
-// fmt.Println(message) // TODO implement
-//}
+// HandleVideoMessage downloads video messages
+func (b *Bwhatsapp) HandleVideoMessage(message whatsapp.VideoMessage) {
+ 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 size %#v and type %s", filename, message.Length, message.Type)
+
+ data, err := message.Download()
+ if err != nil {
+ b.Log.Errorf("Download video failed: %s", err)
+
+ return
+ }
+
+ // Move file to bridge storage
+ helper.HandleDownloadData(b.Log, &rmsg, filename, message.Caption, "", &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
+}
+
//
//func (b *Bwhatsapp) HandleJsonMessage(message string) {
// fmt.Println(message) // TODO implement