Ejemplo n.º 1
0
    def execute(cls, slack_wrapper, args, timestamp, channel_id, user_id,
                user_is_admin):
        """Execute the AddAdmin command."""
        user_object = resolve_user_by_user_id(slack_wrapper, args[0])

        admin_users = handler_factory.botserver.get_config_option(
            "admin_users")

        if user_object['ok'] and admin_users:
            if user_object['user']['id'] not in admin_users:
                admin_users.append(user_object['user']['id'])

                handler_factory.botserver.set_config_option(
                    "admin_users", admin_users)

                response = "User *{}* added to the admin group.".format(
                    user_object['user']['name'])
                slack_wrapper.post_message(channel_id, response)
            else:
                response = "User *{}* is already in the admin group.".format(
                    user_object['user']['name'])
                slack_wrapper.post_message(channel_id, response)
        else:
            response = "User *{}* not found. You must provide the slack user id, not the username.".format(
                args[0])
            slack_wrapper.post_message(channel_id, response)
Ejemplo n.º 2
0
    def parse_slack_message(self, message_list):
        """
        The Slack Real Time Messaging API is an events firehose.
        Return (message, channel, ts, user) if the message is directed at the bot,
        otherwise return (None, None, None, None).
        """
        for msg in message_list:
            if msg.get("type") == "message" and "subtype" not in msg:
                if self.bot_at in msg.get("text", ""):
                    # Return text after the @ mention, whitespace removed
                    return msg['text'].split(
                        self.bot_at)[1].strip(), msg['channel'], msg[
                            'thread_ts'] if 'thread_ts' in msg else msg[
                                'ts'], msg['user']
                elif msg.get("text", "").startswith("!"):
                    # Return text after the !
                    return msg['text'][1:].strip(), msg['channel'], msg[
                        'thread_ts'] if 'thread_ts' in msg else msg['ts'], msg[
                            'user']
            # Check if user tampers with channel purpose
            elif msg.get("type") == "message" and msg[
                    "subtype"] == "channel_purpose" and msg[
                        "user"] != self.bot_id:
                source_user = get_display_name(
                    resolve_user_by_user_id(self.slack_wrapper, msg['user']))
                warning = "*User '{}' changed the channel purpose ```{}```*".format(
                    source_user, msg['text'])
                self.slack_wrapper.post_message(msg['channel'], warning)
            # Check for deletion of messages containing keywords
            elif "subtype" in msg and msg["subtype"] == "message_deleted":
                log_deletions = self.get_config_option("delete_watch_keywords")

                if log_deletions:
                    previous_msg = msg['previous_message']['text']
                    delete_keywords = log_deletions.split(",")

                    if any(keyword.strip() in previous_msg
                           for keyword in delete_keywords):
                        user_name = self.slack_wrapper.get_member(
                            msg['previous_message']['user'])
                        display_name = get_display_name(user_name)
                        self.slack_wrapper.post_message(
                            msg['channel'], "*{}* deleted : `{}`".format(
                                display_name, previous_msg))
            # Greet new users
            elif msg.get("type") == "im_created":
                self.slack_wrapper.post_message(
                    msg['user'], self.get_config_option("intro_message"))

        return None, None, None, None
    def execute(cls, slack_wrapper, args, timestamp, channel_id, user_id, user_is_admin):
        """Execute the As command."""
        dest_user = args[0].lower()
        dest_command = args[1].lower().lstrip("!")

        dest_arguments = args[2:]

        user_obj = resolve_user_by_user_id(slack_wrapper, dest_user)

        if user_obj['ok']:
            dest_user_id = user_obj['user']['id']

            # Redirecting command execution to handler factory
            handler_factory.process_command(slack_wrapper, dest_command,
                                            [dest_command] + dest_arguments, timestamp, channel_id, dest_user_id, user_is_admin)
        else:
            raise InvalidCommand("You have to specify a valid user (use @-notation).")