import os import time import slack import pdb import json import re from pathlib import Path 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 @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, do thing. """ data = payload['data'] channel_id = data['channel'] thread_ts = data['ts'] if reactable_string(data['text']): 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, timestamp=thread_ts ) if __name__ == '__main__': slack_token = os.environ["SLACK_API_TOKEN"] client = slack.WebClient(token=os.environ['SLACK_API_TOKEN']) rtm_client = slack.RTMClient(token=slack_token) rtm_client.start()