Skip to content

Commit

Permalink
Message parsing code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PrasadG193 committed Jan 18, 2019
1 parent 6062bd4 commit 3103fef
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions pkg/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,44 @@ func (b *Bot) Start() {
logging.Logger.Debug("Connection Info: ", ev.Info)

case *slack.MessageEvent:
// Check if message posted in a channel
chanInfo, _ := api.GetChannelInfo(ev.Channel)

// Check if message posted in a group
groupInfo, _ := api.GetGroupInfo(ev.Channel)
// Skip if message posted by BotKube
if ev.User == botID {
continue
}

if chanInfo != nil || groupInfo != nil {
// Message posted in a channel/group
inMessage := ""
// Check if message posted in a channel
_, err := api.GetChannelInfo(ev.Channel)
if err == nil {
// Message posted in a channel
// Serve only if starts with mention
if !strings.HasPrefix(ev.Text, "<@"+botID+"> ") {
continue
}
logging.Logger.Debugf("Slack incoming message: %+v", ev)
msg := strings.TrimPrefix(ev.Text, "<@"+botID+"> ")
sm := slackMessage{
ChannelID: ev.Channel,
BotID: botID,
InMessage: msg,
RTM: rtm,
} else {
// Check if message posted in a group
_, err := api.GetGroupInfo(ev.Channel)
if err == nil {
// Message posted in a group
// Serve only if starts with mention
if !strings.HasPrefix(ev.Text, "<@"+botID+"> ") {
continue
}
}
sm.HandleMessage(b.AllowKubectl)
continue
}

// Message posted as a DM
// Skip if message posted by BotKube
if ev.User == botID {
continue
}
logging.Logger.Debugf("Slack incoming message: %+v", ev)
msg := ev.Text
inMessage = ev.Text

// Trim @BotKube prefix if exists
if strings.HasPrefix(ev.Text, "<@"+botID+"> ") {
msg = strings.TrimPrefix(ev.Text, "<@"+botID+"> ")
inMessage = strings.TrimPrefix(ev.Text, "<@"+botID+"> ")
}
sm := slackMessage{
ChannelID: ev.Channel,
BotID: botID,
InMessage: msg,
InMessage: inMessage,
RTM: rtm,
}
sm.HandleMessage(b.AllowKubectl)
Expand Down

0 comments on commit 3103fef

Please sign in to comment.