def message_router(variables, botname, evt, command, message): all_commands = commands try: if local_commands: all_commands += local_commands except NameError: logger('info', 'no local module commands were defined') if "type" in evt and evt["type"] == "message" and "text" in evt: # Channel/Group Invites # Slack does something along the lines of "@bot has been added to channel" # Botiana thinks is a command, but it's not really. We will process as such # just to keep things nice and clean. if "subtype" in evt.keys(): if evt["subtype"] == "channel_join": message = botname + " channel_join" logger('warn', 'channel_join event') elif evt["subtype"] == "group_join": message = botname + " group_join" logger('warn', 'group_join event') # Build variable Dict messagedetails = { 'command': command, 'message': message, 'channel': evt["channel"], 'ts': evt["ts"], 'thread_ts': evt["thread_ts"], 'caller': evt["user"] } # Command Parsing logic. This is mostly for the older modules. if command.startswith('__'): logger('warn', "ERROR: command starts with __") elif command == "set": logger('warn', "set command sent") elif command == "tot": __send_message(variables.sc, "Oh no! A Grue!", evt["channel"], "", icon_default) __send_message(variables.sc, "_" + BOT_NAME + " dies_ :skull_and_crossbones:", evt["channel"], "", icon_default) logger('crit', "Killed by human") sys.exit(0) else: if command in all_commands: # http://stackoverflow.com/a/16683842/436190 # stop the madness globals()[command](variables, messagedetails) elif command.startswith("channel_join"): __send_message(variables.sc, "Just like the Crimea! You can not keep me out, you capitalist pigs!", evt["channel"], "", icon_default) elif command.startswith("group_join"): __send_message(variables.sc, "You are all parasites and loafers that stop others from working!", evt["channel"], "", icon_default) else: try: if enable_message_processing is True: globals()[message_processing_module](variables, messagedetails) except NameError: logger('info', 'message_processing is not enabled')
def eight_ball(variables, msgdict): if msgdict["message"]: answers = [ "It is certain.", "Outlook good.", "You may rely on it.", "Ask again later.", "Concentrate and ask again.", "Reply hazy, try again.", "My reply is no.", "My sources say no.", ] __send_message(variables.sc, random.choice(answers), msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_poolball")) else: __send_message( variables.sc, "this works better when you ask the magic eight ball a question. Just say'n", msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_poolball"))
def meme(variables, msgdict): if msgdict["message"].lower() == "list": memelist(variables, msgdict) return r = r"(?P<template>\w+)?(\s+(?P<text>.+))?" template, top_text, bottom_text = "kermit", "you should provide valid input", "but that's none of my business" m = re.match(r, msgdict["message"].lower()) if m: if m.group('template'): template = m.group('template') if m.group('text'): top_text, bottom_text = str(m.group('text') + '|||').split('|', 2)[:2] template, top_text, bottom_text = tuple( s.strip().replace("-", "--").replace("_", "__").replace(" ", "-"). replace("?", "~q").replace("%", "~p").replace("#", "~h").replace( "/", "~s").replace('\xe2\x80\x99', "'").replace("''", '"').replace( "'", '%27') for s in (template, top_text, bottom_text)) __send_message( variables.sc, "https://bradme.me/{}/{}/{}.jpg".format(template, top_text, bottom_text), msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_bcat"))
def keywords(sc, evt, yamldata, count): if "channel" in evt and evt["type"] == "message" and "bot_id" not in evt: message = evt["text"].strip() for k in yamldata["keywords"]: if k in message.lower(): # Intent here is not to trigger every time. # Increment on times seen, trigger a random recount vault # when we deploy a message if count["current"] > count["random"]: # Does our entry only apply to a specific channel? print(evt["channel"]) if yamldata["keywords"][k]["watch"] == evt[ "channel"] or yamldata["keywords"][k][ "watch"] == 'all': if yamldata["keywords"][k]["type"] in "phrase": count["random"] = randint(1, 7) count["current"] = 0 __send_message( sc, random.choice( yamldata["keywords"][k]["phrases"]), evt["channel"], evt["thread_ts"], yamldata["keywords"][k]["icon"]) if yamldata["keywords"][k]["type"] == "url": count["random"] = randint(1, 7) count["current"] = 0 __send_message(sc, yamldata["keywords"][k]["url"], evt["channel"], evt["thread_ts"], yamldata["keywords"][k]["icon"]) if yamldata["keywords"][k]["type"] == "emoji": count["random"] = randint(1, 7) count["current"] = 0 __reaction(sc, evt["channel"], evt["ts"], yamldata["keywords"][k]["emoji"]) count["current"] = (count["current"] + 1) return count
def wiki(variables, msgdict): try: page = wikipedia.page(msgdict["message"]) __send_message(variables.sc, page.url, msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_wiki")) except wikipedia.exceptions.PageError: __send_message( variables.sc, msgdict["message"] + " is not a valid article. Try again", msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_wiki")) except wikipedia.exceptions.DisambiguationError as e: __send_message(variables.sc, str(e), msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_wiki")) except wikipedia.exceptions.WikipediaException: __send_message( variables.sc, "Wikipedia Error. Maybe you should have donated to them when Jimmy asked the " + "first time.", msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_wiki"))
def define(variables, msgdict, alternate_definition_index=0, ud_results_per_page=7): if msgdict["message"] in variables.yamldata["words"]: sa_def = __sa_dictionary(str(msgdict["message"]), variables.yamldata) resp = '<@{}> The Sys Admin dictionary defines `{}` as \n>>>{}'.format( msgdict["caller"], msgdict["message"], sa_def) __send_message(variables.sc, resp, msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_tux")) elif msgdict["message"]: parsed_message = msgdict["message"].split('alt:', 1) if len(parsed_message) > 1: try: alternate_definition_index = abs(int(parsed_message[1])) except ValueError: pass payload = {'term': parsed_message[0].strip()} if alternate_definition_index >= ud_results_per_page: payload['page'] = (alternate_definition_index // ud_results_per_page) + 1 alternate_definition_index %= ud_results_per_page r = requests.get("http://www.urbandictionary.com/define.php", params=payload) try: soup = BeautifulSoup(r.content, "lxml") except ValueError: soup = "failure" logger("warn", "soup failure") definitions = soup.findAll("div", attrs={"class": "meaning"}) try: resp = '<@{}> Urban Dictionary defines `{}` as ```{}```'.format( msgdict["caller"], parsed_message[0].strip(), definitions[alternate_definition_index].text) except IndexError: resp = '<@{}> Urban Dictionary doesn\'t have `{}` definitions for `{}`...'.format( msgdict["caller"], alternate_definition_index + 1, parsed_message[0].strip()) __send_message(variables.sc, resp, msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_urban_dictionary")) else: __send_message(variables.sc, "what exactly are you asking me to define?", msgdict["channel"], msgdict["thread_ts"], custom_icon("icon_urban_dictionary"))