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/rasa/core/events/__init__.py b/rasa/core/events/__init__.py index 47d7534c09bc..ba0ab1a308b1 100644 --- a/rasa/core/events/__init__.py +++ b/rasa/core/events/__init__.py @@ -402,13 +402,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 dd4c00dd928e..20cce0046d62 100644 --- a/rasa/core/processor.py +++ b/rasa/core/processor.py @@ -387,7 +387,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) diff --git a/tests/core/test_processor.py b/tests/core/test_processor.py index 69eda13519c8..62545ed43396 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 ):