From 3e858462ac0a60115223f31adc88321dda1e3cc0 Mon Sep 17 00:00:00 2001 From: jowj Date: Tue, 9 Jul 2019 17:22:26 -0500 Subject: [PATCH] Begin migration to v2 of slack's sdk - I fucking hate this 'decorate everything' shit slack wants - its deeply confusing. - i've got most stuff working, with reactions_add working great - handling commands is bad tho; bot will handle it and respond, then - crash afterwards. I don't get it. --- mojo-bot-v2.py | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 mojo-bot-v2.py diff --git a/mojo-bot-v2.py b/mojo-bot-v2.py new file mode 100644 index 0000000..9f391eb --- /dev/null +++ b/mojo-bot-v2.py @@ -0,0 +1,115 @@ +import os +import slack +import pdb +import re + +EXAMPLE_COMMAND = "do" +MENTION_REGEX = "^<@(|[WU].+)>(.*)" +bot_channel = "bots-like-gaston" + + +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 reactable_string(text): + """Return regex objects matching interesting strings + """ + reactable_array = [] + if re.search(r"\bai\b", text.lower()) is not None: + reactable_array.append('ai') + if 'furry' in text.lower() or 'furries' in text.lower() or 'fursuit' in text.lower(): + # no processing needed because: honestly its funnier even if it gets the word boundary wrong. + reactable_array.append('furry') + if 'flavor town' in text.lower() or 'flavortown' in text.lower() or 'guy fieri' in text.lower(): + # no processing needed because: honestly its funnier even if it gets the word boundary wrong. + reactable_array.append('flavortown') + return reactable_array + + +# def react_to_message(reaction): +# payload['web_client'].reactions_add( +# channel = channel_id, +# name = reaction, +# thread_ts = thread_ts +# ) + + +@slack.RTMClient.run_on(event='message') +def handle_messages(**payload): + """ + Does a lot. Fucking decorators. + 1. if the message has react-able strings, react to the message appropriately + 2. if the message starts with @botname and has a command i've planned for, respond appropriately. + """ + data = payload['data'] + channel_id = data['channel'] + thread_ts = data['ts'] + reactions_needed = reactable_string(data['text']) + + + def react_to_message(reaction): + payload['web_client'].reactions_add( + channel = channel_id, + name = reaction, + timestamp = thread_ts + ) + + + if 'ai' in reactions_needed: + react_to_message("robot_face") + if 'furry' in reactions_needed: + react_to_message("eggplant") + react_to_message("sweat_drops") + if 'flavortown' in reactions_needed: + react_to_message("dark_sunglasses") + react_to_message("guyfieri") + + is_command = parse_direct_mention(data['text']) + + if is_command != (None, None): + if is_command[1].startswith("say"): + response = f"{is_command[1]}" + if is_command[1].startswith("furry"): + response = f"{is_command[1]}" + if is_command[1].startswith("download"): + response = "you wouldn't download a car" + if "arke" in is_command[1]: + pass + if "pence" in is_command[1]: + response = "mother wouldn't want me to say that" + else: + response = "that's not my job, bithc" + + webclient = payload['web_client'] + webclient.chat_postMessage( + channel=channel_id, + text=response, + thread_ts=thread_ts + ) + + +#@slack.RTMClient.run_on(event='message') +def channel_specific_test(**payload): + data = payload['data'] + + webclient = payload['web_client'] + for entry in webclient.conversations_list().data['channels']: + if bot_channel in entry['name']: + print(entry['id']) + return entry['id'] + + + +if __name__ == '__main__': + slack_token = os.environ["SLACK_API_TOKEN"] + rtm_client = slack.RTMClient(token=slack_token) + rtm_client.start() + + channel_specific_test