From 7764493298760ec8d33f5387ea2fb6c4705a407e Mon Sep 17 00:00:00 2001 From: Wim Date: Thu, 1 Feb 2018 00:41:09 +0100 Subject: Add comment to file upload from telegram. Show comments on all bridges. Closes #358 --- bridge/gitter/gitter.go | 3 +++ bridge/irc/irc.go | 3 +++ bridge/matrix/matrix.go | 6 ++++++ bridge/sshchat/sshchat.go | 3 +++ bridge/telegram/telegram.go | 16 ++++++++-------- bridge/xmpp/xmpp.go | 5 ++++- 6 files changed, 27 insertions(+), 9 deletions(-) diff --git a/bridge/gitter/gitter.go b/bridge/gitter/gitter.go index fce58c1f..53f1b0d8 100644 --- a/bridge/gitter/gitter.go +++ b/bridge/gitter/gitter.go @@ -124,6 +124,9 @@ func (b *Bgitter) Send(msg config.Message) (string, error) { if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) + if fi.Comment != "" { + msg.Text += fi.Comment + ":" + } if fi.URL != "" { msg.Text = fi.URL } diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index 117398f1..8e85fa29 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -180,6 +180,9 @@ func (b *Birc) Send(msg config.Message) (string, error) { if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) + if fi.Comment != "" { + msg.Text += fi.Comment + ":" + } if fi.URL != "" { msg.Text = fi.URL } diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index d659aaa2..06c52148 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -107,6 +107,12 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) { mtype := mime.TypeByExtension("." + sp[len(sp)-1]) if strings.Contains(mtype, "image") || strings.Contains(mtype, "video") { + if fi.Comment != "" { + resp, err := b.mc.SendText(channel, msg.Username+fi.Comment) + if err != nil { + flog.Errorf("file comment failed: %#v", err) + } + } flog.Debugf("uploading file: %s %s", fi.Name, mtype) res, err := b.mc.UploadToContentRepo(content, mtype, int64(len(*fi.Data))) if err != nil { diff --git a/bridge/sshchat/sshchat.go b/bridge/sshchat/sshchat.go index 81e83244..7b008fe0 100644 --- a/bridge/sshchat/sshchat.go +++ b/bridge/sshchat/sshchat.go @@ -65,6 +65,9 @@ func (b *Bsshchat) Send(msg config.Message) (string, error) { if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) + if fi.Comment != "" { + msg.Text += fi.Comment + ":" + } if fi.URL != "" { msg.Text = fi.URL } diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 9ce4eb14..99968ec9 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -167,22 +167,22 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) { username = "unknown" } if message.Sticker != nil { - b.handleDownload(message.Sticker, &fmsg) + b.handleDownload(message.Sticker, message.Caption, &fmsg) } if message.Video != nil { - b.handleDownload(message.Video, &fmsg) + b.handleDownload(message.Video, message.Caption, &fmsg) } if message.Photo != nil { - b.handleDownload(message.Photo, &fmsg) + b.handleDownload(message.Photo, message.Caption, &fmsg) } if message.Document != nil { - b.handleDownload(message.Document, &fmsg) + b.handleDownload(message.Document, message.Caption, &fmsg) } if message.Voice != nil { - b.handleDownload(message.Voice, &fmsg) + b.handleDownload(message.Voice, message.Caption, &fmsg) } if message.Audio != nil { - b.handleDownload(message.Audio, &fmsg) + b.handleDownload(message.Audio, message.Caption, &fmsg) } if message.ForwardFrom != nil { @@ -239,7 +239,7 @@ func (b *Btelegram) getFileDirectURL(id string) string { return res } -func (b *Btelegram) handleDownload(file interface{}, msg *config.Message) { +func (b *Btelegram) handleDownload(file interface{}, comment string, msg *config.Message) { size := 0 url := "" name := "" @@ -307,7 +307,7 @@ func (b *Btelegram) handleDownload(file interface{}, msg *config.Message) { flog.Errorf("download %s failed %#v", url, err) } else { flog.Debugf("download OK %#v %#v %#v", name, len(*data), len(url)) - msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: name, Data: data}) + msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: name, Data: data, Comment: comment}) } } } diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 63a1048e..9c2eb2cc 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -84,8 +84,11 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) { if len(msg.Extra["file"]) > 0 { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) + if fi.Comment != "" { + msg.Text += fi.Comment + ":" + } if fi.URL != "" { - msg.Text = fi.URL + msg.Text += fi.URL } b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.Config.Muc, Text: msg.Username + msg.Text}) } -- cgit v1.2.3