5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-14 03:50:26 +00:00

Add support for slack channels by ID. Closes #436

This commit is contained in:
Wim 2018-07-13 23:23:11 +02:00
parent fa0c4025f7
commit e04dd78624
2 changed files with 47 additions and 18 deletions

View File

@ -19,14 +19,15 @@ import (
) )
type Bslack struct { type Bslack struct {
mh *matterhook.Client mh *matterhook.Client
sc *slack.Client sc *slack.Client
rtm *slack.RTM rtm *slack.RTM
Users []slack.User Users []slack.User
Usergroups []slack.UserGroup Usergroups []slack.UserGroup
si *slack.Info si *slack.Info
channels []slack.Channel channels []slack.Channel
uuid string UseChannelID bool
uuid string
*bridge.Config *bridge.Config
sync.RWMutex sync.RWMutex
} }
@ -98,6 +99,20 @@ func (b *Bslack) Disconnect() error {
} }
func (b *Bslack) JoinChannel(channel config.ChannelInfo) 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 // we can only join channels using the API
if b.sc != nil { if b.sc != nil {
if strings.HasPrefix(b.GetString("Token"), "xoxb") { if strings.HasPrefix(b.GetString("Token"), "xoxb") {
@ -131,11 +146,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
return b.sendWebhook(msg) return b.sendWebhook(msg)
} }
// get the slack channel channelID := b.getChannelID(msg.Channel)
schannel, err := b.getChannelByName(msg.Channel)
if err != nil {
return "", err
}
// Delete message // Delete message
if msg.Event == config.EVENT_MSG_DELETE { if msg.Event == config.EVENT_MSG_DELETE {
@ -145,7 +156,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
} }
// we get a "slack <ID>", split it // we get a "slack <ID>", split it
ts := strings.Fields(msg.ID) ts := strings.Fields(msg.ID)
_, _, err := b.sc.DeleteMessage(schannel.ID, ts[1]) _, _, err := b.sc.DeleteMessage(channelID, ts[1])
if err != nil { if err != nil {
return msg.ID, err return msg.ID, err
} }
@ -160,7 +171,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
// Edit message if we have an ID // Edit message if we have an ID
if msg.ID != "" { if msg.ID != "" {
ts := strings.Fields(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 { if err != nil {
return msg.ID, err return msg.ID, err
} }
@ -192,16 +203,16 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
// Upload a file if it exists // Upload a file if it exists
if msg.Extra != nil { if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(&msg, b.General) { 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) // check if we have files to upload (from slack, telegram or mattermost)
if len(msg.Extra["file"]) > 0 { if len(msg.Extra["file"]) > 0 {
b.handleUploadFile(&msg, schannel.ID) b.handleUploadFile(&msg, channelID)
} }
} }
// Post normal message // 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 { if err != nil {
return "", err return "", err
} }
@ -486,6 +497,10 @@ 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{})} 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 // find the user id and name
if ev.User != "" && ev.SubType != messageDeleted && ev.SubType != "file_comment" { if ev.User != "" && ev.SubType != messageDeleted && ev.SubType != "file_comment" {
user, err := b.rtm.GetUserInfo(ev.User) user, err := b.rtm.GetUserInfo(ev.User)
@ -682,3 +697,16 @@ func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {
} }
return false 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 ""
}

View File

@ -1377,6 +1377,7 @@ enable=true
#gitter - username/room #gitter - username/room
#xmpp - channel #xmpp - channel
#slack - channel (without the #) #slack - channel (without the #)
# - ID:C123456 (where C123456 is the channel ID) does not work with webhook
#discord - channel (without the #) #discord - channel (without the #)
# - ID:123456789 (where 123456789 is the channel ID) # - ID:123456789 (where 123456789 is the channel ID)
# (https://github.com/42wim/matterbridge/issues/57) # (https://github.com/42wim/matterbridge/issues/57)