Exemplo n.º 1
0
    def handle_message(self, msg):
        if not self.check_message(msg):
            return False

        # if not direct message, check if it starts with a mention to the bot name
        text = msg['text']
        if not is_direct_channel(msg['channel']):
            # Mentions me?
            has_mention, text = has_initial_mentioning(text, self.rtm.user_id,
                                                       self.rtm.find_user(self.rtm.user_id).name)
            if not has_mention:
                return False

        # process the text
        pyjokes_regx = EP_Regex.get_pattern(self._used_regexes[0])
        regx         = re.compile(pyjokes_regx, re.IGNORECASE)
        match        = regx.search(text.strip())
        if not match:
            return False

        # prepare the answer
        message = get_the_joke(match)

        # send the message
        if is_direct_channel(msg['channel']):
            # get who is talking to me, aka, user
            user = self.rtm.find_user(msg['user'])

            yield from user.send_message(message)
            return True
        else:
            reply = {'text': '<@{}> {}'.format(msg['user'], message),
                     'type': 'message', 'channel': msg['channel']}
            yield from self.rtm.send_event(reply)
            return True
    def handle_message(self, msg):
        if not self.check_message(msg):
            return False

        text = msg['text']

        # if not direct message, check if it starts with a mention to the bot name
        if not is_direct_channel(msg['channel']):
            # Mentions me?
            has_mention, text = has_initial_mentioning(text, self.rtm.user_id,
                                                       self.rtm.find_user(self.rtm.user_id).name)
            if not has_mention:
                return False

        # process the text
        if text.strip().lower() != 'help':
            return False

        # prepare the reply
        user         = self.rtm.find_user(msg['user'])
        channel_name = None
        if not is_direct_channel(msg['channel']):
            channel_name = self.rtm.find_channel(msg['channel']).name

        reply_text = do_help_implant(self.bot, user.name, channel_name=channel_name)

        # send the reply
        yield from self.bot.send_message(reply_text, msg['channel'], msg['user'], mkdown=True)
        return True
Exemplo n.º 3
0
    def handle_message(self, msg):
        if not self.check_message(msg):
            return False

        # if not direct message, check if it starts with a mention to the bot name
        text    = msg['text']
        if not is_direct_channel(msg['channel']):
            # Mentions me?
            has_mention, text = has_initial_mentioning(text, self.rtm.user_id,
                                                       self.rtm.find_user(self.rtm.user_id).name)
            if not has_mention:
                return False

        # process the text
        if text.strip().lower() != 'help':
            return False

        # prepare the reply
        user         = self.rtm.find_user(msg['user'])
        channel_name = None
        if not is_direct_channel(msg['channel']):
            channel_name = self.rtm.find_channel(msg['channel']).name

        reply_text = do_help_implant(self.bot, user.name, channel_name=channel_name)

        # send the reply
        if is_direct_channel(msg['channel']):
            yield from user.send_message(reply_text, mkdown=True)
            return True
        else:
            reply = {'type': 'message', 'channel': msg['channel'], 'mkdown': True}
            reply['text'] = '<@{}> {}'.format(msg['user'], reply_text)
            yield from self.rtm.send_event(reply)
            return True
    def handle_message(self, msg):
        if not self.check_message(msg):
            return False

        # cleanup the message text
        text = cleanup_message_text(msg['text'])

        # if not direct message, check if it starts with a mention to the bot name
        if not is_direct_channel(msg['channel']):
            # Mentions me?
            has_mention, text = has_initial_mentioning(text, self.rtm.user_id,
                                                       self.rtm.find_user(self.rtm.user_id).name)
            if not has_mention:
                return False

        # process the text
        pyjokes_regx = self._used_regexes['ask_for_joke']
        regx         = re.compile(pyjokes_regx, re.IGNORECASE)
        match        = regx.search(text.strip())
        if not match:
            return False

        # prepare the answer
        message = get_the_joke(match)
        if message:
            yield from self.bot.send_message(message, msg['channel'], msg['user'], mkdown=True)
            return True
        else:
            return False
Exemplo n.º 5
0
    def handle_message(self, msg):
        log.debug('Starting {}'.format(type(self).__name__))

        if not self.check_message(msg):
            return False

        # if not direct message, check if it starts with a mention to the bot name
        text = msg['text'].strip()
        if not is_direct_channel(msg['channel']):
            # Mentions me?
            has_mention, text = has_initial_mentioning(text, self.rtm.user_id,
                                                       self.rtm.find_user(self.rtm.user_id).name)
            if not has_mention:
                return False

        # get who is talking to me, aka, user
        user = self.rtm.find_user(msg['user'])

        # get the state of the user
        user_state = self.user_state(user.name)

        # process the text
        current_state = user_state.current
        event, match  = self.match_event(user_state, text)
        if event is None:
            return False

        try:
            user_state.trigger(event, msg=text, user=user, implant=self, match=match)
            reply_text = self.pop_reply_text(user.name)
        except Exception as exc:
            log.debug('Error triggering event `{}` in implant `{}` with message `{}`, '
                      'where user state was `{}`. Exception given: {}'.format(event, type(self).__name__,
                                                                              msg, current_state, str(exc)))
            return False
        else:
            if reply_text is None:
                return False

            if is_direct_channel(msg['channel']):
                yield from user.send_message(reply_text, mkdown=True)
                return True
            else:
                reply         = {'type': 'message', 'channel': msg['channel'], 'mkdown': True}
                reply['text'] = '<@{}> {}'.format(msg['user'], reply_text)
                yield from self.rtm.send_event(reply)
                return True
    def handle_message(self, msg):
        log.debug('Starting {}'.format(type(self).__name__))

        # check if this message is worth checking
        if not self.check_message(msg):
            return False

        # here follows the standard part of the process
        # cleanup the message text
        text = cleanup_message_text(msg['text'])

        # if not direct message, check if it starts with a mention to the bot name
        if not is_direct_channel(msg['channel']):
            # Mentions me?
            has_mention, text = has_initial_mentioning(text, self.rtm.user_id,
                                                       self.rtm.find_user(self.rtm.user_id).name)
            if not has_mention:
                return False

        # get who is talking to me, aka, user
        user = self.rtm.find_user(msg['user'])

        # get the state of the user
        user_state = self.user_state(user.name)
        current_state = user_state.current

        # process the text
        event, match  = self.match_event(user_state, text)
        if event is None:
            return False

        # trigger the state machine
        try:
            user_state.trigger(event, msg=text, user=user, implant=self, match=match)
        except Exception as exc:
            log.debug('Error triggering event `{}` in implant `{}` with message `{}`, '
                      'where user state was `{}`. Exception given: {}'.format(event, type(self).__name__,
                                                                              msg, current_state, str(exc)))
            return False
        else:
            # reply the user if there is any message for him
            reply_text = self.pop_reply_text(user.name)
            if reply_text:
                yield from self.bot.send_message(reply_text, msg['channel'], msg.get('user', None), mkdown=True)
                return True