Custom ActionSessionStart returns twice same message

Hi community,

I am using Rasa 2.4 and I tried to customize the ActionSessionStart class just to send a specific user message when triggering /session_start following the below code:

class ActionSessionStart(Action):
    def name(self) -> Text:
        return "action_session_start"

    @staticmethod
    def fetch_slots(tracker: Tracker) -> List[EventType]:
        """Collect slots that contain the user's name and phone number."""

        slots = []
        for key in ("name", "phone_number"):
            value = tracker.get_slot(key)
            if value is not None:
                slots.append(SlotSet(key=key, value=value))
        return slots

    async def run(
      self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]
    ) -> List[Dict[Text, Any]]:

        # the session should begin with a `session_started` event
        events = [SessionStarted()]

        dispatcher.utter_message(text="Bonjour Matthieu, bienvenu dans cette nouvelle session en ma compagnie !")

        # an `action_listen` should be added at the end as a user message follows
        events.append(ActionExecuted("action_listen"))

        return events

However, when launching rasa shell and initiating with /session_start I got twice the user message: Bonjour Matthieu, bienvenu dans cette nouvelle session en ma compagnie !

Anyone would have an idea why ?

Thanks!

Hi,

Please find below the detailed response following /session_start obtained with the --debug flag:

Your input ->  /session_start                                                                                                                                                                                                                                                      
2021-03-30 14:50:07 DEBUG    rasa.core.lock_store  - Issuing ticket for conversation '630849e4545949c0b480cf329024f5f9'.
2021-03-30 14:50:07 DEBUG    rasa.core.lock_store  - Acquiring lock for conversation '630849e4545949c0b480cf329024f5f9'.
2021-03-30 14:50:07 DEBUG    rasa.core.lock_store  - Acquired lock for conversation '630849e4545949c0b480cf329024f5f9'.
2021-03-30 14:50:07 DEBUG    rasa.core.tracker_store  - Could not find tracker for conversation ID '630849e4545949c0b480cf329024f5f9'.
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Starting a new session for conversation ID '630849e4545949c0b480cf329024f5f9'.
2021-03-30 14:50:07 DEBUG    rasa.core.actions.action  - Calling action endpoint to run action 'action_session_start'.
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Policy prediction ended with events '[]'.
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Action 'action_session_start' ended with events '[BotUttered('Bonjour Matthieu, bienvenu dans cette nouvelle session en ma compagnie !', {"elements": null, "quick_replies": null, "buttons": null, "attachment": null, "image": null, "custom": null}, {"utter_action": "utter_session_start", "name": "Matthieu"}, 1617108607.4718068), <rasa.shared.core.events.SessionStarted object at 0x7f46b06b9730>, ActionExecuted(action: action_listen, policy: None, confidence: None)]'.
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Current slot values: 
	name: None
	location: None
	email: None
	session_ready: None
	requested_slot: None
	session_started_metadata: None
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Received user message '/session_start' with intent '{'name': 'session_start', 'confidence': 1.0}' and entities '[]'
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Logged UserUtterance - tracker now has 5 events.
2021-03-30 14:50:07 DEBUG    rasa.core.policies.memoization  - Current tracker state:
[state 1] user intent: session_start | previous action name: action_listen
2021-03-30 14:50:07 DEBUG    rasa.core.policies.memoization  - There is no memorised next action
2021-03-30 14:50:07 DEBUG    rasa.core.policies.rule_policy  - Current tracker state:
[state 1] user text: /session_start | previous action name: action_listen
2021-03-30 14:50:07 DEBUG    rasa.core.policies.rule_policy  - There is no applicable rule.
2021-03-30 14:50:07 DEBUG    rasa.core.policies.rule_policy  - Predicted default action 'action_session_start'.
2021-03-30 14:50:07 DEBUG    rasa.core.policies.ensemble  - Made prediction using user intent.
2021-03-30 14:50:07 DEBUG    rasa.core.policies.ensemble  - Added `DefinePrevUserUtteredFeaturization(False)` event.
2021-03-30 14:50:07 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_RulePolicy.
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Predicted next action 'action_session_start' with confidence 1.00.
2021-03-30 14:50:07 DEBUG    rasa.core.actions.action  - Calling action endpoint to run action 'action_session_start'.
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Policy prediction ended with events '[<rasa.shared.core.events.DefinePrevUserUtteredFeaturization object at 0x7f46b072c8b0>]'.
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Action 'action_session_start' ended with events '[BotUttered('Bonjour Matthieu, bienvenu dans cette nouvelle session en ma compagnie !', {"elements": null, "quick_replies": null, "buttons": null, "attachment": null, "image": null, "custom": null}, {"utter_action": "utter_session_start", "name": "Matthieu"}, 1617108607.479607), <rasa.shared.core.events.SessionStarted object at 0x7f46b06c1bb0>, ActionExecuted(action: action_listen, policy: None, confidence: None)]'.
2021-03-30 14:50:07 DEBUG    rasa.core.processor  - Current slot values: 
	name: None
	location: None
	email: None
	session_ready: None
	requested_slot: None
	session_started_metadata: None
2021-03-30 14:50:07 DEBUG    rasa.core.lock_store  - Deleted lock for conversation '630849e4545949c0b480cf329024f5f9'.
Bonjour Matthieu, bienvenu dans cette nouvelle session en ma compagnie !
Bonjour Matthieu, bienvenu dans cette nouvelle session en ma compagnie !

Would anyone have an idea of the problem?

Hi @mattvan83, I tried to recreate the same scenario as you have done & I too have received the same message twice, after I debugged the issue, I got to know that the action_session_start has been triggered twice for the first time, it’s because, for the initially when you start the server and type the /session_start, it tries to call the action_session_start as shown below

021-03-31 11:04:47 DEBUG    rasa.core.processor  - Starting a new session for conversation ID '76919d5f9f2d43c194b06f773f223563'.
2021-03-31 11:04:47 DEBUG    rasa.core.actions.action  - Calling action endpoint to run action 'action_session_start'.

then it takes the user input and passes it to rasa nlu as show below

2021-03-31 11:04:47 DEBUG    rasa.core.processor  - Received user message '/session_start' with intent '{'name': 'session_start', 'confidence': 1.0}' and entities '[]'

so then it predicts action_session_start with the user intent

2021-03-31 11:04:47 DEBUG    rasa.core.policies.rule_policy  - Predicted default action 'action_session_start'.
2021-03-31 11:04:47 DEBUG    rasa.core.policies.ensemble  - Made prediction using user intent.
2021-03-31 11:04:47 DEBUG    rasa.core.policies.ensemble  - Added `DefinePrevUserUtteredFeaturization(False)` event.
2021-03-31 11:04:47 DEBUG    rasa.core.policies.ensemble  - Predicted next action using policy_2_RulePolicy.
2021-03-31 11:04:47 DEBUG    rasa.core.processor  - Predicted next action 'action_session_start' with confidence 1.00.
2021-03-31 11:04:47 DEBUG    rasa.core.actions.action  - Calling action endpoint to run action 'action_session_start'.

So what I’m trying to say is whenever you start the conversation with the bot for the first time I mean beginning of the conversation, the action_session_start is triggered by default as mentioned here

so even if you type hi instead of /session_start still it will trigger the action_session_start

Hi @JiteshGaikwad thanks for the analysis.

Would you see a way to deal with this problem (as not interpreting /session_start with rasa nlu or other…), since like the documentation says overriding the action_session_start could be used to start the conversation with a bot message… but not with duplicated message?

Or could I easily integrate your solution using .js script within my own chatbot to start with custom action ?

Thanks!

hey @mattvan83 you can use this solution with any front end and it’s easy to integrate.

You can just trigger this api

https://rasa.com/docs/rasa/pages/action-server-api

Hi @JiteshGaikwad thanks for helping!

I am quite new to rasa so could you provide me a quick example on how to run this api (Rasa Open Source Documentation) that differs from the classical definition of custom action in actions.py file ?

@mattvan83 If you know JS then you can check the below code which I’m using to call the action’s api

@JiteshGaikwad sorry I don’t know JS… I don’t want to abuse but could you write it in python?

Thanks!

import requests

url = "http://localhost:5055/webhook/"

payload="{ \"next_action\": \"action_name\", \"tracker\": { \"sender_id\":\"jitesh_1234\" } }"
headers = {
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)


@JiteshGaikwad thanks a lot !

Three following questions related to your code:

  1. Has the sender_id to be adapted to each user?
  2. How is recorded this custom action sent via external action server API ? Is it recorder in the tracker ?
  3. Is it useful to use ansycnhronous protocol ? I see that you use requests which is synchronous and I recently saw in the helpdesk-assistant rasa bot (GitHub - RasaHQ/helpdesk-assistant) that many run methods within custom actions used async definition?

@mattvan83

Yes

ya, you can use async protocols, you just wanted an example so I have used requests module, async is reliable.

I didn’t get what you mean by How is recorded this custom action?

I didn’t get what you mean by How is recorded this custom action ?

I mean that usually custom action is triggered by the dialogue management system which has predicted it following the identification of intent from an upstream user message. Since we launch this custom action at first time connexion, there is no upstream user message from which intent then action were predicted. Since Tracker object records everything, is this custom action also recorded the same way as other actions in Tracker ?

You can check my custom action’s code in which I have used UserUtternaceReverted Event which actually revert backs everything in the trackers before the event which was triggered

Thanks @JiteshGaikwad for your precious help.

So, I see where to define this custom action in the actions.py file. But where do I have to place the following code below calling the action server API in order to execute first this specific custom action?

import requests

url = "http://localhost:5055/webhook/"

payload="{ \"next_action\": \"action_name\", \"tracker\": { \"sender_id\":\"jitesh_1234\" } }"
headers = {
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

The above code needs to be called from your front-end

1 Like