Add comment to file upload from telegram. Show comments on all bridges. Closes #358

This commit is contained in:
Wim 2018-02-01 00:41:09 +01:00
parent 64a20ee61b
commit 7764493298
6 changed files with 27 additions and 9 deletions

View File

@ -124,6 +124,9 @@ func (b *Bgitter) Send(msg config.Message) (string, error) {
if len(msg.Extra["file"]) > 0 { if len(msg.Extra["file"]) > 0 {
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
if fi.Comment != "" {
msg.Text += fi.Comment + ":"
}
if fi.URL != "" { if fi.URL != "" {
msg.Text = fi.URL msg.Text = fi.URL
} }

View File

@ -180,6 +180,9 @@ func (b *Birc) Send(msg config.Message) (string, error) {
if len(msg.Extra["file"]) > 0 { if len(msg.Extra["file"]) > 0 {
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
if fi.Comment != "" {
msg.Text += fi.Comment + ":"
}
if fi.URL != "" { if fi.URL != "" {
msg.Text = fi.URL msg.Text = fi.URL
} }

View File

@ -107,6 +107,12 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {
mtype := mime.TypeByExtension("." + sp[len(sp)-1]) mtype := mime.TypeByExtension("." + sp[len(sp)-1])
if strings.Contains(mtype, "image") || if strings.Contains(mtype, "image") ||
strings.Contains(mtype, "video") { 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) flog.Debugf("uploading file: %s %s", fi.Name, mtype)
res, err := b.mc.UploadToContentRepo(content, mtype, int64(len(*fi.Data))) res, err := b.mc.UploadToContentRepo(content, mtype, int64(len(*fi.Data)))
if err != nil { if err != nil {

View File

@ -65,6 +65,9 @@ func (b *Bsshchat) Send(msg config.Message) (string, error) {
if len(msg.Extra["file"]) > 0 { if len(msg.Extra["file"]) > 0 {
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
if fi.Comment != "" {
msg.Text += fi.Comment + ":"
}
if fi.URL != "" { if fi.URL != "" {
msg.Text = fi.URL msg.Text = fi.URL
} }

View File

@ -167,22 +167,22 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
username = "unknown" username = "unknown"
} }
if message.Sticker != nil { if message.Sticker != nil {
b.handleDownload(message.Sticker, &fmsg) b.handleDownload(message.Sticker, message.Caption, &fmsg)
} }
if message.Video != nil { if message.Video != nil {
b.handleDownload(message.Video, &fmsg) b.handleDownload(message.Video, message.Caption, &fmsg)
} }
if message.Photo != nil { if message.Photo != nil {
b.handleDownload(message.Photo, &fmsg) b.handleDownload(message.Photo, message.Caption, &fmsg)
} }
if message.Document != nil { if message.Document != nil {
b.handleDownload(message.Document, &fmsg) b.handleDownload(message.Document, message.Caption, &fmsg)
} }
if message.Voice != nil { if message.Voice != nil {
b.handleDownload(message.Voice, &fmsg) b.handleDownload(message.Voice, message.Caption, &fmsg)
} }
if message.Audio != nil { if message.Audio != nil {
b.handleDownload(message.Audio, &fmsg) b.handleDownload(message.Audio, message.Caption, &fmsg)
} }
if message.ForwardFrom != nil { if message.ForwardFrom != nil {
@ -239,7 +239,7 @@ func (b *Btelegram) getFileDirectURL(id string) string {
return res return res
} }
func (b *Btelegram) handleDownload(file interface{}, msg *config.Message) { func (b *Btelegram) handleDownload(file interface{}, comment string, msg *config.Message) {
size := 0 size := 0
url := "" url := ""
name := "" name := ""
@ -307,7 +307,7 @@ func (b *Btelegram) handleDownload(file interface{}, msg *config.Message) {
flog.Errorf("download %s failed %#v", url, err) flog.Errorf("download %s failed %#v", url, err)
} else { } else {
flog.Debugf("download OK %#v %#v %#v", name, len(*data), len(url)) 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})
} }
} }
} }

View File

@ -84,8 +84,11 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {
if len(msg.Extra["file"]) > 0 { if len(msg.Extra["file"]) > 0 {
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
if fi.Comment != "" {
msg.Text += fi.Comment + ":"
}
if fi.URL != "" { 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}) b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.Config.Muc, Text: msg.Username + msg.Text})
} }