Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Na maioria dos casos, você configura contextos no momento do projeto (ao criar um agente). Por exemplo, quando você tem uma frase de treinamento como: "Gostaria de adicionar pizza ao meu carrinho de compras".
Em alguns cenários avançados, talvez você queira escrever um código que busque e defina alguns contextos no ambiente de execução (durante uma conversa ativa). Por exemplo, você detectou um local do dispositivo e o adicionou ao contexto, para que possa se referir ao local posteriormente.
Este documento descreve como conseguir e definir contextos no momento do projeto e no ambiente de execução usando o Console do Dialogflow, a API ou fulfillment.
Nomenclatura de contexto
As regras a seguir se aplicam a contextos de nomenclatura:
Regra
Exemplos
Use nomes alfanuméricos.
mycontext1
Use - ou _ em vez de espaços.
my-context-1
Os nomes não diferenciam maiúsculas de minúsculas.
Abc123 e abc123 são considerados equivalentes.
Ao usar a API, todos os nomes de contexto ficam em letras minúsculas.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-08-18 UTC."],[[["\u003cp\u003eContexts are primarily configured at design-time during agent building, using training phrases to set them up.\u003c/p\u003e\n"],["\u003cp\u003eContexts can be dynamically managed at runtime during live conversations, such as adding a device location to a context, using code.\u003c/p\u003e\n"],["\u003cp\u003eContexts can be set at design-time using the Dialogflow Console by navigating to the Intents section and adding input or output contexts, or through the API.\u003c/p\u003e\n"],["\u003cp\u003eAt runtime, contexts can be set using the \u003ccode\u003eoutputContexts\u003c/code\u003e field in a \u003ccode\u003eWebhookResponse\u003c/code\u003e or through the \u003ccode\u003equeryParameters.contexts\u003c/code\u003e field in a \u003ccode\u003edetectIntent\u003c/code\u003e API call.\u003c/p\u003e\n"],["\u003cp\u003eAt runtime, you can retrieve active contexts via the \u003ccode\u003equeryResult.outputContexts\u003c/code\u003e field in a \u003ccode\u003eWebhookRequest\u003c/code\u003e or in a \u003ccode\u003edetectIntent\u003c/code\u003e API response, and can be managed with create, delete, update, get, and list methods.\u003c/p\u003e\n"]]],[],null,["# Manage contexts\n\nIn most cases, you configure contexts at design-time\n(when you are building an agent). For example, when you have a training phrase like:\n\"I would like to add pizza to my shopping cart.\"\n\nIn some advanced scenarios,\nyou may also want to write code that gets and sets some contexts at runtime\n(during a live conversation). An example could be, you detected a device location\nand you have added it to the context, so you can refer to the location at a later moment.\n\nThis document describes how to get and set contexts\nat design-time and runtime by using the\n[Dialogflow Console](/dialogflow/docs/console),\n[the API](/dialogflow/docs/api-overview),\nor [fulfillment](/dialogflow/docs/fulfillment-overview).\n\nContext naming\n--------------\n\nThe following rules apply to naming contexts:\n\nSet contexts at design-time\n---------------------------\n\n### Console\n\n1. Go to the [Dialogflow ES console](https://dialogflow.cloud.google.com).\n2. Select an agent.\n3. Select **Intents** in the left sidebar menu.\n4. Expand the **Contexts** section of the intent data.\n5. Click the **Add output context** or **Add input context** field in intent data.\n6. Enter a name for the context and press **Return**.\n7. For output contexts, optionally click the lifespan circle and change the lifespan.\n8. Add more contexts as needed.\n9. Click **Save**.\n\n### API\n\nSee the\n[Intents reference](/dialogflow/docs/reference/common-types#intents).\n\nSet contexts at runtime\n-----------------------\n\n### Fulfillment\n\nWhen your webhook service sends a\n[`WebhookResponse`](/dialogflow/docs/reference/common-types#webhookresponse),\nset the `outputContexts` field to desired active contexts.\n\n### API\n\nThe request for a\n[`Sessions`](/dialogflow/docs/reference/common-types#sessions)\ntype `detectIntent` call contains a `queryParameters.contexts` field,\nwhich is used to set active contexts.\n\nThe following is a REST JSON example of a `detectIntent` request:\n\n`POST https://dialogflow.googleapis.com/v2/{session=projects/*/agent/sessions/*}:detectIntent` \n\n```\n{\n \"queryInput\": {\n \"text\": {\n \"languageCode\": \"en-US\",\n \"text\": \"I would like to add pizza to my shopping cart.\"\n }\n },\n \"queryParams\": {\n \"contexts\": [{\n \"name\": \"projects/project-id/agent/sessions/session-id/contexts/product-chosen\",\n \"lifespanCount\": 5,\n \"parameters\": {\n \"product\": \"Pizza\",\n \"device-location\" \"@52.3377871,4.8698096,17z\"\n }\n }]\n }\n}\n```\n\nAt any time,\nyou can also call the create, delete, and update methods on the\n[`Contexts`](/dialogflow/docs/reference/common-types#contexts)\ntype.\n\nGet contexts at runtime\n-----------------------\n\n### Fulfillment\n\nWhen your webhook service receives a\n[`WebhookRequest`](/dialogflow/docs/reference/common-types#webhookrequest),\nthe `queryResult.outputContexts` field contains the active contexts.\n\n### API\n\nThe response to a\n[`Sessions`](/dialogflow/docs/reference/common-types#sessions)\ntype `detectIntent` call contains a\n`queryResult.outputContexts` field,\nwhich provides the list of active contexts.\n\nThe following is a REST JSON example of a `detectIntent` response: \n\n```\n{\n \"responseId\": \"response-id\",\n \"queryResult\": {\n \"queryText\": \"I would like to add pizza to my shopping cart.\",\n \"parameters\": {\n \"product\": \"pizza\"\n },\n \"allRequiredParamsPresent\": true,\n \"fulfillmentText\": \"The product has been added.\",\n \"fulfillmentMessages\": [\n {\n \"text\": {\n \"text\": [\n \"The product has been added.\"\n ]\n }\n }\n ],\n \"outputContexts\": [\n {\n \"name\": \"projects/project-id/agent/sessions/session-id/contexts/product-chosen\",\n \"lifespanCount\": 5,\n \"parameters\": {\n \"product\": \"Pizza\",\n \"device-location\" \"@52.3377871,4.8698096,17z\"\n }\n }\n ],\n \"intent\": {\n \"name\": \"projects/project-id/agent/intents/intent-id\",\n \"displayName\": \"buy-product\"\n },\n \"intentDetectionConfidence\": 0.8057143,\n \"languageCode\": \"en\",\n }\n}\n```\n\nAt any time,\nyou can also call the get and list methods on the\n[`Contexts`](/dialogflow/docs/reference/common-types#contexts)\ntype."]]