Exemplo n.º 1
0
 def consume_user_message(self, message):
     self.log("User message received from %s '%s'" %
              (message['from_addr'], message['content']))
     try:
         ref = None
         actions = []
         active_dialogues = self.get_active_dialogues()
         for dialogue in active_dialogues:
             scriptHelper = Dialogue(dialogue['Dialogue'])
             ref, actions = scriptHelper.get_matching_reference_and_actions(
                 message['content'], actions)
             if ref:
                 break
         actions = self.get_matching_request_actions(
             message['content'], actions)
         self.save_history(message_content=message['content'],
                           participant_phone=message['from_addr'],
                           message_type='received',
                           reference_metadata=ref)
         self.log("actions %s reference %s" % (actions, ref))
         for action in actions:
             self.run_action(message['from_addr'], action)
     except:
         exc_type, exc_value, exc_traceback = sys.exc_info()
         self.log(
             "Error during consume user message: %r" %
             traceback.format_exception(exc_type, exc_value, exc_traceback))
Exemplo n.º 2
0
 def consume_user_message(self, message):
     self.log("User message received from %s '%s'" % (message['from_addr'],
                                                      message['content']))
     try:
         ref = None
         actions = []
         active_dialogues = self.get_active_dialogues()
         for dialogue in active_dialogues:
             scriptHelper = Dialogue(dialogue['Dialogue'])
             ref, actions = scriptHelper.get_matching_reference_and_actions(
                 message['content'], actions)
             if ref:
                 break
         actions = self.get_matching_request_actions(
             message['content'],
             actions)
         self.save_history(
             message_content=message['content'],
             participant_phone=message['from_addr'],
             message_type='received',
             reference_metadata=ref)
         self.log("actions %s reference %s" % (actions, ref))
         for action in actions:
             self.run_action(message['from_addr'], action)
     except:
         exc_type, exc_value, exc_traceback = sys.exc_info()
         self.log(
             "Error during consume user message: %r" %
             traceback.format_exception(exc_type, exc_value, exc_traceback))
Exemplo n.º 3
0
 def register_keywords_in_dispatcher(self):
     self.log('Synchronizing with dispatcher')
     keywords = []
     for dialogue in self.get_active_dialogues():
         keywords += Dialogue(dialogue['Dialogue']).get_all_keywords()
     for request in self.collections['requests'].find():
         keyphrases = request['keyword'].split(', ')
         for keyphrase in keyphrases:
             if not (keyphrase.split(' ')[0]) in keywords:
                 keywords.append(keyphrase.split(' ')[0])
     rules = []
     for keyword in keywords:
         rules.append({
             'app': self.transport_name,
             'keyword': keyword,
             'to_addr': self.properties['shortcode']
         })
     msg = DispatcherControl(action='add_exposed',
                             exposed_name=self.transport_name,
                             rules=rules)
     yield self.dispatcher_publisher.publish_message(msg)
Exemplo n.º 4
0
    def test_get_matchin_question_answer(self):
        script = Dialogue(self.question_answer)

        ref, actions = script.get_matching_reference_and_actions("feel 1", [])
        self.assertEqual(ref, {'dialogue-id': '01',
                               'interaction-id': '01-01',
                               'matching-answer': 'Fine'})
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0], {'type-action': 'feedback',
                                      'content': 'thank you'})
        self.assertEqual(actions[1], {'type-action': 'feedback',
                                      'content': 'thank you again'})

        ref, actions = script.get_matching_reference_and_actions("feel 0", [])
        self.assertEqual(ref, {'dialogue-id': '01',
                               'interaction-id': '01-01',
                               'matching-answer': None})
        self.assertEqual(len(actions), 1)
        self.assertEqual(actions[0],{'type-action': 'unmatching-answer',
                                     'answer': '0'})

        ref, actions = script.get_matching_reference_and_actions("feel 3", [])
        self.assertEqual(ref, {'dialogue-id': '01',
                               'interaction-id': '01-01',
                               'matching-answer': None})
        self.assertEqual(len(actions), 1)

        ref, actions = script.get_matching_reference_and_actions("feel ok", [])
        self.assertEqual(ref, {'dialogue-id': '01',
                               'interaction-id': '01-01',
                               'matching-answer': 'Ok'})
        self.assertEqual(len(actions), 0)

        ref, actions = script.get_matching_reference_and_actions("fel ok", [])
        self.assertEqual(ref, {'dialogue-id': '01',
                               'interaction-id': '01-01',
                               'matching-answer': 'Ok'})
        self.assertEqual(len(actions), 0)

        ref, actions = script.get_matching_reference_and_actions("name john doe", [])
        self.assertEqual(ref, {'dialogue-id': '01',
                               'interaction-id': '01-02'})
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0], {'type-action': 'feedback',
                                      'content': 'thank you for this answer'})
        self.assertEqual(actions[1], {'type-action': 'profiling',
                                      'label': 'name',
                                      'value': 'john doe'})

        ref, actions = script.get_matching_reference_and_actions("name", [])
        self.assertEqual(ref, {'dialogue-id': '01',
                               'interaction-id': '01-02'})
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0], {'type-action': 'feedback',
                                      'content': 'thank you for this answer'})
        self.assertEqual(actions[1], {'type-action': 'profiling',
                                      'label': 'name',
                                      'value': ''})

        script = Dialogue(self.other_question_answer)
        ref, actions = script.get_matching_reference_and_actions("something good", [])

        self.assertEqual(ref, {})
        self.assertEqual(actions, [])

        ref, actions = script.get_matching_reference_and_actions('Gen Male', [])
        self.assertEqual(ref, {'dialogue-id': 'script.dialogues[0]',
                               'interaction-id': 'script.dialogues[0].interactions[2]',
                               'matching-answer': 'Male'})
        self.assertEqual(len(actions), 1)
        self.assertEqual(actions[0], {'type-action': 'profiling',
                                      'label': 'gender',
                                      'value': 'Male'})
Exemplo n.º 5
0
    def test_get_all_keywords(self):
        script = Dialogue(self.question_answer)

        self.assertTrue(script.get_all_keywords(),
                        ['feel', 'fel'])
Exemplo n.º 6
0
    def test_get_all_keywords(self):
        script = Dialogue(self.question_answer)

        self.assertTrue(script.get_all_keywords(), ['feel', 'fel'])
Exemplo n.º 7
0
    def test_get_matchin_question_answer(self):
        script = Dialogue(self.question_answer)

        ref, actions = script.get_matching_reference_and_actions("feel 1", [])
        self.assertEqual(
            ref, {
                'dialogue-id': '01',
                'interaction-id': '01-01',
                'matching-answer': 'Fine'
            })
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0], {
            'type-action': 'feedback',
            'content': 'thank you'
        })
        self.assertEqual(actions[1], {
            'type-action': 'feedback',
            'content': 'thank you again'
        })

        ref, actions = script.get_matching_reference_and_actions("feel 0", [])
        self.assertEqual(
            ref, {
                'dialogue-id': '01',
                'interaction-id': '01-01',
                'matching-answer': None
            })
        self.assertEqual(len(actions), 1)
        self.assertEqual(actions[0], {
            'type-action': 'unmatching-answer',
            'answer': '0'
        })

        ref, actions = script.get_matching_reference_and_actions("feel 3", [])
        self.assertEqual(
            ref, {
                'dialogue-id': '01',
                'interaction-id': '01-01',
                'matching-answer': None
            })
        self.assertEqual(len(actions), 1)

        ref, actions = script.get_matching_reference_and_actions("feel ok", [])
        self.assertEqual(
            ref, {
                'dialogue-id': '01',
                'interaction-id': '01-01',
                'matching-answer': 'Ok'
            })
        self.assertEqual(len(actions), 0)

        ref, actions = script.get_matching_reference_and_actions("fel ok", [])
        self.assertEqual(
            ref, {
                'dialogue-id': '01',
                'interaction-id': '01-01',
                'matching-answer': 'Ok'
            })
        self.assertEqual(len(actions), 0)

        ref, actions = script.get_matching_reference_and_actions(
            "name john doe", [])
        self.assertEqual(ref, {'dialogue-id': '01', 'interaction-id': '01-02'})
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0], {
            'type-action': 'feedback',
            'content': 'thank you for this answer'
        })
        self.assertEqual(actions[1], {
            'type-action': 'profiling',
            'label': 'name',
            'value': 'john doe'
        })

        ref, actions = script.get_matching_reference_and_actions("name", [])
        self.assertEqual(ref, {'dialogue-id': '01', 'interaction-id': '01-02'})
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0], {
            'type-action': 'feedback',
            'content': 'thank you for this answer'
        })
        self.assertEqual(actions[1], {
            'type-action': 'profiling',
            'label': 'name',
            'value': ''
        })

        script = Dialogue(self.other_question_answer)
        ref, actions = script.get_matching_reference_and_actions(
            "something good", [])

        self.assertEqual(ref, {})
        self.assertEqual(actions, [])

        ref, actions = script.get_matching_reference_and_actions(
            'Gen Male', [])
        self.assertEqual(
            ref, {
                'dialogue-id': 'script.dialogues[0]',
                'interaction-id': 'script.dialogues[0].interactions[2]',
                'matching-answer': 'Male'
            })
        self.assertEqual(len(actions), 1)
        self.assertEqual(actions[0], {
            'type-action': 'profiling',
            'label': 'gender',
            'value': 'Male'
        })