Skip to content

Commit

Permalink
Merge de7f4e9 into 7c30305
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3r committed Mar 23, 2021
2 parents 7c30305 + de7f4e9 commit 06a61d4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 39 deletions.
1 change: 1 addition & 0 deletions models/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Bot struct {
Metrics bool `mapstructure:"metrics,omitempty"`
CustomHelpText string `mapstructure:"custom_help_text,omitempty"`
DisableNoMatchHelp bool `mapstructure:"disable_no_match_help,omitempty"`
RespondToBots bool `mapstructure:"respond_to_bots,omitempty"`
// System
Log logrus.Logger
RunChat bool
Expand Down
5 changes: 2 additions & 3 deletions remote/discord/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ func (c *Client) InteractiveComponents(inputMsgs chan<- models.Message, message
// message is created on any channel that the authenticated bot has access to
func handleDiscordMessage(bot *models.Bot, inputMsgs chan<- models.Message) interface{} {
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by bots
// This isn't required in this specific example but it's a good practice
if m.Author.Bot {
// check if we should respond to bot messages
if m.Author.Bot && !bot.RespondToBots {
return
}

Expand Down
37 changes: 32 additions & 5 deletions remote/slack/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,44 @@ func handleCallBack(api *slack.Client, event slackevents.EventsAPIInnerEvent, bo

// process the event
bot.Log.Debugf("getEventsAPIEventHandler: Received event '%s'", event.Type)

switch ev := event.Data.(type) {
// There are Events API specific MessageEvents
// https://api.slack.com/events/message.channels
case *slackevents.MessageEvent:
senderID := ev.User
// Only process messages that aren't from the bot itself

// check if we should respond to other bot messages
if ev.BotID != "" && bot.RespondToBots {
senderID = ev.BotID
}

// only process messages that aren't from our bot
if senderID != "" && bot.ID != senderID {
channel := ev.Channel
msgType, err := getMessageType(channel)
if err != nil {
bot.Log.Debug(err.Error())
}

text, mentioned := removeBotMention(ev.Text, bot.ID)

// if senderID is a BotID, we could use .GetBotInfo()
// but this should also give us information on the sender
// including whether it is a bot
user, err := api.GetUserInfo(senderID)
if err != nil && senderID != "" { // we only care if senderID is not empty and there's an error (senderID == "" could be a thread from a message)
if err != nil {
bot.Log.Errorf("getEventsAPIEventHandler: Did not get Slack user info: %s", err.Error())
}

timestamp := ev.TimeStamp
threadTimestamp := ev.ThreadTimeStamp

link, err := api.GetPermalink(&slack.PermalinkParameters{Channel: channel, Ts: timestamp})
if err != nil {
link = ""
}

inputMsgs <- populateMessage(models.NewMessage(), msgType, channel, text, timestamp, threadTimestamp, link, mentioned, user, bot)
}
// This is an Event shared between RTM and the Events API
Expand Down Expand Up @@ -548,24 +562,37 @@ func readFromEventsAPI(api *slack.Client, vToken string, inputMsgs chan<- models
// nolint:gocyclo // needs refactor
func readFromRTM(rtm *slack.RTM, inputMsgs chan<- models.Message, bot *models.Bot) {
go rtm.ManageConnection()

for {
msg := <-rtm.IncomingEvents

switch ev := msg.Data.(type) {
case *slack.MessageEvent:
senderID := ev.User
// Sometimes message events in RTM don't have a User ID?
// Also, only process messages that aren't from the bot itself

// check if we should respond to other bot messages
if ev.BotID != "" && bot.RespondToBots {
senderID = ev.BotID
}

// only process messages that aren't from our bot
if senderID != "" && bot.ID != senderID {
channel := ev.Channel
msgType, err := getMessageType(channel)
if err != nil {
bot.Log.Debug(err.Error())
}

text, mentioned := removeBotMention(ev.Text, bot.ID)

// if senderID is a BotID, we could use .GetBotInfo()
// but this should also give us information on the sender
// including whether it is a bot
user, err := rtm.GetUserInfo(senderID)
if err != nil && senderID != "" { // we only care if senderID is not empty and there's an error (senderID == "" could be a thread from a message)
if err != nil {
bot.Log.Errorf("Did not get Slack user info: %s", err.Error())
}

timestamp := ev.Timestamp
threadTimestamp := ev.ThreadTimestamp

Expand Down
70 changes: 39 additions & 31 deletions remote/telegram/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,40 +68,48 @@ func (c *Client) Read(inputMsgs chan<- models.Message, rules map[string]models.R
continue
}

msg, mentioned := processMessageText(m.Text, bot.Name)

// support slash commands
if len(m.Command()) > 0 {
fullCmd := fmt.Sprintf("%s %s", m.Command(), m.CommandArguments())
mentioned = true
msg = strings.TrimSpace(fullCmd)
// check if we should respond to other bot messages
if m.From.IsBot && !bot.RespondToBots {
continue
}

message := models.NewMessage()
message.Timestamp = strconv.FormatInt(m.Time().Unix(), 10)
message.Type = mapMessageType(*m)
message.Input = msg
message.Output = ""
message.ID = strconv.Itoa(m.MessageID)
message.Service = models.MsgServiceChat
message.BotMentioned = mentioned
message.ChannelID = strconv.FormatInt(m.Chat.ID, 10)

// populate message with metadata
if m.From != nil {
message.Vars["_user.name"] = m.From.UserName
message.Vars["_user.firstname"] = m.From.FirstName
message.Vars["_user.lastname"] = m.From.LastName
message.Vars["_user.id"] = strconv.Itoa(m.From.ID)
message.Vars["_user.realnamenormalized"] = fmt.Sprintf("%s %s", m.From.FirstName, m.From.LastName)
message.Vars["_user.displayname"] = m.From.UserName
message.Vars["_user.displaynamenormalized"] = m.From.UserName
// only process messages not from out bot
if m.From.ID != botuser.ID {
msg, mentioned := processMessageText(m.Text, bot.Name)

// support slash commands
if len(m.Command()) > 0 {
fullCmd := fmt.Sprintf("%s %s", m.Command(), m.CommandArguments())
mentioned = true
msg = strings.TrimSpace(fullCmd)
}

message := models.NewMessage()
message.Timestamp = strconv.FormatInt(m.Time().Unix(), 10)
message.Type = mapMessageType(*m)
message.Input = msg
message.Output = ""
message.ID = strconv.Itoa(m.MessageID)
message.Service = models.MsgServiceChat
message.BotMentioned = mentioned
message.ChannelID = strconv.FormatInt(m.Chat.ID, 10)

// populate message with metadata
if m.From != nil {
message.Vars["_user.name"] = m.From.UserName
message.Vars["_user.firstname"] = m.From.FirstName
message.Vars["_user.lastname"] = m.From.LastName
message.Vars["_user.id"] = strconv.Itoa(m.From.ID)
message.Vars["_user.realnamenormalized"] = fmt.Sprintf("%s %s", m.From.FirstName, m.From.LastName)
message.Vars["_user.displayname"] = m.From.UserName
message.Vars["_user.displaynamenormalized"] = m.From.UserName
}

message.Vars["_channel.id"] = message.ChannelID
message.Vars["_channel.name"] = m.Chat.Title

inputMsgs <- message
}

message.Vars["_channel.id"] = message.ChannelID
message.Vars["_channel.name"] = m.Chat.Title

inputMsgs <- message
}
}

Expand Down

0 comments on commit 06a61d4

Please sign in to comment.