# Context

A bot sometimes needs to

* **Remember** the information the user provided minutes ago.
* **Personalize** the same response to 2 users differently.

With contextual information like **Memory**, **User Attribute**, and **NLP Attribute**, a bot makes smarter decisions to find a Passing Rule.

![Types of Contexts - User, Memory and NLP](https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LirAdLo22OkAW9w3tvY%2F-MjEDiXNugrG5CSjVQBi%2F-MjEKqthEq0LunTi54yW%2FScreenshot%202021-09-10%20at%206.53.21%20PM.png?alt=media\&token=c52c165b-8a15-447a-966b-c38a50c68917)

### Session

{% hint style="info" %}
A group of user interactions with your bot that take place within a given time frame.&#x20;
{% endhint %}

A single session may have multiple messages, button clicks, and transactions. A single user may have multiple chat sessions with your bot, occurring on the same day or over several days, weeks, or months. By default, a session expires after **10 minutes** of inactivity. You may customize your bot's session length on the [Personality](https://docs.botdistrikt.com/features/personality) page.

{% hint style="warning" %}
Changing Session Length affects the [Time Spent](https://docs.botdistrikt.com/how-botdistrikt-works/broken-reference) and [Sessions](https://docs.botdistrikt.com/how-botdistrikt-works/broken-reference) stats.
{% endhint %}

### Memory

A temporary variable is used to store additional information about a chat session. It is used to **remember** topics and values that the user was talking about in the current session. Access it in the bot's responses with the merge tag `{{memory.property-name}}`.

Here's an example of memory property usage

> **User:** How much is the large pepperoni pizza?\
> **Bot:** The large pepperoni pizza is $29.90\
> `Bot stores a memory property: item-requested, value: pepperoni pizza`\
> \-------------\
> **User:** How about the small one?\
> `Bot remembers "pepperoni pizza" from the item-requested memory property`\
> **Bot:** The small pepperoni pizza is $19.90

{% hint style="info" %}
Memory properties are ALWAYS hyphenated ([kebab-cased](https://en.wikipedia.org/wiki/Kebab_case)), so item-requested is a valid memory property ✅ but item\_requested is NOT ❌
{% endhint %}

{% hint style="danger" %}
&#x20;When your session expires, your bot forgets its memory context (i.e. all its memory properties)
{% endhint %}

### User Attribute

A permanent variable used to store custom information about a user messaging your bot. Access the user context (i.e. all user attributes)  in User Profiles. It is used to keep track of long-term custom fields like preferences, survey answers, access levels, and transaction activity. It is essential for customer segmentation, which in turn allows you to create personalized chat experiences and targeted broadcasts. Access it in the bot's responses with the merge tag `{{user.attribute_name}}`.

{% hint style="info" %}
User attributes are ALWAYS underscored ([snake\_cased](https://en.wikipedia.org/wiki/Snake_case)), so user\_address is a valid user attribute ✅ but user-address is NOT ❌
{% endhint %}

<div align="right"><figure><img src="https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LirAdLo22OkAW9w3tvY%2Fuploads%2FHzDJI9qdn9735gp6Okl9%2Fattributes%20story.png?alt=media&#x26;token=9af9646a-6ed9-426a-a900-0f52b37d48a8" alt=""><figcaption><p>User attribute used in a greeting</p></figcaption></figure></div>

<div data-full-width="true"><figure><img src="https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LirAdLo22OkAW9w3tvY%2Fuploads%2FsqyBcCv78RxB4AER2PBd%2Fattributes%20chat.png?alt=media&#x26;token=1ba708e9-e140-4c98-9954-8f39df5b6117" alt=""><figcaption></figcaption></figure></div>

### NLP Attribute

A derived variable from connected Artificial Intelligence (AI) integrations. You may connect any number of AI integrations like Dialogflow and Wit.ai. After adding at least one AI integration, every message sent to your bot is forwarded to the AI integration. The AI integration derives intents, traits, and entities and stores them in the NLP context (i.e. all NLP attributes).&#x20;

Access it in your bot's responses with the merge tag `{{nlp.attribute_name}}`.

{% hint style="info" %}
NLP attributes are ALWAYS underscored ([snake\_cased](https://en.wikipedia.org/wiki/Snake_case))
{% endhint %}

### Shortcut

A static global variable for your bot, used in any response in every chat with every user. Shortcuts are declared on the Personality page. It is useful for text replacements like on [iPhones](https://support.apple.com/en-sg/guide/iphone/iph6d01d862/ios) and [Androids](https://www.howtogeek.com/276635/how-to-add-custom-text-shortcuts-to-android/), and also for storing environment variables for Developers.&#x20;

Access it in a conversation with the merge tag `{{bot.shortcut_name}}`.

{% hint style="info" %}
Shortcuts are ALWAYS underscored ([snake\_cased](https://en.wikipedia.org/wiki/Snake_case))
{% endhint %}

### Action

A way to dynamically modify contexts during a session. Once you add actions in stories, it modifies one of 4 contexts: memory, user attributes, user tags, and shortcuts. An action has 3 parts to it:&#x20;

* Property
* Function
* Value

From our example above, we create a story with one action:

|    | Context  | Property         | Function | Value             |
| -- | -------- | ---------------- | -------- | ----------------- |
| A1 | `memory` | `item-requested` | `set to` | `pepperoni pizza` |

<figure><img src="https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LirAdLo22OkAW9w3tvY%2Fuploads%2FpkvTrnfLMfvYjQhdUExX%2FNew%20Project%20(26).png?alt=media&#x26;token=1bb8a90f-fa37-4d96-ab91-f9817e4ae7e2" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2535542804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LirAdLo22OkAW9w3tvY%2Fuploads%2FDKoxM9I8wVdjknLvHrMx%2FNew%20Project%20(27).png?alt=media&#x26;token=ba8b53c2-cc5e-4fef-b568-7172d16ddd7b" alt=""><figcaption></figcaption></figure>

The interaction in our example looks like this:

> **User:** How much is the large pepperoni pizza?\
> **Bot:** The large pepperoni pizza is $29.90\
> `Bot stores a memory property: item-requested, value: pepperoni pizza`

An action has a lifespan.

### Lifespan

The number of messages for which a new memory property from an action remains active. The default lifespan of a memory property is **1**. This means that the bot remembers that you stored the new memory property `item-requested` for 1 more message from the user. After the user replies with 1 message, `item-requested` is removed from the memory context automatically. The 3 most commonly used lifespans are

| Lifespan | Description                                                      | Use                                                                                               |
| -------- | ---------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| 1        | Remembers the property for 1 more message                        | Direct answers from users, e.g. Yes/No Questions                                                  |
| 0        | Remembers the property for the entire period of the chat session | Session-wide topics, e.g Browsing subtopics                                                       |
| -1       | Remembers the property for the current message                   | Intermediate values for subsequent actions or rules, e.g. Variables for Mathematical Calculations |

{% hint style="info" %}
Only Memory Properties have customizable lifespans. User Attributes are stored permanently. NLP Attributes have a fixed lifespan value of **-1**
{% endhint %}

### Function

A block of JavaScript code used in conditions and actions. It is used within a condition for comparisons, or within an action for assignments.

### Webhook

An API request made to external software. There are 2 methods available - `GET` and `POST` . It is used to collect and update dynamic information in your custom technology infrastructure or integrations, and to pre-populate responses, quick replies, and modify memory and user contexts.
