def test_ignores_unmonitored_channels(self): client = WebClient() client.chat_postMessage = MagicMock() subject = LoggingBot(client, [], ["MONITORED"], [], [], []) handled = subject.handle_message("NOT_MONITORED", "user", "text", bot_profile="bot_profile") self.assertFalse(handled) client.chat_postMessage.assert_not_called()
def test_triggers_replies_in_a_thread(self): client = WebClient() client.chat_postMessage = MagicMock() user = "******" channel = "channel" trigger_word = "help me" subject = LoggingBot(client, [trigger_word], [channel], [], [], []) handled = subject.handle_message(channel, user, trigger_word, ts="ts") self.assertTrue(handled) self.assertNotEqual( "", client.chat_postMessage.call_args.kwargs.get("thread_ts", ""))
def test_ignores_bot_messages(self): client = WebClient() client.chat_postMessage = MagicMock() logging.debug = MagicMock() subject = LoggingBot(client, [], [], [], [], []) handled = subject.handle_message("channel", "user", "text", bot_profile="bot_profile") self.assertFalse(handled) client.chat_postMessage.assert_not_called() logging.debug.assert_called_with(AnyStringWith("bot"))
def test_triggers_does_not_trigger_in_threads(self): client = WebClient() client.chat_postMessage = MagicMock() user = "******" channel = "channel" trigger_word = "help me" subject = LoggingBot(client, [trigger_word], [channel], [], [], []) handled = subject.handle_message(channel, user, trigger_word, thread_ts="ts") self.assertFalse(handled) client.chat_postMessage.assert_not_called()
def test_ignores_admin_messages(self): client = WebClient() client.chat_postMessage = MagicMock() logging.debug = MagicMock() user = "******" trigger_word = "triggered!" channel = "channel_8" subject = LoggingBot(client, [trigger_word], [channel], [], [user], []) handled = subject.handle_message(channel, user, "text") self.assertFalse(handled) client.chat_postMessage.assert_not_called() logging.debug.assert_called_with(AnyStringWith("admin"))
def test_triggers_trigger_words(self): client = WebClient() client.chat_postMessage = MagicMock() user = "******" channel = "channel" trigger_words = ["help me"] for msg, should_handle in [("good morning", False), ("help", False), ("help me", True)]: subject = LoggingBot(client, trigger_words, [channel], [], [], []) handled = subject.handle_message(channel, user, msg, ts="ts") self.assertEqual(should_handle, handled, msg="Message=%s" % msg) if handled: self.assertEqual( user, client.chat_postMessage.call_args.kwargs.get("user"))
def test_executes_commands_in_channels(self): client = WebClient() client.chat_postMessage = MagicMock() channel = "channel" subject = LoggingBot(client, [], [channel], [], [], []) # choose the first command cmd = list(consts.COMMANDS.keys())[0] user = "******" handled = subject.handle_message(channel, user, cmd, ts="ts") self.assertTrue(handled) # because we did not supply a thread_ts to handle_message that # indicates the message was NOT within an existing thread so we should # not see a thread_ts (which means post in the original channel) thread_ts = client.chat_postMessage.call_args.kwargs.get("thread_ts") self.assertFalse(thread_ts)
def test_executes_commands_in_threads(self): client = WebClient() client.chat_postMessage = MagicMock() channel = "channel" subject = LoggingBot(client, [], [channel], [], [], []) # choose the first command cmd = list(consts.COMMANDS.keys())[0] user = "******" handled = subject.handle_message(channel, user, cmd, ts="ts", thread_ts="thread_ts") self.assertTrue(handled) # because we supplied a thread_ts to handle_message that indicates the # message was within an existing thread so we should see thread_ts # here. thread_ts = client.chat_postMessage.call_args.kwargs.get( "thread_ts", "") self.assertEqual("thread_ts", thread_ts)
def test_executes_commands_for_admins(self): client = WebClient() client.chat_postMessage = MagicMock() channel = "channel" user = "******" subject = LoggingBot(client, [], [channel], [], [user], []) # choose the first command cmd = list(consts.COMMANDS.keys())[0] handled = subject.handle_message(channel, user, cmd, ts="ts", thread_ts="thread_ts") self.assertTrue(handled, "handled message") args = client.chat_postMessage.call_args.kwargs block = consts.COMMANDS[cmd].items() # block is the message text that gets added when the command is called for key, value in block: self.assertIn(key, args, "key=%s" % key) self.assertEqual(value, args[key], "key=%s value=%s" % (key, value))
import config from bot import LoggingBot # Initialize a Flask app to host the events adapter app = Flask(__name__) metrics = PrometheusMetrics(app) slack_events_adapter = SlackEventAdapter(config.SlackSigningSecret, "/slack/events", app) # Initialize a Web API client slack_client = WebClient(token=config.SlackBotToken) # send a test message to make sure we can access what we need slack_client.api_test() bot = LoggingBot(slack_client, config.TriggerWords, config.OnboardChannels, config.AdminGroups, config.AdminUsers, config.OnboardIgnoredUsers) @app.route("/ping") def ping(): return 'PONG' # ================ Member Joined Channel Event =============== # # When a user first joins a channel that the bot is watching. # We want to provide users joining channels with an ephemeral # welcome message. The ephemeral message is only visible to # the user to whom it is directed. @slack_events_adapter.on("member_joined_channel") def onboarding_message(payload):