def remove_from_group(self, u_email, group): if self.get_group_id(group): group_id = self.get_group_id(group) if self.get_user(u_email)["id"]: user_id = self.get_user(u_email)["id"] params = {"channel": group_id, "user": user_id} url = SlackAPI(self.config).channels("kick", params) urllib2.Request(url)
def get_members_raw(self, group): all_channels = self.all_groups_raw()["channels"] for channel in all_channels: if channel["name"] == group: params = {"channel": channel["id"]} url = SlackAPI(self.config).users("info", params) request = urllib2.Request(url) response = urllib2.urlopen(request) return json.load(response)
def add_to_group(self, u_email, group): if self.get_group_id(group): group_id = self.get_group_id(group) if self.get_user(u_email)["id"]: user_id = self.get_user(u_email)["id"] params = {"channel": group_id, "user": user_id} url = SlackAPI(self.config).channels("invite", params) print "sending request for {} to join {}".format( u_email, group) urllib2.Request(url) print "request sent"
def get_user(self, u_email): # although slack has a get_user call, it requires their unique id which can # only be found through the users.list method >_> url = SlackAPI(self.config).users("list") request = urllib2.Request(url) response = urllib2.urlopen(request) response = json.load(response) for user in response["members"]: if user["deleted"] is False and user["is_bot"] is False: if u_email in user["profile"]["email"]: return user
def all_users(self): # slack has a lot of inconsistencies in their json structure, can investigate # further but it seems like most are optional, the most reliable form to get # real users (not bots) is to return entries with emails url = SlackAPI(self.config).users("list") request = urllib2.Request(url) response = urllib2.urlopen(request) response = json.load(response) user_emails = [] for user in response["members"]: if user["deleted"] is False and user["is_bot"] is False: user_emails.append(user["profile"]["email"]) return user_emails
def all_groups_raw(self): url = SlackAPI(self.config).users("list") request = urllib2.Request(url) response = urllib2.urlopen(request) return json.load(response)
def get_user_with_id(self, u_id): params = {"user": u_id} url = SlackAPI(self.config).users("info", params) request = urllib2.Request(url) response = urllib2.urlopen(request) return json.load(response)
def schedule_inject(inject: Inject) -> None: slack_api = SlackAPI() slack_api.schedule_message(inject.start_description, inject.start_time) slack_api.schedule_message(inject.end_description, inject.end_time)
def __init__(self): self.slack = SlackAPI() self.db = DB() self.bot_id = None
class Robotino(object): def __init__(self): self.slack = SlackAPI() self.db = DB() self.bot_id = None def run(self): if self.slack.connect(): print("Robotino connected and running!") self.db.setup() self.bot_id = self.slack.get_bot_id() while True: command, channel = self.parse_bot_commands(self.slack.read()) try: if command: self.handle_command(command, channel) except: self.slack.post_message(channel, ":weird:") self.slack.post_message(channel, "I just crashed horribly :boom:\nTell my family I'm sorry") time.sleep(RTM_READ_DELAY) else: print("Connection failed. Exception traceback printed above.") def parse_bot_commands(self, 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: print("[user:{}][channel:{}]: {}".format(event['user'], event['channel'], event['text'])) user_id, message = self.parse_direct_mention(event["text"]) if user_id == self.bot_id: return message, event["channel"] else: self.db.save_message( text=event['text'], user=event['user'], channel=event['channel'], date=datetime.now() ) return None, None def parse_direct_mention(self, 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(self, command, channel): """ Executes bot command if the command is known """ default_response = "Not sure what you mean.\nTry:\n {}".format('\n '.join(COMMAND_HELP)) if 'pyramid' in command: self.handle_pyramid(command, channel) elif 'epic' in command: self.handle_epic_phrase(command, channel) else: self.slack.post_message(channel, default_response) def handle_pyramid(self, command, channel): command_args = command.split(' ') emoji = ":party_parrot:" for arg in command_args: if arg.startswith(":"): emoji = arg.strip() number = 5 for arg in command_args: try: number = int(arg) except: continue number = min(number, 9) # With great power comes great responsibility iterations = list(range(1, number + 1)) + list(reversed(range(1, number))) print("Creating emoji loop: pyramid {} {}".format(emoji, iterations)) for i in iterations: self.slack.post_message(channel, text=emoji * i) def handle_epic_phrase(self, command, channel): ignored_words = ['epic', 'phrase', 'frase', 'epica', 'de', 'for', 'of'] text_filter = [] for command_part in command.split(" "): if command_part.strip() and command_part not in ignored_words: text_filter.append(command_part) text_filter = " ".join(text_filter) messages = self.db.get_messages(FRASES_EPICAS, text_filter) if not messages: if not text_filter: response = "Sorry, I don't have any epic phrase at the moment." else: response = "Sorry, I don't have any epic phrase for '{}'. Try a wider search.".format(text_filter) else: random_message = choice(messages) text_parts = random_message.text.split('\n') if len(text_parts) > 1: quoted_text = "" for text in text_parts: quoted_text += "> {}\n".format(text) else: quoted_text = "> {}".format(random_message.text) response = "{}\n{}".format(quoted_text, random_message.date.strftime("%-d %b. %Y")) print(response) self.slack.post_message(channel, text=response)