Session entities

A session represents a conversation between a Dialogflow agent and an end-user. You can create special entities, called session entities, during a session. Session entities can extend or replace custom entity types and only exist during the session that they were created for. All session data, including session entities, is stored by Dialogflow for 20 minutes.

For example, if your agent has a @fruit entity type that includes "pear" and "grape", that entity type could be updated to include "apple" or "orange", depending on the information your agent collects from the end-user. The updated entity type would have the "apple" or "orange" entity entry for the rest of the session.

Creating session entities with fulfillment

You can create session entities with fulfillment. The WebhookResponse type contains a field called sessionEntityTypes that is used to set session entities.

The following examples show how to set a fruit entity type's entity entries to apple and orange for the current session.

Example WebhookResponse:

{
  "fulfillmentMessages": [
    {
      "text": {
        "text": [
          "Choose apple or orange"
        ]
      }
    }
  ],
  "sessionEntityTypes":[
    {
      "name":"projects/project-id/agent/sessions/session-id/entityTypes/fruit",
      "entities":[
        {
          "value":"APPLE_KEY",
          "synonyms":[
            "apple",
            "green apple",
            "crabapple"
          ]
        },
        {
          "value":"ORANGE_KEY",
          "synonyms":[
            "orange"
          ]
        }
      ],
      "entityOverrideMode":"ENTITY_OVERRIDE_MODE_OVERRIDE"
    }
  ]
}

Example using the Actions on Google client library:

If you are using the Actions on Google client library, you can use the Session Entities plugin.

Your code would look similar to the following:

const { sessionEntitiesHelper } = require('actions-on-google-dialogflow-session-entities-plugin')

const app = dialogflow()
    .use(sessionEntitiesHelper())

app.intent('input.welcome', (conv) => {
  conv.ask('make your choice: apple or orange?');
  // Set the fruit session entity values to 'apple' and 'orange'.
  conv.sessionEntities.add({
    name: 'fruit',
    entities: [{
      value: 'APPLE_KEY',
      synonyms: [
        'apple', 'green apple', 'crabapple'
      ]
    }, {
      value: 'ORANGE_KEY',
      synonyms: ['orange']
    }]
  });
  conv.sessionEntities.send();
});

Creating session entities with the API

You create, manage, and update session entities using the SessionEntityTypes type.