Example #1
0
    def _run_next_action(self, action_name, message):
        # type: (Text, UserMessage) -> Dict[Text, Any]
        """Run the next action communicating with the remote core server."""

        tracker = self.core_client.tracker(message.sender_id, self.domain)
        dispatcher = Dispatcher(message.sender_id, message.output_channel,
                                self.nlg)

        action = self.domain.action_for_name(action_name)
        # events and return values are used to update
        # the tracker state after an action has been taken
        try:
            action_events = action.run(dispatcher, tracker, self.domain)
        except Exception:
            logger.exception("Encountered an exception while running action "
                             "'{}'. Bot will continue, but the actions "
                             "events are lost. Make sure to fix the "
                             "exception in your custom code."
                             "".format(action.name()))
            action_events = []

        # this is similar to what is done in the processor, but instead of
        # logging the events on the tracker we need to return them to the
        # remote core instance
        events = []
        for m in dispatcher.latest_bot_messages:
            events.append(BotUttered(text=m.text, data=m.data))

        events.extend(action_events)
        return self.core_client.continue_core(action_name, events,
                                              message.sender_id)
Example #2
0
def test_reminder_scheduled(default_processor):
    out = CollectingOutputChannel()
    sender_id = uuid.uuid4().hex

    d = Dispatcher(sender_id, out, default_processor.nlg)
    r = ReminderScheduled("utter_greet", datetime.datetime.now())
    t = default_processor.tracker_store.get_or_create_tracker(sender_id)

    t.update(UserUttered("test"))
    t.update(ActionExecuted("action_reminder_reminder"))
    t.update(r)

    default_processor.tracker_store.save(t)
    default_processor.handle_reminder(r, d)

    # retrieve the updated tracker
    t = default_processor.tracker_store.retrieve(sender_id)
    assert t.events[-4] == UserUttered(None)
    assert t.events[-3] == ActionExecuted("utter_greet")
    assert t.events[-2] == BotUttered("hey there None!", {
        'elements': None,
        'buttons': None,
        'attachment': None
    })
    assert t.events[-1] == ActionExecuted("action_listen")
Example #3
0
    def _reset(self):
        # type: () -> None
        """Reset tracker to initial state - doesn't delete events though!."""

        self._reset_slots()
        self._paused = False
        self.latest_action_name = None
        self.latest_message = UserUttered.empty()
        self.latest_bot_utterance = BotUttered.empty()
        self.followup_action = ACTION_LISTEN_NAME
Example #4
0
    def log_bot_utterances_on_tracker(tracker, dispatcher):
        # type: (DialogueStateTracker, Dispatcher) -> None

        if dispatcher.latest_bot_messages:
            for m in dispatcher.latest_bot_messages:
                bot_utterance = BotUttered(text=m.text, data=m.data)
                logger.debug("Bot utterance '{}'".format(bot_utterance))
                tracker.update(bot_utterance)

            dispatcher.latest_bot_messages = []
Example #5
0
def test_json_parse_bot():
    # DOCS MARKER BotUttered
    evt = \
        {
            'event': 'bot',
            'text': 'Hey there!',
            'data': {}
        }
    # DOCS END
    assert Event.from_parameters(evt) == BotUttered("Hey there!", {})
Example #6
0
    def _reset(self):
        # type: () -> None
        """Reset tracker to initial state - doesn't delete events though!."""

        self._reset_slots()
        self._paused = False
        self.latest_action_name = None
        self.latest_message = UserUttered.empty()
        self.latest_bot_utterance = BotUttered.empty()
        self.follow_up_action = ACTION_LISTEN_NAME
Example #7
0
    def _reset(self):
        # type: () -> None
        """Reset tracker to initial state - doesn't delete events though!."""

        self._reset_slots()
        self._paused = False
        self.latest_action_name = None
        self.latest_message = UserUttered.empty()
        self.latest_bot_utterance = BotUttered.empty()
        self.follow_up_action = None
        self._topic_stack = utils.TopicStack(self.topics, [],
                                             self.default_topic)
Example #8
0
from tests.conftest import DEFAULT_STORIES_FILE

# a couple of event instances that we can use for testing
test_events = [
    Event.from_parameters({
        "event": UserUttered.type_name,
        "text": "/goodbye",
        "parse_data": {
            "intent": {
                "confidence": 1.0,
                "name": "greet"
            },
            "entities": []
        }
    }),
    BotUttered("Welcome!", {"test": True}),
    SlotSet("cuisine", 34),
    SlotSet("cuisine", "34"),
    SlotSet("location", None),
    SlotSet("location", [34, "34", None]),
]


@pytest.fixture(scope="module")
def http_app(request, core_server):
    http_server = WSGIServer(application=core_server)
    http_server.start()

    request.addfinalizer(http_server.stop)
    return http_server.url
Example #9
0
        "name": "greet",
        "confidence": 1.0
    }, []), UserUttered("/goodbye", {
        "name": "goodbye",
        "confidence": 1.0
    }, [])),
    (TopicSet("my_topic"), TopicSet("my_other_topic")),
    (SlotSet("my_slot", "value"), SlotSet("my__other_slot", "value")),
    (Restarted(), None),
    (AllSlotsReset(), None),
    (ConversationPaused(), None),
    (ConversationResumed(), None),
    (StoryExported(), None),
    (ActionReverted(), None),
    (ActionExecuted("my_action"), ActionExecuted("my_other_action")),
    (BotUttered("my_text",
                "my_data"), BotUttered("my_other_test", "my_other_data")),
    (ReminderScheduled("my_action",
                       "now"), ReminderScheduled("my_other_action", "now")),
])
def test_event_has_proper_implementation(one_event, another_event):
    # equals tests
    assert one_event != another_event, \
        "Same events with different values need to be different"
    assert one_event == deepcopy(one_event), \
        "Event copies need to be the same"
    assert one_event != 42, \
        "Events aren't equal to 42!"

    # hash test
    assert hash(one_event) == hash(deepcopy(one_event)), \
        "Same events should have the same hash"