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
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