def handle_command(command, details, channel, respond = True): """ Receives commands directed at the bot and determines if they are valid commands. If so, then acts on the commands. If not, returns back what it needs for clarification. """ response = False if command == "learn": learner = Learner() response = learner.learn(details[0], " ".join(details[1:])) elif command == "unlearn": learner = Learner() content = None if len(details) > 1: content = " ".join(details[1:]) response = learner.unlearn(details[0], content) elif command == "commands": learner = Learner() response = learner.list_commands() elif command == "list": learner = Learner() response = learner.list(details[0]) elif command == "cowsay": out = subprocess.check_output(['cowsay', " ".join(details)]) response = "```" + out + "```" elif command == "meme": memer = Memer() if not details or len(details) == 0: response = memer.list_templates() else: template = details.pop(0).strip() parts = [x.strip() for x in " ".join(details).split(",")] top = parts[0] if len(parts) > 0 else None bottom = parts[1] if len(parts) > 1 else None response = memer.get_meme(template, top, bottom) elif command == "hostname": response = "slurms coming to you live from: `%s (%s)`" % (subprocess.check_output("hostname -A", shell=True).strip(), subprocess.check_output("hostname -i", shell=True).strip()) elif command == "write": writer = Writer() response = writer.get_writing(" ".join(details)) elif command == "imglearn": learner = Learner() imgur = Imgur() image_url = imgur.save_from_url(" ".join(details[1:])) response = learner.learn(details[0], image_url) elif command == "++" or command == "endorse": plusser = Plusser() reason = "" if len(details) > 1: reason = " ".join(details[1:]) response = plusser.plus(details[0], reason) elif command == "plusses": plusser = Plusser() response = plusser.get(details[0]) elif command == "leaders" or command == "leader_board": plusser = Plusser() response = plusser.leader_board() elif command == "monthly_leaders" or command == "monthly_leader_board": plusser = Plusser() months_ago = 0 if details and len(details) > 0: months_ago = details[0] response = plusser.monthly_leader_board(months_ago) elif command == "youtube": query = " ".join(details) videos = youtube.youtube_search(query) if len(videos) > 0: response = videos[-1] else: response = "sorry, couldnt find any videos for %s" % query elif command == "echo": response = " ".join(details) elif command == "pipe": pipe(command, details, channel) elif command == "doom": doom = Doom() response = doom.doom(details) else: """ see if a randomly entered command is something that was previously learned """ learner = Learner() response = learner.get(command) if response and respond: slack_client.api_call("chat.postMessage", channel=channel, text=response, as_user=True) elif not respond: return response