Thumbnail

rani/matterbridge.git

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

commit 7c363866c5447e344a2d6fab5d79b6da79bac4df Author: Wim <wim@42.be> Date: Fri Jul 13 23:23:11 2018 +0000 Add support for slack channels by ID. Closes #436 diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 417a433..d40c418 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -1914 +1915 @@ import (  )    type Bslack struct { - mh *matterhook.Client - sc *slack.Client - rtm *slack.RTM - Users []slack.User - Usergroups []slack.UserGroup - si *slack.Info - channels []slack.Channel - uuid string + mh *matterhook.Client + sc *slack.Client + rtm *slack.RTM + Users []slack.User + Usergroups []slack.UserGroup + si *slack.Info + channels []slack.Channel + UseChannelID bool + uuid string   *bridge.Config   sync.RWMutex  } @@ -986 +9920 @@ func (b *Bslack) Disconnect() error {  }    func (b *Bslack) JoinChannel(channel config.ChannelInfo) error { + // use ID:channelid and resolve it to the actual name + idcheck := strings.Split(channel.Name, "ID:") + if len(idcheck) > 1 { + b.UseChannelID = true + ch, err := b.sc.GetChannelInfo(idcheck[1]) + if err != nil { + return err + } + channel.Name = ch.Name + if err != nil { + return err + } + } +   // we can only join channels using the API   if b.sc != nil {   if strings.HasPrefix(b.GetString("Token"), "xoxb") { @@ -13111 +1467 @@ func (b *Bslack) Send(msg config.Message) (string, error) {   return b.sendWebhook(msg)   }   - // get the slack channel - schannel, err := b.getChannelByName(msg.Channel) - if err != nil { - return "", err - } + channelID := b.getChannelID(msg.Channel)     // Delete message   if msg.Event == config.EVENT_MSG_DELETE { @@ -1457 +1567 @@ 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(schannel.ID, ts[1]) + _, _, err := b.sc.DeleteMessage(channelID, ts[1])   if err != nil {   return msg.ID, err   } @@ -1607 +1717 @@ 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(schannel.ID, ts[1], msg.Text) + _, _, _, err := b.sc.UpdateMessage(channelID, ts[1], msg.Text)   if err != nil {   return msg.ID, err   } @@ -19216 +20316 @@ func (b *Bslack) Send(msg config.Message) (string, error) {   // Upload a file if it exists   if msg.Extra != nil {   for _, rmsg := range helper.HandleExtra(&msg, b.General) { - b.sc.PostMessage(schannel.ID, rmsg.Username+rmsg.Text, np) + b.sc.PostMessage(channelID, rmsg.Username+rmsg.Text, np)   }   // check if we have files to upload (from slack, telegram or mattermost)   if len(msg.Extra["file"]) > 0 { - b.handleUploadFile(&msg, schannel.ID) + b.handleUploadFile(&msg, channelID)   }   }     // Post normal message - _, id, err := b.sc.PostMessage(schannel.ID, msg.Text, np) + _, id, err := b.sc.PostMessage(channelID, msg.Text, np)   if err != nil {   return "", err   } @@ -4866 +49710 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er     rmsg := config.Message{Text: ev.Text, Channel: channel.Name, Account: b.Account, ID: "slack " + ev.Timestamp, Extra: make(map[string][]interface{})}   + if b.UseChannelID { + rmsg.Channel = "ID:" + channel.ID + } +   // find the user id and name   if ev.User != "" && ev.SubType != messageDeleted && ev.SubType != "file_comment" {   user, err := b.rtm.GetUserInfo(ev.User) @@ -6823 +69716 @@ func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {   }   return false  } + +func (b *Bslack) getChannelID(name string) string { + idcheck := strings.Split(name, "ID:") + if len(idcheck) > 1 { + return idcheck[1] + } + for _, channel := range b.channels { + if channel.Name == name { + return channel.ID + } + } + return "" +} diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 5844561..790eaad 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -13776 +13777 @@ enable=true #gitter - username/room #xmpp - channel #slack - channel (without the #) + # - ID:C123456 (where C123456 is the channel ID) does not work with webhook #discord - channel (without the #) # - ID:123456789 (where 123456789 is the channel ID) # (https://github.com/42wim/matterbridge/issues/57)