If you are not using an integration, you must write code to interact with end-users. For each conversational turn, your code calls the Dialogflow API to query your agent. This guide shows you how to interact with an agent by using the REST API at the command line and by using the client libraries.
Before you begin
If you do not plan on using the API, you can skip this quickstart.
You should do the following before reading this guide:
- Understand Dialogflow basics.
- Perform setup steps.
- Perform steps in the
Build an agent
quickstart guide.
Steps below continue working on the agent you started in that guide.
If you no longer have that agent, you can download
build-agent-quickstart.zip
and import the file.
Sessions
A session represents a conversation between a Dialogflow agent and an end-user. You create a session at the beginning of a conversation and use it for each turn of the conversation. Once the conversation has ended, you discontinue using the session.
You should not use the same session for concurrent conversations with different end-users. Dialogflow maintains the currently active contexts for each active session. Session data is stored by Dialogflow for 20 minutes.
Each session is determined unique by a session ID generated by your system. You create a new session by providing a new session ID in a detect intent request. A session ID is a string of at most 36 bytes in size. Your system is responsible for generating unique session IDs. They can be random numbers, hashed end-user identifiers, or any other values that are convenient for you to generate.
Detect intent
When you use the API for interactions,
your service interacts directly with the end-user.
For each conversational turn,
your service sends end-user expressions to Dialogflow by calling the
detectIntent
or streamingDetectIntent
method of the
Sessions
type.
Dialogflow responds with information about the matched intent,
the action, the parameters, and the response defined for the intent.
Your service performs actions as needed
(for example, database queries or external API calls)
and sends a message to the end-user.
This process continues until the conversation has ended.
The following samples show how to detect intent. Each sample accepts a subset of the following inputs:
- Project ID: Use the project ID for the project you created in the setup steps.
- Session ID: For the purpose of testing an agent, you can use anything. For example, "123456789" is frequently used by samples.
- Text or texts: This is the single end-user expression or list of end-user expressions. If multiple expressions are supplied, the sample code calls detect intent for each expression. Try using "I know french".
- Language code: The language code for the end-user expression. Use "en-US" for this example agent.
REST
To detect intent, call thedetectIntent
method on the Sessions
resource.
Before using any of the request data, make the following replacements:
- PROJECT_ID: your Google Cloud project ID
- SESSION_ID: a session ID
HTTP method and URL:
POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/agent/sessions/SESSION_ID:detectIntent
Request JSON body:
{ "query_input": { "text": { "text": "I know french", "language_code": "en-US" } } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "responseId": "856510ca-f617-4e25-b0bb-a26c0a59e030-19db3199", "queryResult": { "queryText": "I know french", "parameters": { "language": "French", "language-programming": "" }, "allRequiredParamsPresent": true, "fulfillmentText": "Wow! I didn't know you knew French. How long have you known French?", "fulfillmentMessages": [ { "text": { "text": [ "Wow! I didn't know you knew French. How long have you known French?" ] } } ], "outputContexts": [ { "name": "projects/PROJECT_ID/agent/sessions/123456789/contexts/set-language-followup", "lifespanCount": 2, "parameters": { "language": "French", "language.original": "french", "language-programming": "", "language-programming.original": "" } } ], "intent": { "name": "projects/PROJECT_ID/agent/intents/fe45022f-e58a-484f-96e8-1cbd6628f648", "displayName": "set-language" }, "intentDetectionConfidence": 1, "languageCode": "en" } }
Note the following about the response:
- The
queryResult.intent
field contains the matched intent. - The value of the
queryResult.fulfillmentMessages
field contains the intent response. This is the response that your system should forward to the end-user. - The value of the
queryResult.parameters
field contains the parameters extracted from the end-user expression. - The
queryResult.outputContext
field contains the active context.
To authenticate to Dialogflow, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
To authenticate to Dialogflow, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
To authenticate to Dialogflow, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
To authenticate to Dialogflow, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C#:
Please follow the
C# setup instructions
on the client libraries page
and then visit the
Dialogflow reference documentation for .NET.
PHP:
Please follow the
PHP setup instructions
on the client libraries page
and then visit the
Dialogflow reference documentation for PHP.
Ruby:
Please follow the
Ruby setup instructions
on the client libraries page
and then visit the
Dialogflow reference documentation for Ruby.
Go
Java
Node.js
Python
Additional languages
Productionization
Before running your agent in production, be sure to implement the productionization best practices.