# Custom ActionSessionStart returns twice same message

**URL:** <https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838>\
**Category:** Rasa Open Source\
**Created:** [March 29, 2021, 6:07pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838 "2021-03-29T18:07:53Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattvan83](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mattvan83/32/12835_2.png) [@mattvan83](https://forum.rasa.com/u/mattvan83)\
**Post date:** [March 29, 2021, 6:07pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/1 "2021-03-29T18:07:53Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![mattvan83](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mattvan83/32/12835_2.png) [@mattvan83](https://forum.rasa.com/u/mattvan83)\
**Post date:** [March 30, 2021, 12:55pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/2 "2021-03-30T12:55:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JiteshGaikwad](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@JiteshGaikwad](https://forum.rasa.com/u/JiteshGaikwad)\
**Post date:** [March 31, 2021, 5:50am UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/3 "2021-03-31T05:50:47Z")

</div>

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

```auto
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

```auto
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

```auto
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](https://rasa.com/docs/rasa/domain#session-configuration)

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/1/1e3873245e481c926e4ab2d360b156d0968fe857.png)

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

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/7/76351431e201c9938c130f017e67e1083939c9d6.png)

---

<div class="post-metadata">

**Author:** ![mattvan83](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mattvan83/32/12835_2.png) [@mattvan83](https://forum.rasa.com/u/mattvan83)\
**Post date:** [March 31, 2021, 11:50am UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/4 "2021-03-31T11:50:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![JiteshGaikwad](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@JiteshGaikwad](https://forum.rasa.com/u/JiteshGaikwad)\
**Post date:** [March 31, 2021, 1:02pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/5 "2021-03-31T13:02:50Z")

</div>

> [@mattvan83](#):
>
> Or could I easily integrate your solution using .js script within my own chatbot to start with custom action ?

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

---

<div class="post-metadata">

**Author:** ![JiteshGaikwad](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@JiteshGaikwad](https://forum.rasa.com/u/JiteshGaikwad)\
**Post date:** [March 31, 2021, 1:03pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/6 "2021-03-31T13:03:32Z")

</div>

You can just trigger this api

[https://rasa.com/docs/rasa/pages/action-server-api](https://rasa.com/docs/rasa/pages/action-server-api)

---

<div class="post-metadata">

**Author:** ![mattvan83](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mattvan83/32/12835_2.png) [@mattvan83](https://forum.rasa.com/u/mattvan83)\
**Post date:** [March 31, 2021, 1:47pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/7 "2021-03-31T13:47:53Z")

</div>

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](https://rasa.com/docs/rasa/pages/action-server-api)) that differs from the classical definition of custom action in `actions.py` file ?

---

<div class="post-metadata">

**Author:** ![JiteshGaikwad](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@JiteshGaikwad](https://forum.rasa.com/u/JiteshGaikwad)\
**Post date:** [March 31, 2021, 2:39pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/8 "2021-03-31T14:39:29Z")

</div>

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

> <https://github.com/JiteshGaikwad/ChatbotWidget/blob/6b261c373c43bb65bbd529ad3703abe217089235/static/js/components/chat.js#L301>

---

<div class="post-metadata">

**Author:** ![mattvan83](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mattvan83/32/12835_2.png) [@mattvan83](https://forum.rasa.com/u/mattvan83)\
**Post date:** [March 31, 2021, 2:43pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/9 "2021-03-31T14:43:31Z")

</div>

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

Thanks!

---

<div class="post-metadata">

**Author:** ![JiteshGaikwad](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@JiteshGaikwad](https://forum.rasa.com/u/JiteshGaikwad)\
**Post date:** [March 31, 2021, 2:51pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/10 "2021-03-31T14:51:17Z")

</div>

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![mattvan83](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mattvan83/32/12835_2.png) [@mattvan83](https://forum.rasa.com/u/mattvan83)\
**Post date:** [March 31, 2021, 2:58pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/11 "2021-03-31T14:58:54Z")

</div>

@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](https://github.com/RasaHQ/helpdesk-assistant)) that many run methods within custom actions used `async` definition?

---

<div class="post-metadata">

**Author:** ![JiteshGaikwad](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@JiteshGaikwad](https://forum.rasa.com/u/JiteshGaikwad)\
**Post date:** [March 31, 2021, 3:07pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/12 "2021-03-31T15:07:18Z")

</div>

@mattvan83

> [@mattvan83](#):
>
> Has the `sender_id` to be adapted to each user?

Yes

> [@mattvan83](#):
>
> 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](https://github.com/RasaHQ/helpdesk-assistant)) that many run methods within custom actions used `async` definition?

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

> [@mattvan83](#):
>
> How is recorded this custom action sent via external action server API ? Is it recorder in the tracker ?

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

---

<div class="post-metadata">

**Author:** ![mattvan83](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mattvan83/32/12835_2.png) [@mattvan83](https://forum.rasa.com/u/mattvan83)\
**Post date:** [March 31, 2021, 3:14pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/13 "2021-03-31T15:14:24Z")

</div>

> 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` ?

---

<div class="post-metadata">

**Author:** ![JiteshGaikwad](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@JiteshGaikwad](https://forum.rasa.com/u/JiteshGaikwad)\
**Post date:** [March 31, 2021, 3:33pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/14 "2021-03-31T15:33:57Z")

</div>

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

> <https://github.com/JiteshGaikwad/HR_Bot/blob/79522b8c2dd91b4ef5fc6b99b6c7bf13b9295aaa/actions.py#L28>

> **[rasa.shared.core.events](https://rasa.com/docs/rasa/2.1.x/reference/rasa/shared/core/events#userutterancereverted-objects)**
>
> An open source machine learning framework for automated text and voice-based conversations

---

<div class="post-metadata">

**Author:** ![mattvan83](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/mattvan83/32/12835_2.png) [@mattvan83](https://forum.rasa.com/u/mattvan83)\
**Post date:** [March 31, 2021, 3:38pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/15 "2021-03-31T15:38:15Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![JiteshGaikwad](https://avatars.discourse-cdn.com/v4/letter/j/77aa72/32.png) [@JiteshGaikwad](https://forum.rasa.com/u/JiteshGaikwad)\
**Post date:** [March 31, 2021, 3:40pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/16 "2021-03-31T15:40:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MelT](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/melt/32/21530_2.png) [@MelT](https://forum.rasa.com/u/MelT)\
**Post date:** [November 3, 2022, 2:19pm UTC](https://forum.rasa.com/t/custom-actionsessionstart-returns-twice-same-message/41838/17 "2022-11-03T14:19:19Z")

</div>

> [@JiteshGaikwad](#):
>
> [Rasa & Rasa Pro Documentation](https://rasa.com/docs/rasa/pages/action-server-api)

Hi @JiteshGaikwad, I assume you get millions of people asking you about your great widget! But could you explain hoe can one trigger the custom action from your front end and also pass an entity value with that trigger? Specifically, I get an ID of users (not related to RASA user ID) from the URL and would like to pass it to the RASA bot. Is there a way how one can achieve this with your front end?

Many thanks in advance!
