Quickstart: Using the Command Line

This quickstart shows you how to set up a Google Cloud Platform project that uses the Dialogflow V2 API, create a Dialogflow agent for that project, and then add intents to the project that you can use to determine what actions to take based on user input.

Set up your GCP project and authentication

Create an agent

Create intents

Intents determine which user requests are understood and what action to take. In this example, we will create two intents that a user can use with a stopwatch.

  • If the user says "start stopwatch", then the first intent will specify start as the action and will return the text "Stopwatch started".

  • If the user says "stop stopwatch", then the second intent will specify stop as the action and will return the text "Stopwatch stopped".

  1. To create the intent for starting the stopwatch, use the following curl command to access the intents endpoint. The command uses the gcloud command-line interface from the Google Cloud SDK to get an authorization token for the command based on the authentication that you set up at the beginning of this quickstart. Replace project-id with your Google Cloud project ID.

    curl -X POST -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) -H "Content-Type: application/json; charset=utf-8" --data "{
      'displayName': 'StartStopwatch',
      'priority': 500000,
      'webhookState': 'WEBHOOK_STATE_UNSPECIFIED',
      'trainingPhrases': [
        {
          'type': 'EXAMPLE',
          'parts': [
            {
              'text': 'start stopwatch'
            }
          ]
        }
      ],
      'action': 'start',
       'messages': [
          {
            'text': {
              'text': [
               'Stopwatch started'
              ]
            }
          }
        ],
    }" "https://dialogflow.googleapis.com/v2/projects/project-id/agent/intents"
      

    You should see a response similar to the following:

    {
      "name": "projects/project-id/agent/intents/8f4058b1-e216-4b40-b60b-7499c9d6f236",
      "displayName": "StartStopwatch",
      "priority": 500000,
      "action": "start",
      "messages": [
        {
          "text": {
            "text": [
              "Stopwatch started"
            ]
          }
        }
      ]
    }
      
  2. To create the intent for stopping the stopwatch, use the following curl command. Replace project-id with your Google Cloud project ID.

    curl -X POST -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
                 -H "Content-Type: application/json; charset=utf-8" --data "{
      'displayName': 'StopStopwatch',
      'priority': 500000,
      'webhookState': 'WEBHOOK_STATE_UNSPECIFIED',
      'trainingPhrases': [
        {
          'type': 'EXAMPLE',
          'parts': [
            {
              'text': 'stop stopwatch'
            }
          ]
        }
      ],
      'action': 'stop',
      'messages': [
          {
            'text': {
              'text': [
               'Stopwatch stopped'
              ]
            }
          }
        ],
    }" "https://dialogflow.googleapis.com/v2/projects/project-id/agent/intents"
      

    You should see a response similar to the following:

    {
      "name": "projects/project-id/agent/intents/21c5ee2e-dd32-4f6b-94a7-0f4afedc2bf9",
      "displayName": "StopStopwatch",
      "priority": 500000,
      "action": "stop",
      "messages": [
        {
          "text": {
            "text": [
              "Stopwatch stopped"
            ]
          }
        }
      ]
    }
      

You can also view the intents that you created in the https://console.dialogflow.com console. If you already have the console open in a browser, you might need to refresh your browser to see the new intents.

Detect intent

After you have created your intents, you can pass user input to the detectIntent endpoint to determine what the user has requested and which action to take.

Follow these steps to determine the intent when the user input is "start stopwatch" or "stop stopwatch".

  1. Use the following curl command to access the detectIntent endpoint and specify user input of "start stopwatch". Replace project-id with your Google Cloud project ID.

    curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
         -H "Content-Type: application/json; charset=utf-8" --data "{
      'query_input': {
        'text': {
          'text': 'start stopwatch',
          'language_code': 'en-US'
        }
      }
    }" "https://dialogflow.googleapis.com/v2/projects/project-id/agent/sessions/123456789:detectIntent"
      

    You should see a response similar to the following. Notice that the action is start, and the response text is "Stopwatch started".

    {
      "responseId": "96cdb4e1-35d9-4fee-8d9a-465753633318",
      "queryResult": {
        "queryText": "start stopwatch",
        "action": "start",
        "parameters": {},
        "allRequiredParamsPresent": true,
        "fulfillmentText": "Stopwatch started",
        "fulfillmentMessages": [
          {
            "text": {
              "text": [
                "Stopwatch started"
              ]
            }
          }
        ],
        "intent": {
          "name": "projects/project-id/agent/intents/8f4058b1-e216-4b40-b60b-7499c9d6f236",
          "displayName": "StartStopwatch"
        },
        "intentDetectionConfidence": 1,
        "diagnosticInfo": {},
        "languageCode": "en-us"
      }
    }
      
  2. Use the following curl command to access the detectIntent endpoint and specify user input of "stop stopwatch". Replace project-id with your Google Cloud project ID.

    curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
         -H "Content-Type: application/json; charset=utf-8" --data "{
      'query_input': {
        'text': {
          'text': 'stop stopwatch',
          'language_code': 'en-US'
        }
      }
    }" "https://dialogflow.googleapis.com/v2/projects/project-id/agent/sessions/123456789:detectIntent"
      

    You should see a response similar to the following. Notice that the action is stop, and the response text is "Stopwatch stopped".

    {
      "responseId": "a6ac6312-c6a6-4bf1-a57b-4e9e75b09ea1",
      "queryResult": {
        "queryText": "stop stopwatch",
        "action": "stop",
        "parameters": {},
        "allRequiredParamsPresent": true,
        "fulfillmentText": "Stopwatch stopped",
        "fulfillmentMessages": [
          {
            "text": {
              "text": [
                "Stopwatch stopped"
              ]
            }
          }
        ],
        "intent": {
          "name": "projects/project-id/agent/intents/21c5ee2e-dd32-4f6b-94a7-0f4afedc2bf9",
          "displayName": "StopStopwatch"
        },
        "intentDetectionConfidence": 1,
        "diagnosticInfo": {},
        "languageCode": "en-us"
      }
    }
      

Congratulations! You've successfully determined user intent using the Dialogflow Enterprise Edition.

Cleanup

List intents

To delete the intents that you created for your project, first list the intents to get the intent ids, and then delete the intent using the intent id.

  1. To list the intents for your project, use the following curl command to access the intents endpoint. Replace project-id with your Google Cloud project ID.

    curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
         -H "Content-Type: application/json; charset=utf-8" \
         "https://dialogflow.googleapis.com/v2/projects/project-id/agent/intents"
      

    You should see a response similar to the following:

    {
      "intents": [
        {
          "name": "projects/project-id/agent/intents/21c5ee2e-dd32-4f6b-94a7-0f4afedc2bf9",
          "displayName": "StopStopwatch",
          "priority": 500000,
          "action": "stop",
          "messages": [
            {
              "text": {
                "text": [
                  "Stopwatch stopped"
                ]
              }
            }
          ]
        },
        {
          "name": "projects/project-id/agent/intents/8f4058b1-e216-4b40-b60b-7499c9d6f236",
          "displayName": "StartStopwatch",
          "priority": 500000,
          "action": "start",
          "messages": [
            {
              "text": {
                "text": [
                  "Stopwatch started"
                ]
              }
            }
          ]
        },
        {
          "name": "projects/project-id/agent/intents/3cab8d75-7eb8-4501-927e-2165c5c8f8ae",
          "displayName": "Default Welcome Intent",
          "priority": 500000,
          "events": [
            "WELCOME"
          ],
          "action": "input.welcome",
          "messages": [
            {
              "text": {
                "text": [
                  "Hi!",
                  "Hello!",
                  "Good day!",
                  "Greetings!"
                ]
              }
            }
          ]
        },
        {
          "name": "projects/project-id/agent/intents/847f2f11-5f86-4c38-8ec8-deba3a4d0992",
          "displayName": "Default Fallback Intent",
          "priority": 500000,
          "isFallback": true,
          "action": "input.unknown",
          "messages": [
            {
              "text": {
                "text": [
                  "I didn't get that. Can you say it again?",
                  "I missed what you said. Say it again?",
                  "Sorry, could you say that again?",
                  "Sorry, can you say that again?",
                  "Can you say that again?",
                  "Sorry, I didn't get that.",
                  "Sorry, what was that?",
                  "One more time?",
                  "What was that?",
                  "Say that again?",
                  "I didn't get that.",
                  "I missed that."
                ]
              }
            }
          ]
        }
      ]
    }
      

Delete intents

  1. To delete an intent from your project, use the following curl command to send a DELETE command to the intents endpoint. Replace project-id with your Google Cloud project ID and replace the sample intent id (21c5ee2e-dd32-4f6b-94a7-0f4afedc2bf9) with the intent id of the intent that you want to delete.

    curl -X DELETE -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
                   -H "Content-Type: application/json; charset=utf-8" \
                   "https://dialogflow.googleapis.com/v2/projects/project-id/agent/intents/21c5ee2e-dd32-4f6b-94a7-0f4afedc2bf9"
      

    You should see an empty response.

Re-list intents (optional)

You can reissue the command from the List intents step to list the intents for your agent and verify that the deleted intents have been deleted.

Delete your project

To avoid unnecessary Google Cloud Platform charges, use the GCP Console to delete your project if you do not need it.

Was this page helpful? Let us know how we did:

Send feedback about...

Dialogflow Enterprise Edition Documentation