async def test_action_utter_default_retrieved_response(default_channel, default_nlg, default_tracker, default_domain): from rasa.core.channels.channel import UserMessage action_name = "respond_chitchat" default_tracker.latest_message = UserMessage( "Who are you?", parse_data={ "response_selector": { "default": { "response": { "name": "I am a bot." } } } }, ) events = await ActionRetrieveResponse(action_name).run( default_channel, default_nlg, default_tracker, default_domain) assert events[0].as_dict().get("text") == BotUttered( "I am a bot.").as_dict().get("text")
async def test_action_default_ask_affirmation(default_channel, default_nlg, default_tracker, default_domain): events = await ActionDefaultAskAffirmation().run(default_channel, default_nlg, default_tracker, default_domain) assert events == [ BotUttered( "Did you mean 'None'?", { "buttons": [ { "title": "Yes", "payload": "/None" }, { "title": "No", "payload": "/out_of_scope" }, ] }, ) ]
async def test_action_utter_template_with_buttons(default_channel, template_nlg, template_sender_tracker, default_domain): events = await ActionUtterTemplate("utter_buttons").run( default_channel, template_nlg, template_sender_tracker, default_domain) assert events == [ BotUttered( "button message", { "buttons": [ { "payload": "button1", "title": "button1" }, { "payload": "button2", "title": "button2" }, ] }, ) ]
from tests.nlu.utilities import ResponseTest # 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 def rasa_app_without_api(rasa_server_without_api: Sanic) -> SanicTestClient: return rasa_server_without_api.test_client @pytest.fixture def rasa_app(rasa_server: Sanic) -> SanicTestClient: return rasa_server.test_client
async def test_remote_action_logs_events(default_channel, default_nlg, default_tracker, default_domain): endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) response = { "events": [{ "event": "slot", "value": "rasa", "name": "name" }], "responses": [ { "text": "test text", "template": None, "buttons": [{ "title": "cheap", "payload": "cheap" }], }, { "template": "utter_greet" }, ], } with aioresponses() as mocked: mocked.post("https://example.com/webhooks/actions", payload=response) events = await remote_action.run(default_channel, default_nlg, default_tracker, default_domain) r = latest_request(mocked, "post", "https://example.com/webhooks/actions") assert r assert json_of_latest_request(r) == { "domain": default_domain.as_dict(), "next_action": "my_action", "sender_id": "my-sender", "version": rasa.__version__, "tracker": { "latest_message": { "entities": [], "intent": {}, "text": None, "message_id": None, "metadata": {}, }, "active_form": {}, "latest_action_name": None, "sender_id": "my-sender", "paused": False, "followup_action": "action_listen", "latest_event_time": None, "slots": { "name": None }, "events": [], "latest_input_channel": None, }, } assert len(events) == 3 # first two events are bot utterances assert events[0] == BotUttered( "test text", {"buttons": [{ "title": "cheap", "payload": "cheap" }]}) assert events[1] == BotUttered("hey there None!", metadata={"template_name": "utter_greet"}) assert events[2] == SlotSet("name", "rasa")
def _two_stage_clarification_request() -> List[Event]: return [ActionExecuted(ACTION_TWO_STAGE_FALLBACK_NAME), BotUttered("please affirm")]
async def test_handle_message_with_session_start( default_channel: CollectingOutputChannel, default_processor: MessageProcessor, monkeypatch: MonkeyPatch, ): sender_id = uuid.uuid4().hex entity = "name" slot_1 = {entity: "Core"} await default_processor.handle_message( UserMessage(f"/greet{json.dumps(slot_1)}", default_channel, sender_id) ) assert default_channel.latest_output() == { "recipient_id": sender_id, "text": "hey there Core!", } # patch processor so a session start is triggered monkeypatch.setattr(default_processor, "_has_session_expired", lambda _: True) slot_2 = {entity: "post-session start hello"} # handle a new message await default_processor.handle_message( UserMessage(f"/greet{json.dumps(slot_2)}", default_channel, sender_id) ) tracker = default_processor.tracker_store.get_or_create_tracker(sender_id) # make sure the sequence of events is as expected assert list(tracker.events) == [ ActionExecuted(ACTION_SESSION_START_NAME), SessionStarted(), ActionExecuted(ACTION_LISTEN_NAME), UserUttered( f"/greet{json.dumps(slot_1)}", {INTENT_NAME_KEY: "greet", "confidence": 1.0}, [{"entity": entity, "start": 6, "end": 22, "value": "Core"}], ), SlotSet(entity, slot_1[entity]), ActionExecuted("utter_greet"), BotUttered("hey there Core!", metadata={"template_name": "utter_greet"}), ActionExecuted(ACTION_LISTEN_NAME), ActionExecuted(ACTION_SESSION_START_NAME), SessionStarted(), # the initial SlotSet is reapplied after the SessionStarted sequence SlotSet(entity, slot_1[entity]), ActionExecuted(ACTION_LISTEN_NAME), UserUttered( f"/greet{json.dumps(slot_2)}", {INTENT_NAME_KEY: "greet", "confidence": 1.0}, [ { "entity": entity, "start": 6, "end": 42, "value": "post-session start hello", } ], ), SlotSet(entity, slot_2[entity]), ActionExecuted(ACTION_LISTEN_NAME), ]
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!", {})
"name": "goodbye", "confidence": 1.0 }, []), ), (SlotSet("my_slot", "value"), SlotSet("my__other_slot", "value")), (Restarted(), None), (AllSlotsReset(), None), (ConversationPaused(), None), (ConversationResumed(), None), (StoryExported(), None), (ActionReverted(), None), (UserUtteranceReverted(), None), (ActionExecuted("my_action"), ActionExecuted("my_other_action")), (FollowupAction("my_action"), FollowupAction("my_other_action")), ( BotUttered("my_text", {"my_data": 1}), BotUttered("my_other_test", {"my_other_data": 1}), ), ( AgentUttered("my_text", "my_data"), AgentUttered("my_other_test", "my_other_data"), ), ( ReminderScheduled("my_action", datetime.now()), ReminderScheduled("my_other_action", datetime.now()), ), ], ) def test_event_has_proper_implementation(one_event, another_event): # equals tests assert (one_event != another_event
user_uttered("greet"), ActionExecuted("loop"), ActiveLoop("loop"), SlotSet(REQUESTED_SLOT, "bla"), SlotSet("slot", "value"), ActiveLoop(None), SlotSet(REQUESTED_SLOT, None), ], ), ( [ user_uttered("trigger form"), ActionExecuted("form"), ActiveLoop("form"), SlotSet(REQUESTED_SLOT, "some slot"), BotUttered("ask slot"), ActionExecuted(ACTION_LISTEN_NAME), user_uttered("fill requested slots"), SlotSet("some slot", "value"), ActionExecuted("form"), SlotSet("some slot", "value"), SlotSet(REQUESTED_SLOT, None), ActiveLoop(None), ], [ user_uttered("trigger form"), ActionExecuted("form"), ActiveLoop("form"), SlotSet(REQUESTED_SLOT, "some slot"), BotUttered("ask slot"), SlotSet("some slot", "value"),
"name": "goodbye", "confidence": 1.0 }, []), ), (SlotSet("my_slot", "value"), SlotSet("my__other_slot", "value")), (Restarted(), None), (AllSlotsReset(), None), (ConversationPaused(), None), (ConversationResumed(), None), (StoryExported(), None), (ActionReverted(), None), (UserUtteranceReverted(), None), (ActionExecuted("my_action"), ActionExecuted("my_other_action")), (FollowupAction("my_action"), FollowupAction("my_other_action")), ( BotUttered("my_text", "my_data"), BotUttered("my_other_test", "my_other_data"), ), ( AgentUttered("my_text", "my_data"), AgentUttered("my_other_test", "my_other_data"), ), ( ReminderScheduled("my_action", datetime.now()), ReminderScheduled("my_other_action", datetime.now()), ), ], ) def test_event_has_proper_implementation(one_event, another_event): # equals tests assert (one_event != another_event
async def test_action_utter_template(default_channel, default_nlg, default_tracker, default_domain): events = await ActionUtterTemplate("utter_channel").run( default_channel, default_nlg, default_tracker, default_domain) assert events == [BotUttered("this is a default channel")]