From 6b8064ce7ed5cfdabb4f3d9f479949aa3135a5f6 Mon Sep 17 00:00:00 2001 From: Federico Tedin Date: Mon, 24 Aug 2020 14:15:55 +0200 Subject: [PATCH 1/2] Keep latest_input_channel when triggering intents --- rasa/core/events/__init__.py | 5 ++++- rasa/core/processor.py | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/rasa/core/events/__init__.py b/rasa/core/events/__init__.py index 0e13450c12e6..7a620ecb3d29 100644 --- a/rasa/core/events/__init__.py +++ b/rasa/core/events/__init__.py @@ -346,13 +346,16 @@ def apply_to(self, tracker: "DialogueStateTracker") -> None: @staticmethod def create_external( - intent_name: Text, entity_list: Optional[List[Dict[Text, Any]]] = None + intent_name: Text, + entity_list: Optional[List[Dict[Text, Any]]] = None, + input_channel: Optional[Text] = None, ) -> "UserUttered": return UserUttered( text=f"{EXTERNAL_MESSAGE_PREFIX}{intent_name}", intent={INTENT_NAME_KEY: intent_name}, metadata={IS_EXTERNAL: True}, entities=entity_list or [], + input_channel=input_channel, ) diff --git a/rasa/core/processor.py b/rasa/core/processor.py index 1926f2c88672..ce892957a797 100644 --- a/rasa/core/processor.py +++ b/rasa/core/processor.py @@ -390,7 +390,14 @@ async def trigger_external_user_uttered( f"Invalid entity specification: {entities}. Assuming no entities." ) entity_list = [] - tracker.update(UserUttered.create_external(intent_name, entity_list)) + + # Set the new event's input channel to the latest input channel, so + # that we don't lose this property. + input_channel = tracker.get_latest_input_channel() + + tracker.update( + UserUttered.create_external(intent_name, entity_list, input_channel) + ) await self._predict_and_execute_next_action(output_channel, tracker) # save tracker state to continue conversation from this state self._save_tracker(tracker) From 10b89d2cd1ead5e69570efc0cae7b2dfee246126 Mon Sep 17 00:00:00 2001 From: Federico Tedin Date: Mon, 7 Sep 2020 23:37:48 +0200 Subject: [PATCH 2/2] Test and changelog --- changelog/6472.improvement.md | 1 + tests/core/test_processor.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 changelog/6472.improvement.md diff --git a/changelog/6472.improvement.md b/changelog/6472.improvement.md new file mode 100644 index 000000000000..f21ae5eb4c08 --- /dev/null +++ b/changelog/6472.improvement.md @@ -0,0 +1 @@ +Do not modify conversation tracker's ``latest_input_channel`` property when using ``POST /trigger_intent`` or ``ReminderScheduled``. diff --git a/tests/core/test_processor.py b/tests/core/test_processor.py index 23815baf664e..6b1094465258 100644 --- a/tests/core/test_processor.py +++ b/tests/core/test_processor.py @@ -182,6 +182,25 @@ async def test_reminder_scheduled( assert t.events[-1] == ActionExecuted("action_listen") +async def test_trigger_external_latest_input_channel( + default_channel: CollectingOutputChannel, default_processor: MessageProcessor +): + sender_id = uuid.uuid4().hex + tracker = default_processor.tracker_store.get_or_create_tracker(sender_id) + input_channel = "test_input_channel_external" + + tracker.update(UserUttered("test1")) + tracker.update(UserUttered("test2", input_channel=input_channel)) + + await default_processor.trigger_external_user_uttered( + "test3", None, tracker, default_channel + ) + + tracker = default_processor.tracker_store.retrieve(sender_id) + + assert tracker.get_latest_input_channel() == input_channel + + async def test_reminder_aborted( default_channel: CollectingOutputChannel, default_processor: MessageProcessor ):