Creating Intents and Entities with Dialogflow
Published: 2020-09-21 02:03:06

###Introduction
Last week we looked at how to create a Dialogflow agent and integrate it into our NodeJS application. This week we are going to take a more in-depth look at how to use our agent. We will create a new conversational intent, provide some entities and parameters, and get a conversation going with our agent via the simple text-box app from last week. For a better background on what exactly the Dialogflow service can provide, check out the article from last week https://dev.to/maxwellboecker/using-the-dialogflow-api-in-your-nodejs-app-4hd8 
or the official documentation https://cloud.google.com/dialogflow/es/docs. 

###Intents
A conversational intent ‘categorizes an end-user intention for one conversational turn’. This means that our agent is trying to parse exactly what the user is trying to convey with their turn in the conversation. All agents come with the ‘default welcome intent’. This means that they will be able to match our intention based on how closely our message resembles the any of the training phrases stored under their 'training phrases' list in the 'default welcome intent'.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vy5xz4c4f8b9fumdm15a.png)

When we say such things as ‘hello’, ‘hey’, or ‘hi there’, the agent recognizes our intent to greet it. It then selects one of the Responses programmed in order for it to fulfill the request. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/s87cvhuol9d5ymz4mq0k.png)

Now we’re going to walk through how to create a new intent with training phrases and to help our agent recognize this intent by supplying entities and parameters. For the example, imagine we have a small grill that people must place orders for ahead of time. We are takeout only right now due to social-distancing. I am having my Dialogflow agent 'staff' my ordering application so that I can focus on cooking up orders for my customers and getting them ready in a timely and efficient manner. I'm going to train my agent to take an order.
I've created an intent called 'ordering entree'. Let's enumerate some training phrases. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wcdpjm3390glka5815oz.png)

Here, the thing I am concerned with is that my agent can recognize the intent to order, so I provide it with some examples of how people order. 
However, I need to actually extract the value of the word used in the user request, the value of the entree. So I have to train my agent to recognize an entree and to distinguish it from something like a side or a drink. For this, I will create a parameter and corresponding entity. 
###Parameters
Defining parameters helps our agent to extract information from the user input. When our agent recognizes an intent, it searches for parameters and resolves them to the value that it finds in the entity recognized in the text written by the user. These values will then be returned in the agent's response to our NodeJS server where we can use them as we see fit. So I have created a parameter called entree, which has an entity called entree. The entity has two possible values, 'burger' and 'fish special'
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ursxfgdlsj9bjc8i43bz.png)
Adding automated values allows the agent some freewill to try and make matches out of entities that have not been explicitly defined by the programmer. This essentially means that you trust it to make matches on its own if possible.
When I test my agent by sending a message through the simple application, I am met by the response I defined for recognition of the ordering entree intent.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wtyv0c78j9u41m68br9a.png)
###Understanding the data
When my agent matches an intent sent from my user via my server, it sends back an object that contains quite a lot of information. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r2cekk2afru5tmabv9v6.png)

Notice how the queryText is the message we sent, fulfillmentText is the response our agent fulfilled the request with, and parameters is an object. Let's take a look at the parameters object:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/giqt3rmio09nlx5yrbts.png)
Our agent was able to parse out the entree item from our ordering entree request! Now we can add it to the ticket.

###Conclusion
The more entities we add for each intent, the more flexible our agent is in identifying exactly what it is that our end-user wants from the interaction, and also the precise value of the thing that is wanted. In our example, not only did our agent recognize the request to order an entree, it also was able to find out which entree was being requested. This ability to anticipate and parse valuable information out of user input, which is inherently variable, can give us an advantage insofar as we are able to deal with a wide range of interactions via just a single interface, that of our agent. Allowing such a wide range of services to be rendered from just a single component can help to simplify the structure of our application while still retaining maximum flexibility in how we respond to user input.