Intents are the lifeblood of DialogFlow. They tell the agent how to respond to certain phrases, and how to interact with the user. When creating an Intent using the python api, there are a few things you can pass in and I wanted to cover several of the basics.
I got most of the information from the dialogflow_v2.types.Intent python client types doc, the projects.agent.intents DialogFlow rest api, and the dialogflow_v2.IntentsClient python client doc.
DialogFlow Intents can be created through python with the follow code.
intents_client = dialogflow.IntentsClient()
parent = intents_client.project_agent_path(dialogflow_project_id())
intent = ##INTENT DEFINITION##
response = intents_client.create_intent(parent, intent)
The "intent" variable is defined here: dialogflow_v2.types.Intent and consists of multiple options you can pass in.
The only required option is "display_name", though it won't be a very useful intent.
intents_client = dialogflow.IntentsClient()
parent = intents_client.project_agent_path(dialogflow_project_id())
intent = dialogflow.types.Intent(
display_name = "Jason's New Intent"
)
response = intents_client.create_intent(parent, intent)
The most basic useful intent needs training phrases and messages. Training phrases tell the agent when the intent should be invoked, and the messages tell the agent how it should respond.
Training phrases can get pretty complicated, but the basic form is.
part = dialogflow.types.Intent.TrainingPhrase.Part(
text = "Training Phrase to Invoke")
training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part])
intent = dialogflow.types.Intent(
display_name = "Jason's New Intent",
training_phrases = [training_phrase]
)
You can pass in as many training phrases as you like for the intent, up to the dialogflow limit.
Training phrases get complicated when you start adding in parameters, which is something I haven't tackled yet.
The messages are just the standard response you want the agent to have when one of the training phrases is triggered. All the messages are handled via the dialogflow.types.Intent.Message object
You can set up any type of message, but the most basic is just a text response.
messages = []
text = dialogflow.types.Intent.Message.Text(text = ["First message"])
text_message = dialogflow.types.Intent.Message(text=text)
messages.append(text_message)
intent = dialogflow.types.Intent(
display_name = "Jason's New Intent",
training_phrases = [training_phrase],
messages = messages
)
You can append as many new text messages as DialogFlow will allow. Each type of message response has its own object, including QuickReplies, Cards, Images, custom payloads, and many more. You can see a full list here. They all work pretty similarly to the text one.
image = dialogflow.types.Intent.Message.Image(image_uri="https://s3.amazonaws.com/com.niches.production/niche_pages/square_images/000/001/619/giantc/sous-vide-ribeye-white-bean-puree-17.jpg")
image_message = dialogflow.types.Intent.Message(image=image)
messages.append(image_message)
quick_reply = dialogflow.types.Intent.Message.QuickReplies(
title="Reply Prompt",
quick_replies=["reply1", "reply2"])
quick_reply_message = dialogflow.types.Intent.Message(quick_replies=quick_reply)
messages.append(quick_reply_message)
Note that using the DialogFlow Quick Replies feature you can only have quick replies pass on the text on the button. To pass a different value, you will need to use a custom payload.
If we put the above together we will end up with:
intents_client = dialogflow.IntentsClient()
parent = intents_client.project_agent_path(dialogflow_project_id())
part = dialogflow.types.Intent.TrainingPhrase.Part(
text = "Training Phrase to Invoke")
training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part])
messages = []
text = dialogflow.types.Intent.Message.Text(text = ["First message"])
text_message = dialogflow.types.Intent.Message(text=text)
messages.append(text_message)
image = dialogflow.types.Intent.Message.Image(image_uri="https://s3.amazonaws.com/com.niches.production/niche_pages/square_images/000/001/619/giantc/sous-vide-ribeye-white-bean-puree-17.jpg")
image_message = dialogflow.types.Intent.Message(image=image)
messages.append(image_message)
quick_reply = dialogflow.types.Intent.Message.QuickReplies(
title="Reply Prompt",
quick_replies=["reply1", "reply2"])
quick_reply_message = dialogflow.types.Intent.Message(quick_replies=quick_reply)
messages.append(quick_reply_message)
intent = dialogflow.types.Intent(
display_name = "Jason's New Intent",
training_phrases = [training_phrase],
messages = messages
)
response = intents_client.create_intent(parent, intent)
You can see that DialogFlow Intents can be pretty powerful even using only the basic api functionality. I'll be diving into more specifics over the coming months.