Fix #1039: messages sent to Slack being synced back

This commit is contained in:
Qais Patankar
2020-03-17 15:17:28 +00:00
parent 6b4b19194e
commit d8471eb047
2 changed files with 26 additions and 4 deletions

View File

@@ -78,7 +78,6 @@ func (b *Bslack) handleSlackClient(messages chan *config.Message) {
// should now tell that we are a member of it.
b.channels.registerChannel(ev.Channel)
case *slack.ConnectedEvent:
b.si = ev.Info
b.channels.populateChannels(true)
b.users.populateUsers(true)
case *slack.InvalidAuthEvent:
@@ -112,6 +111,7 @@ func (b *Bslack) handleMatterHook(messages chan *config.Message) {
// skipMessageEvent skips event that need to be skipped :-)
func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {
fromSelf := (ev.User == b.userID) || (ev.BotID == b.botID)
switch ev.SubType {
case sChannelLeave, sChannelJoin:
return b.GetBool(noSendJoinConfig)
@@ -119,14 +119,14 @@ func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {
return true
case sChannelTopic, sChannelPurpose:
// Skip the event if our bot/user account changed the topic/purpose
if ev.User == b.si.User.ID {
if fromSelf {
return true
}
}
// Skip any messages that we made ourselves or from 'slackbot' (see #527).
if ev.Username == sSlackBotUser ||
(b.rtm != nil && ev.Username == b.si.User.Name) ||
(b.rtm != nil && fromSelf) ||
(len(ev.Attachments) > 0 && ev.Attachments[0].CallbackID == "matterbridge_"+b.uuid) {
return true
}

View File

@@ -24,11 +24,14 @@ type Bslack struct {
mh *matterhook.Client
sc *slack.Client
rtm *slack.RTM
si *slack.Info
cache *lru.Cache
uuid string
useChannelID bool
userID string
// botID is only set for bot accounts
botID string
channels *channels
users *users
@@ -112,6 +115,25 @@ func (b *Bslack) Connect() error {
b.channels = newChannelManager(b.Log, b.sc)
b.users = newUserManager(b.Log, b.sc)
// Get our own user ID
authTestResp, err := b.sc.AuthTest()
if err != nil {
return errors.New("auth_test: " + err.Error())
}
b.userID = authTestResp.UserID
// Get the info of our user ID so that we can determine our bot ID
user, err := b.sc.GetUserInfo(b.userID)
if err != nil {
return errors.New("get_user_info: " + err.Error())
}
if user.IsBot {
b.botID = user.Profile.BotID
b.Log.Debugf("Our bot ID is %#v\n", b.botID)
} else {
b.Log.Debugln("We are not a bot.")
}
b.rtm = b.sc.NewRTM()
go b.rtm.ManageConnection()
go b.handleSlack()