Get cookies data from webpage whenver bot open

i want to extract the cookie data from the web page whenver chat window open

In my case I use the Rasa Webchat widget.

This widget provides different events to be able to react on them, it has two types of events:

  • Events on the socket connection (onSocketEvent), for example, to detect when a connection has been established with Rasa (this indicates that the page has been opened and rendered in the browser).
  • It also provides events on the Frontend widget itself (onWidgetEvent), such as onChatOpen, warning that the chat has been opened.

So, you can get a browser cookie in the following way using Javascript:

function getCookie(cookieName) {
    let cookie = {};
    document.cookie.split(';').forEach(function(el) {
        let [key,value] = el.split('=');
        cookie[key.trim()] = value;
    return cookie[cookieName];

And you can send the cookies to Rasa as entities using the initPayload attribute of the widget.

Example to generate the initPayload:

    function generateInitPayload() {
        let action = '/start_intent';

        let csrftoken = getCookie("csrftoken");
        let sessionid = "{{ request.session.session_key }}";
        let csrfmiddlewaretoken = document.getElementsByName("csrfmiddlewaretoken")[0].value;
        return action + '{\"csrfmiddlewaretoken\":\"' + csrfmiddlewaretoken + '\",\"csrftoken\":\"' + csrftoken + '\",\"sessionid\":\"' + sessionid + '\"}';

The above code would result in a string like this:


To send it:

        title: 'Rasa bot',
        subtitle: getChatSubtitle(),
        initPayload: generateInitPayload(),
        customData: { language: "en" },
        socketUrl: "http://localhost:5005",
1 Like

can you show how you get this value in rasa actions file @edu_vives

What you must do is to map the entity to a slot, so that whenever that entity is detected, its value will be automatically saved to the Slot you specify in the domain.yml file.

To do this, in rasa 2.x you must use the auto_fill field, indicating that its value is true for that slot.


First create the entity.

  - sessionid
  - csrftoken
  - csrfmiddlewaretoken

Then create the slot with the same name as the previously created entity. Indicate in the slot that its value will be set from the entity automatically:

    type: text
    influence_conversation: false
    auto_fill: true
    type: text
    influence_conversation: false
    auto_fill: true
    type: text
    influence_conversation: false
    auto_fill: true

In rasa 3.x you can use Slot Mappings. In the following link you can learn how to use it: Domain

Now you can access the value of the slots from the custom actions. To do this you must call the tracker method get_slot , as follows: tracker.get_slot("slot_name")

To get the value of a Slot:


If you need to modify that value from a custom action, you can set the value of a Slot using the SlotSet Event :


In case you have any doubt, the following video shows in detail how to work with Slots inside the Custom Actions, how to obtain and set the value of these…

I hope it helps you