From 36a800c3f57ed1880ed6530db0542c979f6c69e7 Mon Sep 17 00:00:00 2001 From: Wim Date: Mon, 13 Nov 2017 00:20:31 +0100 Subject: [PATCH] Add support for comments from slack file uploads (slack) --- bridge/config/config.go | 5 +++-- bridge/discord/discord.go | 3 ++- bridge/mattermost/mattermost.go | 4 ++-- bridge/slack/slack.go | 18 ++++++++++++------ bridge/telegram/telegram.go | 28 +++++++++++++++++----------- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/bridge/config/config.go b/bridge/config/config.go index 0dfba94b..b4124347 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -33,8 +33,9 @@ type Message struct { } type FileInfo struct { - Name string - Data *[]byte + Name string + Data *[]byte + Comment string } type ChannelInfo struct { diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 12e0d827..183b1193 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -151,11 +151,12 @@ func (b *bdiscord) Send(msg config.Message) (string, error) { fi := f.(config.FileInfo) files := []*discordgo.File{} files = append(files, &discordgo.File{fi.Name, "", bytes.NewReader(*fi.Data)}) - _, err = b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{Content: msg.Text, Files: files}) + _, err = b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{Content: msg.Username + fi.Comment, Files: files}) if err != nil { flog.Errorf("file upload failed: %#v", err) } } + return "", nil } } diff --git a/bridge/mattermost/mattermost.go b/bridge/mattermost/mattermost.go index 3274d81d..9d9990b2 100644 --- a/bridge/mattermost/mattermost.go +++ b/bridge/mattermost/mattermost.go @@ -190,9 +190,9 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) { flog.Debugf("ERROR %#v", err) return "", err } - message = "uploaded a file: " + fi.Name + message = fi.Comment if b.Config.PrefixMessagesWithNick { - message = nick + "uploaded a file: " + fi.Name + message = nick + fi.Comment } res, err = b.mc.PostMessageWithFiles(b.mc.GetChannelId(channel, ""), message, []string{id}) } diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 74003753..b753c1a5 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -195,9 +195,10 @@ func (b *Bslack) Send(msg config.Message) (string, error) { for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) _, err = b.sc.UploadFile(slack.FileUploadParameters{ - Reader: bytes.NewReader(*fi.Data), - Filename: fi.Name, - Channels: []string{schannel.ID}, + Reader: bytes.NewReader(*fi.Data), + Filename: fi.Name, + Channels: []string{schannel.ID}, + InitialComment: fi.Comment, }) if err != nil { flog.Errorf("uploadfile %#v", err) @@ -294,16 +295,21 @@ func (b *Bslack) handleSlack() { if message.Raw.File != nil { // limit to 1MB for now if message.Raw.File.Size <= 1000000 { + comment := "" data, err := b.downloadFile(message.Raw.File.URLPrivateDownload) if err != nil { flog.Errorf("download %s failed %#v", message.Raw.File.URLPrivateDownload, err) } else { - msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: message.Raw.File.Name, Data: data}) + results := regexp.MustCompile(`.*?commented: (.*)`).FindAllStringSubmatch(msg.Text, -1) + if len(results) > 0 { + comment = results[0][1] + } + msg.Extra["file"] = append(msg.Extra["file"], config.FileInfo{Name: message.Raw.File.Name, Data: data, Comment: comment}) } + flog.Debugf("Message is %#v", msg) + b.Remote <- msg } } - flog.Debugf("Message is %#v", msg) - b.Remote <- msg } } diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index f4662669..5cd49253 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -114,20 +114,14 @@ func (b *Btelegram) Send(msg config.Message) (string, error) { if err != nil { log.Errorf("file upload failed: %#v", err) } + if fi.Comment != "" { + b.sendMessage(chatid, msg.Username+fi.Comment) + } } + return "", nil } } - - m := tgbotapi.NewMessage(chatid, msg.Username+msg.Text) - if b.Config.MessageFormat == "HTML" { - m.ParseMode = tgbotapi.ModeHTML - } - res, err := b.c.Send(m) - if err != nil { - return "", err - } - return strconv.Itoa(res.MessageID), nil - + return b.sendMessage(chatid, msg.Username+msg.Text) } func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) { @@ -275,3 +269,15 @@ func (b *Btelegram) handleDownload(file interface{}, msg *config.Message) { } } } + +func (b *Btelegram) sendMessage(chatid int64, text string) (string, error) { + m := tgbotapi.NewMessage(chatid, text) + if b.Config.MessageFormat == "HTML" { + m.ParseMode = tgbotapi.ModeHTML + } + res, err := b.c.Send(m) + if err != nil { + return "", err + } + return strconv.Itoa(res.MessageID), nil +}