Add support for comments from slack file uploads (slack)

This commit is contained in:
Wim 2017-11-13 00:20:31 +01:00
parent 6d21f84187
commit 36a800c3f5
5 changed files with 36 additions and 22 deletions

View File

@ -35,6 +35,7 @@ type Message struct {
type FileInfo struct {
Name string
Data *[]byte
Comment string
}
type ChannelInfo struct {

View File

@ -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
}
}

View File

@ -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})
}

View File

@ -198,6 +198,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
Reader: bytes.NewReader(*fi.Data),
Filename: fi.Name,
Channels: []string{schannel.ID},
InitialComment: fi.Comment,
})
if err != nil {
flog.Errorf("uploadfile %#v", err)
@ -294,18 +295,23 @@ 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
}
}
}
}
func (b *Bslack) handleSlackClient(mchan chan *MMMessage) {
for msg := range b.rtm.IncomingEvents {

View File

@ -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
}