def consume_user_message(self, msg): regex_KEYWORD = re.compile('KEYWORD') log.debug("Consumer user message %s" % (msg,)) if msg['timestamp']: timestamp = time_to_vusion_format(msg['timestamp']) self.unmatchable_reply_collection.save( {'participant-phone': msg['from_addr'], 'to': msg['to_addr'], 'message-content': msg['content'], 'timestamp': timestamp, }) code = self.shortcodes_collection.find_one({ 'shortcode': msg['to_addr']}) if code is None: return template = self.templates_collection.find_one({ '_id': ObjectId(code['error-template'])}) if template is None: return error_message = TransportUserMessage(**{ 'from_addr': msg['to_addr'], 'to_addr': msg['from_addr'], 'transport_name': msg['transport_name'], 'transport_type': msg['transport_type'], 'transport_metadata': msg['transport_metadata'], 'content': re.sub( regex_KEYWORD, get_first_word(msg['content']), template['template'] ) }) yield self.transport_publisher.publish_message(error_message) log.debug("Reply '%s' sent to %s" % (error_message['content'], error_message['to_addr']))
def dispatch_inbound_message(self, msg): keyword = get_first_word(msg['content']).lower() matched = False for rule in self.rules: if self.is_msg_matching_routing_rules(keyword, msg, rule): matched = True self.publish_exposed_inbound(rule['app'], msg) if not matched: if self.fallback_application is not None: self.publish_exposed_inbound(self.fallback_application, msg) else: log.error('Message could not be routed: %r' % (msg, ))
def dispatch_inbound_message(self, msg): keyword = get_first_word(msg['content']).lower() matched = False for rule in self.rules: if self.is_msg_matching_routing_rules(keyword, msg, rule): matched = True self.publish_exposed_inbound(rule['app'], msg) if not matched: if self.fallback_application is not None: self.publish_exposed_inbound(self.fallback_application, msg) else: log.error('Message could not be routed: %r' % (msg,))
def dispatch_inbound_message(self, msg): keyword = get_first_word(msg['content']).lower() matched = False for rule in self.rules: if self.is_msg_matching_routing_rules(keyword, msg, rule): matched = True # copy message so that the middleware doesn't see a particular # message instance multiple times self.publish_exposed_inbound(rule['app'], msg.copy()) if not matched: if self.fallback_application is not None: self.publish_exposed_inbound(self.fallback_application, msg) else: log.error(DispatcherError( 'Message could not be routed: %r' % (msg,)))
def dispatch_inbound_message(self, msg): keyword = get_first_word(msg['content']).lower() matched = False for rule in self.rules: if self.is_msg_matching_routing_rules(keyword, msg, rule): matched = True # copy message so that the middleware doesn't see a particular # message instance multiple times self.publish_exposed_inbound(rule['app'], msg.copy()) if not matched: if self.fallback_application is not None: self.publish_exposed_inbound(self.fallback_application, msg) else: log.error( DispatcherError('Message could not be routed: %r' % (msg, )))
def get_matching_reference_and_actions(self, message, actions): reference_metadata = {} keyword = get_first_word(message).lower() reply = self.get_reply(message).lower() dialogue_id, interaction = self.get_matching_interaction(keyword) if not interaction: return reference_metadata, actions reference_metadata = { 'dialogue-id': dialogue_id, 'interaction-id': interaction['interaction-id']} if 'answers' in interaction: answer = self.get_matching_answer(interaction['answers'], reply) if not answer or answer is None: reference_metadata['matching-answer'] = None actions.append({ 'type-action': 'unmatching-answer', 'answer': reply}) else: reference_metadata['matching-answer'] = answer['choice'] actions = self.add_feedback_action(actions, answer) if 'label-for-participant-profiling' in interaction: actions.append( {'type-action': 'profiling', 'label': interaction['label-for-participant-profiling'], 'value': answer['choice']}) if 'answer-actions' in answer: for answer_action in answer['answer-actions']: action = answer_action action['type-action'] = answer_action['type-answer-action'] actions.append(action) else: actions = self.add_feedback_action(actions, interaction) if 'answer-label' in interaction: actions.append( {'type-action': 'profiling', 'label': interaction['answer-label'], 'value': self.get_open_answer(message)}) return reference_metadata, actions
def test_get_first_word(self): self.assertEqual('KEYWORD', get_first_word('KEYWORD rest of the message')) self.assertEqual('', get_first_word('')) self.assertEqual('', get_first_word(None))