From 6911458d15f548070da870cc7f68c2a882f1c674 Mon Sep 17 00:00:00 2001 From: Duco van Amstel Date: Mon, 22 Oct 2018 10:43:57 -0700 Subject: [PATCH] Clean up message send logic (slack). (#531) --- bridge/slack/slack.go | 181 ++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 87 deletions(-) diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 6585c9fc..2022ebce 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -165,6 +165,10 @@ func (b *Bslack) JoinChannel(channel config.ChannelInfo) error { return nil } +func (b *Bslack) Reload(cfg *bridge.Config) (string, error) { + return "", nil +} + func (b *Bslack) Send(msg config.Message) (string, error) { b.Log.Debugf("=> Receiving %#v", msg) @@ -177,7 +181,70 @@ func (b *Bslack) Send(msg config.Message) (string, error) { if b.GetString(outgoingWebhookConfig) != "" { return b.sendWebhook(msg) } + return b.sendRTM(msg) +} + +// sendWebhook uses the configured WebhookURL to send the message +func (b *Bslack) sendWebhook(msg config.Message) (string, error) { + // skip events + if msg.Event != "" { + return "", nil + } + + if b.GetBool(useNickPrefixConfig) { + msg.Text = msg.Username + msg.Text + } + + if msg.Extra != nil { + // this sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE + for _, rmsg := range helper.HandleExtra(&msg, b.General) { + iconURL := config.GetIconURL(&rmsg, b.GetString(iconURLConfig)) + matterMessage := matterhook.OMessage{ + IconURL: iconURL, + Channel: msg.Channel, + UserName: rmsg.Username, + Text: rmsg.Text, + } + if err := b.mh.Send(matterMessage); err != nil { + b.Log.Errorf("Failed to send message: %v", err) + } + } + // webhook doesn't support file uploads, so we add the url manually + for _, f := range msg.Extra["file"] { + fi := f.(config.FileInfo) + if fi.URL != "" { + msg.Text += " " + fi.URL + } + } + } + + // if we have native slack_attachments add them + var attachs []slack.Attachment + for _, attach := range msg.Extra[sSlackAttachment] { + attachs = append(attachs, attach.([]slack.Attachment)...) + } + + iconURL := config.GetIconURL(&msg, b.GetString(iconURLConfig)) + matterMessage := matterhook.OMessage{ + IconURL: iconURL, + Attachments: attachs, + Channel: msg.Channel, + UserName: msg.Username, + Text: msg.Text, + } + if msg.Avatar != "" { + matterMessage.IconURL = msg.Avatar + } + err := b.mh.Send(matterMessage) + if err != nil { + b.Log.Error(err) + return "", err + } + return "", nil +} + +func (b *Bslack) sendRTM(msg config.Message) (string, error) { channelInfo, err := b.getChannel(msg.Channel) if err != nil { return "", fmt.Errorf("could not send message: %v", err) @@ -191,7 +258,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) { } // we get a "slack ", split it ts := strings.Fields(msg.ID) - _, _, err = b.sc.DeleteMessage(channelInfo.ID, ts[1]) + _, _, err = b.rtm.DeleteMessage(channelInfo.ID, ts[1]) if err != nil { return msg.ID, err } @@ -206,39 +273,19 @@ func (b *Bslack) Send(msg config.Message) (string, error) { // Edit message if we have an ID if msg.ID != "" { ts := strings.Fields(msg.ID) - _, _, _, err = b.sc.UpdateMessage(channelInfo.ID, ts[1], msg.Text) + _, _, _, err = b.rtm.UpdateMessage(channelInfo.ID, ts[1], msg.Text) if err != nil { return msg.ID, err } return msg.ID, nil } - // create slack new post parameters - np := slack.NewPostMessageParameters() - if b.GetBool(useNickPrefixConfig) { - np.AsUser = true - } - np.Username = msg.Username - np.LinkNames = 1 // replace mentions - np.IconURL = config.GetIconURL(&msg, b.GetString(iconURLConfig)) - if msg.Avatar != "" { - np.IconURL = msg.Avatar - } - // add a callback ID so we can see we created it - np.Attachments = append(np.Attachments, slack.Attachment{CallbackID: "matterbridge_" + b.uuid}) - // add file attachments - np.Attachments = append(np.Attachments, b.createAttach(msg.Extra)...) - // add slack attachments (from another slack bridge) - if msg.Extra != nil { - for _, attach := range msg.Extra[sSlackAttachment] { - np.Attachments = append(np.Attachments, attach.([]slack.Attachment)...) - } - } + messageParameters := b.prepareMessageParameters(&msg) // Upload a file if it exists if msg.Extra != nil { for _, rmsg := range helper.HandleExtra(&msg, b.General) { - _, _, err = b.sc.PostMessage(channelInfo.ID, rmsg.Username+rmsg.Text, np) + _, _, err = b.rtm.PostMessage(channelInfo.ID, rmsg.Username+rmsg.Text, *messageParameters) if err != nil { b.Log.Error(err) } @@ -248,15 +295,35 @@ func (b *Bslack) Send(msg config.Message) (string, error) { } // Post normal message - _, id, err := b.sc.PostMessage(channelInfo.ID, msg.Text, np) + _, id, err := b.rtm.PostMessage(channelInfo.ID, msg.Text, *messageParameters) if err != nil { return "", err } return "slack " + id, nil } -func (b *Bslack) Reload(cfg *bridge.Config) (string, error) { - return "", nil +func (b *Bslack) prepareMessageParameters(msg *config.Message) *slack.PostMessageParameters { + params := slack.NewPostMessageParameters() + if b.GetBool(useNickPrefixConfig) { + params.AsUser = true + } + params.Username = msg.Username + params.LinkNames = 1 // replace mentions + params.IconURL = config.GetIconURL(msg, b.GetString(iconURLConfig)) + if msg.Avatar != "" { + params.IconURL = msg.Avatar + } + // add a callback ID so we can see we created it + params.Attachments = append(params.Attachments, slack.Attachment{CallbackID: "matterbridge_" + b.uuid}) + // add file attachments + params.Attachments = append(params.Attachments, b.createAttach(msg.Extra)...) + // add slack attachments (from another slack bridge) + if msg.Extra != nil { + for _, attach := range msg.Extra[sSlackAttachment] { + params.Attachments = append(params.Attachments, attach.([]slack.Attachment)...) + } + } + return ¶ms } func (b *Bslack) createAttach(extra map[string][]interface{}) []slack.Attachment { @@ -291,63 +358,3 @@ func extractStringField(data map[string]interface{}, field string) string { } return "" } - -// sendWebhook uses the configured WebhookURL to send the message -func (b *Bslack) sendWebhook(msg config.Message) (string, error) { - // skip events - if msg.Event != "" { - return "", nil - } - - if b.GetBool(useNickPrefixConfig) { - msg.Text = msg.Username + msg.Text - } - - if msg.Extra != nil { - // this sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE - for _, rmsg := range helper.HandleExtra(&msg, b.General) { - iconURL := config.GetIconURL(&rmsg, b.GetString(iconURLConfig)) - matterMessage := matterhook.OMessage{ - IconURL: iconURL, - Channel: msg.Channel, - UserName: rmsg.Username, - Text: rmsg.Text, - } - if err := b.mh.Send(matterMessage); err != nil { - b.Log.Errorf("Failed to send message: %v", err) - } - } - - // webhook doesn't support file uploads, so we add the url manually - for _, f := range msg.Extra["file"] { - fi := f.(config.FileInfo) - if fi.URL != "" { - msg.Text += " " + fi.URL - } - } - } - - // if we have native slack_attachments add them - var attachs []slack.Attachment - for _, attach := range msg.Extra[sSlackAttachment] { - attachs = append(attachs, attach.([]slack.Attachment)...) - } - - iconURL := config.GetIconURL(&msg, b.GetString(iconURLConfig)) - matterMessage := matterhook.OMessage{ - IconURL: iconURL, - Attachments: attachs, - Channel: msg.Channel, - UserName: msg.Username, - Text: msg.Text, - } - if msg.Avatar != "" { - matterMessage.IconURL = msg.Avatar - } - err := b.mh.Send(matterMessage) - if err != nil { - b.Log.Error(err) - return "", err - } - return "", nil -}