Here is an example of creating an intent in a
Dialogflow agent.
Set up your GCP project and authentication
If you have not created a
Google Cloud Platform (GCP) project
and service account credentials,
do so now.
Expand this section for instructions.
To use Dialogflow,
you must create a GCP project and enable the Dialogflow API.
To use the API directly via REST, RPC, or client libraries,
you must also create a service account and download a private key for authentication.
The quick steps below will accomplish this.
For more detail on these steps, see
Project setup and management.
We recommend that you create a special GCP project for quickstarts and how-to guides,
so the changes you apply when experimenting do not affect your production service.
When creating a project, take note of the
project ID.
You will use this ID often to make API calls.
You can view and manage these resources at any time in the
GCP Console.
Set the environment variable GOOGLE_APPLICATION_CREDENTIALS
to the file path of the JSON file that contains your service account key.
This variable only applies to your current shell session, so if you open
a new session, set the variable again.
Example: Linux or macOS
Replace [PATH] with the file path of the JSON file that
contains your service account key.
You should create a new agent before following the steps in this guide.
Expand this section for instructions.
The steps in this guide make assumptions about your agent.
You should delete any existing agent for the GCP project you use for quickstart and how-to guides,
and create a new one.
Click Create Agent in the left menu.
(If you already have other agents, click the agent name,
scroll to the bottom and click Create new agent.)
Enter your agent's name, default language, and default time zone.
If you have created a GCP project in steps above, enter that project.
If you want to allow the Dialogflow console to create the project,
select Create a new Google project.
Click the Create button.
Import the example file to your agent
Importing will add intents and entities to your agent.
If any existing intents or entities have the same name as those in the imported file,
they will be replaced.
Click the settings settings button next to the agent name
Select the Export and Import tab
Select Import From Zip and import the zip file that you downloaded
Create intent
Protocol
Intents determine which user requests are understood and what action to take.
In this example, we will create an intent to expand what the room reservation
agent can respond to. The new intent will associate user questions about
which rooms are available with the action listRooms.
To create the intent, use the following
curl command to access the intents resource.
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': 'ListRooms',
'priority': 500000,
'webhookState': 'WEBHOOK_STATE_UNSPECIFIED',
'trainingPhrases': [
{
'type': 'EXAMPLE',
'parts': [
{
'text': 'What rooms are available at 10am today?'
}
]
}
],
'action': 'listRooms',
'messages': [
{
'text': {
'text': [
'Here are the available rooms:'
]
}
}
],
}" "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/5b290a94-55d6-4074-96f4-9c4c4879c2bb",
"displayName": "ListRooms",
"priority": 500000,
"action": "listRooms",
"messages": [
{
"text": {
"text": [
"Here are the available rooms:"
]
}
}
]
}
If you detect intent with text of "what rooms are available at 9am?",
then you should see a response similar to the following. Notice that
the user intent is mapped to the listRooms action.
{
"responseId": "5b35eb91-084f-4072-ac08-3ed47ed6e6ae",
"queryResult": {
"queryText": "what rooms are available at 9am?",
"action": "listRooms",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentText": "Here are the available rooms:",
"fulfillmentMessages": [
{
"text": {
"text": [
"Here are the available rooms:"
]
}
}
],
"intent": {
"name": "projects/project-id/agent/intents/5b290a94-55d6-4074-96f4-9c4c4879c2bb",
"displayName": "ListRooms"
},
"intentDetectionConfidence": 0.63,
"diagnosticInfo": {},
"languageCode": "en-us"
}
}
public static int Create(string projectId,
string displayName,
string messageText,
string[] trainingPhrasesParts)
{
var client = IntentsClient.Create();
var text = new Intent.Types.Message.Types.Text();
text.Text_.Add(messageText);
var message = new Intent.Types.Message()
{
Text = text
};
var phraseParts = new List<Intent.Types.TrainingPhrase.Types.Part>();
foreach (var part in trainingPhrasesParts)
{
phraseParts.Add(new Intent.Types.TrainingPhrase.Types.Part()
{
Text = part
});
}
var trainingPhrase = new Intent.Types.TrainingPhrase();
trainingPhrase.Parts.AddRange(phraseParts);
var intent = new Intent();
intent.DisplayName = displayName;
intent.Messages.Add(message);
intent.TrainingPhrases.Add(trainingPhrase);
var newIntent = client.CreateIntent(
parent: new ProjectAgentName(projectId),
intent: intent
);
Console.WriteLine($"Created Intent: {newIntent.Name}");
return 0;
}
/**
* Create an intent of the given intent type
*
* @param displayName The display name of the intent.
* @param projectId Project/Agent Id.
* @param trainingPhrasesParts Training phrases.
* @param messageTexts Message texts for the agent's response when the intent is detected.
* @return The created Intent.
*/
public static Intent createIntent(
String displayName,
String projectId,
List<String> trainingPhrasesParts,
List<String> messageTexts) throws Exception {
// Instantiates a client
try (IntentsClient intentsClient = IntentsClient.create()) {
// Set the project agent name using the projectID (my-project-id)
ProjectAgentName parent = ProjectAgentName.of(projectId);
// Build the trainingPhrases from the trainingPhrasesParts
List<TrainingPhrase> trainingPhrases = new ArrayList<>();
for (String trainingPhrase : trainingPhrasesParts) {
trainingPhrases.add(
TrainingPhrase.newBuilder().addParts(
Part.newBuilder().setText(trainingPhrase).build())
.build());
}
// Build the message texts for the agent's response
Message message = Message.newBuilder()
.setText(
Text.newBuilder()
.addAllText(messageTexts).build()
).build();
// Build the intent
Intent intent = Intent.newBuilder()
.setDisplayName(displayName)
.addMessages(message)
.addAllTrainingPhrases(trainingPhrases)
.build();
// Performs the create intent request
Intent response = intentsClient.createIntent(parent, intent);
System.out.format("Intent created: %s\n", response);
return response;
}
}
namespace Google\Cloud\Samples\Dialogflow;
use Google\Cloud\Dialogflow\V2\IntentsClient;
use Google\Cloud\Dialogflow\V2\Intent_TrainingPhrase_Part;
use Google\Cloud\Dialogflow\V2\Intent_TrainingPhrase;
use Google\Cloud\Dialogflow\V2\Intent_Message_Text;
use Google\Cloud\Dialogflow\V2\Intent_Message;
use Google\Cloud\Dialogflow\V2\Intent;
/**
* Create an intent of the given intent type.
*/
function intent_create($projectId, $displayName, $trainingPhraseParts = [],
$messageTexts = [])
{
$intentsClient = new IntentsClient();
// prepare parent
$parent = $intentsClient->projectAgentName($projectId);
// prepare training phrases for intent
$trainingPhrases = [];
foreach ($trainingPhraseParts as $trainingPhrasePart) {
$part = new Intent_TrainingPhrase_Part;
$part->setText($trainingPhrasePart);
// create new training phrase for each provided part
$trainingPhrase = new Intent_TrainingPhrase();
$trainingPhrase->setParts([$part]);
$trainingPhrases[] = $trainingPhrase;
}
// prepare messages for intent
$text = new Intent_Message_Text();
$text->setText($messageTexts);
$message = new Intent_Message();
$message->setText($text);
// prepare intent
$intent = new Intent();
$intent->setDisplayName($displayName);
$intent->setTrainingPhrases($trainingPhrases);
$intent->setMessages([$message]);
// create intent
$response = $intentsClient->createIntent($parent, $intent);
printf('Intent created: %s' . PHP_EOL, $response->getName());
$intentsClient->close();
}
def create_intent(project_id, display_name, training_phrases_parts,
message_texts):
"""Create an intent of the given intent type."""
import dialogflow_v2 as dialogflow
intents_client = dialogflow.IntentsClient()
parent = intents_client.project_agent_path(project_id)
training_phrases = []
for training_phrases_part in training_phrases_parts:
part = dialogflow.types.Intent.TrainingPhrase.Part(
text=training_phrases_part)
# Here we create a new training phrase for each provided part.
training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part])
training_phrases.append(training_phrase)
text = dialogflow.types.Intent.Message.Text(text=message_texts)
message = dialogflow.types.Intent.Message(text=text)
intent = dialogflow.types.Intent(
display_name=display_name,
training_phrases=training_phrases,
messages=[message])
response = intents_client.create_intent(parent, intent)
print('Intent created: {}'.format(response))