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

@ -33,8 +33,9 @@ type Message struct {
} }
type FileInfo struct { type FileInfo struct {
Name string Name string
Data *[]byte Data *[]byte
Comment string
} }
type ChannelInfo struct { type ChannelInfo struct {

View File

@ -151,11 +151,12 @@ func (b *bdiscord) Send(msg config.Message) (string, error) {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
files := []*discordgo.File{} files := []*discordgo.File{}
files = append(files, &discordgo.File{fi.Name, "", bytes.NewReader(*fi.Data)}) 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 { if err != nil {
flog.Errorf("file upload failed: %#v", err) 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) flog.Debugf("ERROR %#v", err)
return "", err return "", err
} }
message = "uploaded a file: " + fi.Name message = fi.Comment
if b.Config.PrefixMessagesWithNick { 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}) res, err = b.mc.PostMessageWithFiles(b.mc.GetChannelId(channel, ""), message, []string{id})
} }

View File

@ -195,9 +195,10 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
_, err = b.sc.UploadFile(slack.FileUploadParameters{ _, err = b.sc.UploadFile(slack.FileUploadParameters{
Reader: bytes.NewReader(*fi.Data), Reader: bytes.NewReader(*fi.Data),
Filename: fi.Name, Filename: fi.Name,
Channels: []string{schannel.ID}, Channels: []string{schannel.ID},
InitialComment: fi.Comment,
}) })
if err != nil { if err != nil {
flog.Errorf("uploadfile %#v", err) flog.Errorf("uploadfile %#v", err)
@ -294,16 +295,21 @@ func (b *Bslack) handleSlack() {
if message.Raw.File != nil { if message.Raw.File != nil {
// limit to 1MB for now // limit to 1MB for now
if message.Raw.File.Size <= 1000000 { if message.Raw.File.Size <= 1000000 {
comment := ""
data, err := b.downloadFile(message.Raw.File.URLPrivateDownload) data, err := b.downloadFile(message.Raw.File.URLPrivateDownload)
if err != nil { if err != nil {
flog.Errorf("download %s failed %#v", message.Raw.File.URLPrivateDownload, err) flog.Errorf("download %s failed %#v", message.Raw.File.URLPrivateDownload, err)
} else { } 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
} }
} }

View File

@ -114,20 +114,14 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
if err != nil { if err != nil {
log.Errorf("file upload failed: %#v", err) log.Errorf("file upload failed: %#v", err)
} }
if fi.Comment != "" {
b.sendMessage(chatid, msg.Username+fi.Comment)
}
} }
return "", nil
} }
} }
return b.sendMessage(chatid, msg.Username+msg.Text)
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
} }
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) { 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
}