Generating Unique Session-ID for Unique User | Rasa Chatbot

In this blog, I’ll show you about one of the unique but the most important topic of the Rasa chatbot which makes it multi-tasking to handle multiple conversations at a time. So what I am discussing is known as the Session ID or the Conversation ID, which is a unique identification ID for each user which will help the rasa chatbot to recognize that who is texting the bot, what reply it has to give to the which user, etc. That’s all just because of this single but unique identification Id know as a session ID or conversation ID.

Now, let’s see how to generate the conversation for each unique user. Let’s get started now.

Generating Session ID

To generate or get the session ID for each conversation, I will create a short conversation which is as shown here,

User: May i know what is my conversation id
Bot: The conversation id is 5d24a6dbf2684897aed420465deffda1

So, this is a short conversation that I’ll have with my bot and my bot will tell me the unique session ID in return. Now let’s create this conversation into the rasa project and let’s understand how to get the unique session ID. For that make the changes into the existing Rasa project in such a way,

nlu.md

## intent:ask_id
- tell me the conversation id
- what's my conversation id
- tell me session id
- may i know what is my session id
- What is the ID of this conversation?

Here, I have create a new intent which is a user input to ask for the conversation ID.

actions.py

class ActionSessionId(Action):
    def name(self) -> Text:
        return "action_session_id"

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

        conversation_id=tracker.sender_id
        dispatcher.utter_message("The conversation id is {}".format(conversation_id))
        return []

In the actions.py script, I’ve added a new action class where I’ve fetched the session ID using the tracker. Rasa itself takes care of this session ID, so we don’t have to generate it manually. That is the reason which makes it multi-tasking more user friendly.

conversation_id=tracker.sender_id
dispatcher.utter_message("The conversation id is {}".format(conversation_id))

This code is used to get the unique session ID and to display it to the user in return with session ID.

domain.yml

intents:
- ask_id:
triggers: action_session_id
actions:
- action_session_id

Here, instead of calling the stories, I just triggered the custom action directly with respect to the intent that we created to ask for the session/conversation ID.

You can also check this video for more clarity,

After you have added all the codings and data to the project, it’s time to train the model and test it with the following commands,

rasa train
rasa x
and also run action server in seperate terminal,
rasa run actions

and once the model is trained and Rasa UI is running you can talk your bot to check the conversation id, which will look something like this.

You can download the full code here,

DOWNLOAD FULL CODE HERE

Also, as I said this conversation ID is a unique ID so for every user talking to the bot will have a unique session ID. To explain this here you can see the conversation ID for different users who talked to the bot.

This is all about this blog, I hope you have enjoyed this blog. But still, if you are having any queries regarding the topic or you want to share your views then feel free to leave a comment below in the comment section.
Stay tuned and Happy Learning. 🙂

2 thoughts on “Generating Unique Session-ID for Unique User | Rasa Chatbot

  1. hello, how can i use rasa x conversation id in rasa because i am getting default value as a session id from rasa and i need to use rasa x conversation id as you shown in your video.

Leave a Reply

Your email address will not be published. Required fields are marked *