Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 4a0f42142f1d8f324b34c1b42b5af1a5226e86c1 Author: Duco van Amstel <duco.vanamstel@gmail.com> Date: Mon Oct 22 10:43:57 2018 +0000 Clean up message send logic (slack). (#531) diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 6585c9f..2022ebc 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -1656 +16510 @@ 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)   @@ -1777 +18170 @@ 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) @@ -1917 +2587 @@ func (b *Bslack) Send(msg config.Message) (string, error) {   }   // we get a "slack <ID>", 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   } @@ -20639 +27319 @@ 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)   } @@ -24815 +29535 @@ 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 &params  }    func (b *Bslack) createAttach(extra map[string][]interface{}) []slack.Attachment { @@ -29163 +3583 @@ 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 -}