From 17f0cce5fb4fe5de776fe8424864469debfd4783 Mon Sep 17 00:00:00 2001 From: jowj Date: Sat, 27 Jan 2018 10:42:13 -0600 Subject: [PATCH] splitting work out to new file to keep functionality intact during updates --- mojojojo-bot.py | 33 ------------- mojojojo-bot3.py | 126 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 33 deletions(-) create mode 100644 mojojojo-bot3.py diff --git a/mojojojo-bot.py b/mojojojo-bot.py index f96f270..0ce3c84 100644 --- a/mojojojo-bot.py +++ b/mojojojo-bot.py @@ -3,8 +3,6 @@ import time import re import logging from slackclient import SlackClient -import pdb - # instantiate Slack client slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN')) @@ -29,21 +27,6 @@ def parse_bot_commands(slack_events): user_id, message = parse_direct_mention(event["text"]) if user_id == starterbot_id: return message, event["channel"] - # trying to adapt mojojojo-bot2 code here - if reactable_message(event): - channel = event['channel'] - text = event['text'] - if 'blah' in text.lower(): - reactable_timestamp = event.get('ts') - return channel, event, reactable_timestamp - # print(event.get('ts')) # does this populate - # #pdb.set_trace() - # slack_client.api_call( - # 'reactions.add', - # channel = get_channel_ID("BOTS"), - # name = "thumbsup", - # timestamp = event.get('ts') - # ) return None, None def parse_direct_mention(message_text): @@ -102,22 +85,6 @@ if __name__ == "__main__": if command: handle_command(command, channel) time.sleep(RTM_READ_DELAY) - # mojojojo-bot2 functionality below in loop. - # events = slack_client.rtm_read() - # for event in events: - # if reactable_message(event): - # channel = event['channel'] - # text = event['text'] - # if 'blah' in text.lower(): - # print(event.get('ts')) # does this populate - # #pdb.set_trace() - # slack_client.api_call( - # 'reactions.add', - # channel = get_channel_ID("BOTS"), - # name = "thumbsup", - # timestamp = event.get('ts') - # ) - time.sleep(1) logging.info("Client worked. No errors (we think lol)") else: print("Connection failed. Exception traceback printed above.") diff --git a/mojojojo-bot3.py b/mojojojo-bot3.py new file mode 100644 index 0000000..f60075a --- /dev/null +++ b/mojojojo-bot3.py @@ -0,0 +1,126 @@ +import os +import time +import re +import logging +from slackclient import SlackClient +import pdb + +# combine mojojojo1 and 2. + +# instantiate Slack client +slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN')) +# starterbot's user ID in Slack: value is assigned after the bot starts up +starterbot_id = None +# channel i want to get ID from +bot_channel = "bots-like-gaston" + +# constants +RTM_READ_DELAY = 1 # 1 second delay between reading from RTM +EXAMPLE_COMMAND = "do" +MENTION_REGEX = "^<@(|[WU].+)>(.*)" + +def parse_bot_commands(slack_events): + """ + Parses a list of events coming from the Slack RTM API to find bot commands. + If a bot command is found, this function returns a tuple of command and channel. + If its not found, then this function returns None, None. + """ + for event in slack_events: + if event["type"] == "message" and not "subtype" in event: + user_id, message = parse_direct_mention(event["text"]) + if user_id == starterbot_id: + return message, event["channel"] + # trying to adapt mojojojo-bot2 code here + if reactable_message(event): + channel = event['channel'] + text = event['text'] + if 'blah' in text.lower(): + reactable_timestamp = event.get('ts') + return channel, event, reactable_timestamp + # print(event.get('ts')) # does this populate + # #pdb.set_trace() + # slack_client.api_call( + # 'reactions.add', + # channel = get_channel_ID("BOTS"), + # name = "thumbsup", + # timestamp = event.get('ts') + # ) + return None, None + +def parse_direct_mention(message_text): + """ + Finds a direct mention (a mention that is at the beginning) in message text + and returns the user ID which was mentioned. If there is no direct mention, returns None + """ + matches = re.search(MENTION_REGEX, message_text) + # the first group contains the username, the second group contains the remaining message + return (matches.group(1), matches.group(2).strip()) if matches else (None, None) + +def handle_command(command, channel): + """ + Executes bot command if the command is known + """ + # Default response is help text for the user + default_response = "fuck off bithc" + + # Finds and executes the given command, filling in response + response = None + # This is where you start to implement more commands! + if command.startswith(EXAMPLE_COMMAND): + response = "Sure...write some more code then I can do that!" + if command.startswith("say"): + response = "you're not my real dad" + + # Sends the response back to the channel + slack_client.api_call( + "chat.postMessage", + channel=channel, + text=response or default_response + ) + +def reactable_message(event): + """Test whether a (Slack) event is a reaction-able message + + Check whether it's not a DM, it's not empty, and it's actually a message + """ + return 'channel' in event and 'text' in event and event.get('type') == 'message' + +def get_channel_ID(channelName): + for channel in slack_client.api_call('channels.list')["channels"]: + if channel["name"] == channelName: + return channel["id"] + raise Exception("couldn't find channel requested.") + +if __name__ == "__main__": + logging.basicConfig(filename='mojojojo.log', level=logging.INFO) + logging.info('Started') + if slack_client.rtm_connect(with_team_state=False): + print("mojo jojo online, connected, and running!") + # Read bot's user ID by calling Web API method `auth.test` + starterbot_id = slack_client.api_call("auth.test")["user_id"] + while True: + command, channel = parse_bot_commands(slack_client.rtm_read()) + if command: + handle_command(command, channel) + time.sleep(RTM_READ_DELAY) + # mojojojo-bot2 functionality below in loop. + # events = slack_client.rtm_read() + # for event in events: + # if reactable_message(event): + # channel = event['channel'] + # text = event['text'] + # if 'blah' in text.lower(): + # print(event.get('ts')) # does this populate + # #pdb.set_trace() + # slack_client.api_call( + # 'reactions.add', + # channel = get_channel_ID("BOTS"), + # name = "thumbsup", + # timestamp = event.get('ts') + # ) + time.sleep(1) + logging.info("Client worked. No errors (we think lol)") + else: + print("Connection failed. Exception traceback printed above.") + logging.info('Check console for exception traceback.') + logging.info('Finished') \ No newline at end of file