# Get cookies data from webpage whenver bot open

**URL:** <https://forum.rasa.com/t/get-cookies-data-from-webpage-whenver-bot-open/55760>\
**Category:** Rasa Open Source\
**Created:** [October 13, 2022, 12:59pm UTC](https://forum.rasa.com/t/get-cookies-data-from-webpage-whenver-bot-open/55760 "2022-10-13T12:59:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anshal](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@anshal](https://forum.rasa.com/u/anshal)\
**Post date:** [October 13, 2022, 12:59pm UTC](https://forum.rasa.com/t/get-cookies-data-from-webpage-whenver-bot-open/55760/1 "2022-10-13T12:59:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edu\_vives](https://avatars.discourse-cdn.com/v4/letter/e/7993a0/32.png) [@edu\_vives](https://forum.rasa.com/u/edu_vives)\
**Post date:** [October 13, 2022, 4:26pm UTC](https://forum.rasa.com/t/get-cookies-data-from-webpage-whenver-bot-open/55760/2 "2022-10-13T16:26:09Z")

</div>

In my case I use the [Rasa Webchat](https://github.com/botfront/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:

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

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

```auto
/start_intent{"csrfmiddlewaretoken":"middlewaretoken_value","csrftoken":"token_value","sessionid":"sessionid_value"}

```

To send it:

```auto
      window.WebChat.default(
      {
        title: 'Rasa bot',
        subtitle: getChatSubtitle(),
        initPayload: generateInitPayload(),
        customData: { language: "en" },
        socketUrl: "http://localhost:5005",
        ...

```

---

<div class="post-metadata">

**Author:** ![anshal](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@anshal](https://forum.rasa.com/u/anshal)\
**Post date:** [October 14, 2022, 7:39am UTC](https://forum.rasa.com/t/get-cookies-data-from-webpage-whenver-bot-open/55760/3 "2022-10-14T07:39:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edu\_vives](https://avatars.discourse-cdn.com/v4/letter/e/7993a0/32.png) [@edu\_vives](https://forum.rasa.com/u/edu_vives)\
**Post date:** [October 14, 2022, 1:21pm UTC](https://forum.rasa.com/t/get-cookies-data-from-webpage-whenver-bot-open/55760/4 "2022-10-14T13:21:35Z")

</div>

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.

Example:

First create the entity.

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

```auto
slots:
  sessionid:
    type: text
    influence_conversation: false
    auto_fill: true
  csrftoken:
    type: text
    influence_conversation: false
    auto_fill: true
  csrfmiddlewaretoken:
    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](https://rasa.com/docs/rasa/domain/#slot-mappings)

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:

![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/3X/7/0/70e14298ec11716f9b80b599763c8aac68deb15d.png)

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

![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/3X/f/a/fab0f35d073981ea95cc382e10f5cf1141f135f7.png)

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…

[![](https://img.youtube.com/vi/D3DigLEd6Gw/hqdefault.jpg "Rasa Chatbot Tutorial | How to use Slots (Bot's memory)| Slot set from Custom Action | RASA - 8") ](https://www.youtube.com/watch?v=D3DigLEd6Gw)

I hope it helps you
