Manage contexts

In most cases, you configure contexts at design-time (when you are building an agent). In some advanced scenarios, you may also want to write code that gets and sets some contexts at runtime (during a live conversation).

This document describes how to get and set contexts at design-time and runtime by using the Dialogflow Console, the API, or fulfillment.

Context naming

The following rules apply to naming contexts:

Rule Examples
Use alphanumeric names. mycontext1
Use - or _ instead of spaces. my-context-1
Names are not case sensitive. Abc123 and abc123 are considered equivalent.
All context names are lowercase when using the API. abc123

Set contexts at design-time

Console

  1. Go to the Dialogflow Console.
  2. Select an agent.
  3. Select Intents in the left sidebar menu.
  4. Expand the Contexts section of the intent data.
  5. Click the Add output context or Add input context field in intent data.
  6. Enter a name for the context and press Return.
  7. For output contexts, optionally click the lifespan circle and change the lifespan. Lifespan UI snapshot
  8. Add more contexts as needed.
  9. Click Save.

API

See the Intents reference.

Set contexts at runtime

Fulfillment

When your webhook service sends a WebhookResponse, set the outputContexts field to desired active contexts.

API

The request for a Sessions type detectIntent call contains a queryParameters.contexts field, which is used to set active contexts.

At any time, you can also call the create, delete, and update methods on the Contexts type.

Get contexts at runtime

Fulfillment

When your webhook service receives a WebhookRequest, the queryResult.outputContexts field contains the active contexts.

API

The response to a Sessions type detectIntent call contains a queryResult.outputContexts field, which provides the list of active contexts.

At any time, you can also call the get and list methods on the Contexts type.