Skip to content

Commit

Permalink
Merge e4f20bf into 41a0ba4
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3r committed Jan 27, 2020
2 parents 41a0ba4 + e4f20bf commit 8be3887
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 47 deletions.
32 changes: 31 additions & 1 deletion core/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,50 +47,73 @@ func configureChatApplication(bot *models.Bot) {
if err != nil {
bot.Log.Warnf("Could not configure bot Name: %s", err.Error())
}

bot.Name = token

if bot.ChatApplication != "" {
switch strings.ToLower(bot.ChatApplication) {
case "discord":
// Bot token from Discord
// Discord bot token
token, err := utils.Substitute(bot.DiscordToken, map[string]string{})
if err != nil {
bot.Log.Warnf("Could not set Discord Token: %s", err.Error())
bot.RunChat = false
}

if token == "" {
bot.Log.Warnf("Discord Token is empty: '%s'", token)
bot.RunChat = false
}

bot.DiscordToken = token

// Discord Server ID
// See https://support.discordapp.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-
serverID, err := utils.Substitute(bot.DiscordServerID, map[string]string{})
if err != nil {
bot.Log.Warnf("Could not set Discord Server ID: %s", err.Error())
bot.RunChat = false
}

if serverID == "" {
bot.Log.Warnf("Discord Server ID is empty: '%s'", serverID)
bot.RunChat = false
}

bot.DiscordServerID = serverID

case "slack":
// Slack bot token
token, err := utils.Substitute(bot.SlackToken, map[string]string{})
if err != nil {
bot.Log.Warnf("Could not set Slack Token: %s", err.Error())
bot.RunChat = false
}

if token == "" {
bot.Log.Warnf("Slack Token is empty: %s", token)
bot.RunChat = false
}

bot.SlackToken = token

// Slack verification token
vToken, err := utils.Substitute(bot.SlackVerificationToken, map[string]string{})
if err != nil {
bot.Log.Warnf("Could not set Slack Verification Token: %s", err.Error())
bot.Log.Warn("Defaulting to use Slack RTM")

vToken = ""
}

bot.SlackVerificationToken = vToken

// Slack workspace token
wsToken, err := utils.Substitute(bot.SlackWorkspaceToken, map[string]string{})
if err != nil {
bot.Log.Warnf("Could not set Slack Workspace Token: %s", err.Error())
}

bot.SlackWorkspaceToken = wsToken

// Get Slack Events path
Expand All @@ -100,6 +123,7 @@ func configureChatApplication(bot *models.Bot) {
bot.Log.Warn("Defaulting to use Slack RTM")
bot.SlackVerificationToken = ""
}

bot.SlackEventsCallbackPath = eCallbackPath

// Get Slack Interactive Components path
Expand All @@ -108,10 +132,12 @@ func configureChatApplication(bot *models.Bot) {
bot.Log.Errorf("Could not set Slack Interactive Components callback path: %s", err.Error())
bot.InteractiveComponents = false
}

if iCallbackPath == "" {
bot.Log.Warnf("Slack Interactive Components callback path is empty: %s", iCallbackPath)
bot.InteractiveComponents = false
}

bot.SlackInteractionsCallbackPath = iCallbackPath

default:
Expand All @@ -125,18 +151,22 @@ func validateRemoteSetup(bot *models.Bot) {
if bot.ChatApplication != "" {
bot.RunChat = true
}

if bot.CLI {
bot.RunCLI = true
}

if !bot.CLI && bot.ChatApplication == "" {
bot.Log.Fatalf("No chat_application specified and cli mode is not enabled. Exiting...")
}

if bot.Scheduler {
bot.RunScheduler = true
if bot.CLI && bot.ChatApplication == "" {
bot.Log.Warn("Scheduler does not support scheduled outputs to CLI mode")
bot.RunScheduler = false
}

if bot.ChatApplication == "" {
bot.Log.Warn("Scheduler did not find any configured chat applications. Scheduler is closing")
bot.RunScheduler = false
Expand Down
30 changes: 24 additions & 6 deletions core/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func Test_configureChatApplication(t *testing.T) {
testBotSlackNoToken.ChatApplication = "slack"
validateRemoteSetup(testBotSlackNoToken)

testBotBadName := new(models.Bot)
testBotBadName.CLI = true
testBotBadName.ChatApplication = "slack"
testBotBadName.Name = "${BOT_NAME}"
validateRemoteSetup(testBotBadName)

testBotSlackBadToken := new(models.Bot)
testBotSlackBadToken.CLI = true
testBotSlackBadToken.ChatApplication = "slack"
Expand Down Expand Up @@ -150,12 +156,21 @@ func Test_configureChatApplication(t *testing.T) {
testBotDiscordBadToken.DiscordToken = "${TOKEN}"
validateRemoteSetup(testBotDiscordBadToken)

testBotDiscord := new(models.Bot)
testBotDiscord.CLI = true
testBotDiscord.ChatApplication = "discord"
testBotDiscord.DiscordToken = "${TEST_DISCORD_TOKEN}"
testBotDiscordServerID := new(models.Bot)
testBotDiscordServerID.CLI = true
testBotDiscordServerID.ChatApplication = "discord"
testBotDiscordServerID.DiscordToken = "${TEST_DISCORD_TOKEN}"
testBotDiscordServerID.DiscordServerID = "${TEST_DISCORD_SERVER_ID}"
os.Setenv("TEST_DISCORD_TOKEN", "TESTTOKEN")
validateRemoteSetup(testBotDiscord)
os.Setenv("TEST_DISCORD_SERVER_ID", "TESTSERVERID")
validateRemoteSetup(testBotDiscordServerID)

testBotDiscordBadServerID := new(models.Bot)
testBotDiscordBadServerID.CLI = true
testBotDiscordBadServerID.ChatApplication = "discord"
testBotDiscordBadServerID.DiscordToken = "${TEST_DISCORD_TOKEN}"
testBotDiscordBadServerID.DiscordServerID = "${TOKEN}"
validateRemoteSetup(testBotDiscordServerID)

tests := []struct {
name string
Expand All @@ -166,6 +181,7 @@ func Test_configureChatApplication(t *testing.T) {
{"Fail", args{bot: testBot}, false, false},
{"Fail - no chat_application not set", args{bot: testBotNoChat}, false, false},
{"Fail - Invalid value for chat_application", args{bot: testBotInvalidChat}, false, false},
{"Bad Name", args{bot: testBotBadName}, false, false},
{"Slack - no token", args{bot: testBotSlackNoToken}, false, false},
{"Slack - bad token", args{bot: testBotSlackBadToken}, false, false},
{"Slack - bad verification token", args{bot: testBotSlackBadVerificationToken}, false, false},
Expand All @@ -176,7 +192,8 @@ func Test_configureChatApplication(t *testing.T) {
{"Slack w/ bad events callback", args{bot: testBotSlackEventsCallbackFail}, true, false},
{"Discord - no token", args{bot: testBotDiscordNoToken}, false, false},
{"Discord - bad token", args{bot: testBotDiscordBadToken}, false, false},
{"Discord", args{bot: testBotDiscord}, true, false},
{"Discord w/ server id", args{bot: testBotDiscordServerID}, true, false},
{"Discord w/ bad server id", args{bot: testBotDiscordBadServerID}, false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -193,6 +210,7 @@ func Test_configureChatApplication(t *testing.T) {

os.Unsetenv("TEST_SLACK_TOKEN")
os.Unsetenv("TEST_DISCORD_TOKEN")
os.Unsetenv("TEST_DISCORD_SERVER_ID")
os.Unsetenv("TEST_SLACK_INTERACTIONS_CALLBACK_PATH")
os.Unsetenv("TEST_SLACK_INTERACTIONS_CALLBACK_PATH_FAIL")
}
Expand Down
1 change: 1 addition & 0 deletions models/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Bot struct {
SlackEventsCallbackPath string `mapstructure:"slack_events_callback_path"`
SlackInteractionsCallbackPath string `mapstructure:"slack_interactions_callback_path"`
DiscordToken string `mapstructure:"discord_token"`
DiscordServerID string `mapstructure:"discord_server_id"`
Users map[string]string `mapstructure:"slack_users"`
UserGroups map[string]string `mapstructure:"slack_usergroups"`
Rooms map[string]string `mapstructure:"slack_channels"`
Expand Down
100 changes: 90 additions & 10 deletions remote/discord/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,107 @@ func populateMessage(message models.Message, msgType models.MessageType, channel
message.BotMentioned = mentioned
message.ID = id

// if msgType != models.MsgTypeDirect {
// name, ok := findKey(bot.Rooms, channel)
// if !ok {
// bot.Log.Warnf("Could not find name of channel '%s'.", channel)
// }
// message.ChannelName = name
// }
if msgType != models.MsgTypeDirect {
name, ok := findKey(bot.Rooms, channel)
if !ok {
bot.Log.Warnf("Could not find name of channel '%s'.", channel)
}

message.ChannelName = name
}

message.Vars["_channel.id"] = channel
message.Vars["_channel.name"] = channel
message.Vars["_channel.name"] = message.ChannelName

// Populate message user sender
// These will be accessible on rules via ${_user.email}, etc
if user != nil { // nil user implies a message from an api/bot (i.e. not an actual user)
message.Vars["_user.email"] = user.Email
// message.Vars["_user.firstname"] = ""
// message.Vars["_user.lastname"] = ""
message.Vars["_user.name"] = user.Username
message.Vars["_user.id"] = user.ID
}

message.Debug = true

return message
}

// send - handles the sending logic of a message going to Discord
func send(dg *discordgo.Session, message models.Message, bot *models.Bot) {
if message.DirectMessageOnly {
err := handleDirectMessage(dg, message, bot)
if err != nil {
bot.Log.Errorf("Problem sending message: %s", err.Error())
}
} else {
err := handleNonDirectMessage(dg, message, bot)
if err != nil {
bot.Log.Errorf("Problem sending message: %s", err.Error())
}
}
}

// handleDirectMessage - handle sending logic for direct messages
func handleDirectMessage(dg *discordgo.Session, message models.Message, bot *models.Bot) error {
// Is output to rooms set?
if len(message.OutputToRooms) > 0 {
bot.Log.Warn("You have specified 'direct_message_only' as 'true' and provided 'output_to_rooms'." +
" Messages will not be sent to listed rooms. If you want to send messages to these rooms," +
" please set 'direct_message_only' to 'false'.")
}
// Is output to users set?
if len(message.OutputToUsers) > 0 {
bot.Log.Warn("You have specified 'direct_message_only' as 'true' and provided 'output_to_users'." +
" Messages will not be sent to the listed users (other than you). If you want to send messages to other users," +
" please set 'direct_message_only' to 'false'.")
}

userChannel, err := dg.UserChannelCreate(message.Vars["_user.id"])
if err != nil {
return err
}

_, err = dg.ChannelMessageSend(userChannel.ID, message.Output)
if err != nil {
return err
}

return nil
}

// handleNonDirectMessage - handle sending logic for non direct messages
func handleNonDirectMessage(dg *discordgo.Session, message models.Message, bot *models.Bot) error {
if len(message.OutputToUsers) == 0 && len(message.OutputToRooms) == 0 && len(message.Output) > 0 {
_, err := dg.ChannelMessageSend(message.ChannelID, message.Output)
if err != nil {
return err
}
}

// message.OutputToRooms is already processed to be the translated IDs
// vs. the originally provided names
if len(message.OutputToRooms) > 0 {
for _, roomID := range message.OutputToRooms {
_, err := dg.ChannelMessageSend(roomID, message.Output)
if err != nil {
return err
}
}
}

if len(message.OutputToUsers) > 0 {
for _, user := range message.OutputToUsers {
userChannel, err := dg.UserChannelCreate(bot.Users[user])
if err != nil {
return err
}

_, err = dg.ChannelMessageSend(userChannel.ID, message.Output)
if err != nil {
return err
}
}
}

return nil
}
Loading

0 comments on commit 8be3887

Please sign in to comment.