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 match_event(self, state, msg): event = None match = None if not state.isstate('idle') and msg == 'cancel': return 'cancel', None if state.isstate('idle'): tran_rgx = re.compile(EP_Regex.get_pattern(self._used_regexes[0])) vote_rgx = re.compile(EP_Regex.get_pattern(self._used_regexes[1])) # '(?P<action>\bcreate \b|\bclose \b|\bshow (?:\bresults of \b)?\b)' \ match = tran_rgx.search(msg) if match: action = get_group_from_match(match, 'action', '').strip() object = get_group_from_match(match, 'object', '').strip() poll_code = get_group_from_match(match, 'poll_code', '').strip() if action.startswith('show') and (action.endswith('result') or action.endswith('results')): if 'poll' in object or 'voting' in object: return 'request_results', match elif action.startswith('show'): if 'poll' in object or 'voting' in object: if 'open' in object: return 'request_open_polls', match else: return 'request_all_polls', match elif action.startswith('create'): if 'poll' in object or 'voting' in object: return 'open_poll', match elif action.startswith('close'): if 'poll' in object or 'voting' in object: if poll_code: return 'close_poll', match # match a vote value match = vote_rgx.match(msg) if match: action = get_group_from_match(match, 'action', '') if action.startswith('vote'): return 'vote_poll', match elif state.isstate('ask_topic'): if msg: return 'receive_topic', None else: return 'topic_error', None elif state.isstate('ask_code'): code_rgx = re.compile(EP_Regex.get_pattern(self._used_regexes[2])) match = code_rgx.match(msg) if match: return 'receive_code', match else: return 'code_error', None elif state.isstate('ask_hours'): match = re.compile(r"(?P<hours>[0-9]{1,3})").match(msg) if match: return 'receive_hours', match else: return 'hours_error', None elif state.isstate('ask_open_confirm'): if msg == 'yes' or msg == 'no': return 'confirm_open', None else: return 'confirm_open_error', None elif state.isstate('ask_close_confirm'): if msg == 'yes' or msg == 'no': return 'confirm_close', None else: return 'confirm_close_error', None else: event = None match = None return event, match