官术网_书友最值得收藏!

Creating your first context

In this section, you will learn how to incorporate contexts into intents.

The following flowchart shows the conversational flow that you will learn to build for FaqChatBot, which is asking about the Presidents' birthdays:

President Birthday Context flow diagram
  1. In FaqChatBot, you will find the list of intents created by Dialogflow: Default Fallback Intent and Default Welcome Intent. The following screenshot shows the default welcome and fallback intents:
Default welcome and fallback intents
  1. The Default Welcome Intent is triggered the very first time the user uses FaqChatBot. Modify the Default Welcome Intent by changing the text response to Hi! Welcome to FAQ Chatbot. What is your name? so that the user will be greeted and asked what his or her name is. The following screenshot shows the modified Default Welcome Intent text response:
Modified default welcome intent text response
  1. Create Get Name and, in the Contexts section, type in GET_NAME in the Add output context textbox. GET_NAME can now be used as the input context in other intents, allowing other intents to access all the parameters captured by the Get Name intent.
  2. From the @sys.given-name:username template or my name is @sys.given-name:username, the intent will extract the username @sys.given-name:username and store it in the $username variable.
  3. Create a personalized text response using the user's name stored in the $username variable, $username you can ask me anything about the president of America. The following image shows the Get Name intent, which gets triggered when the user responds with his or her name: 
Get name intent context and user says

The following screenshot shows the username variable, which contains the captured username found in the Action section of the Get Name intent:

Get name intent action and response
Notice that there is a number 5 next to the GET_NAME context. This number defines the lifespan of the context. The number 5 means that the context will be available for the next 5 requests or for the next 10 minutes after the time context is activated. You can simply increase the lifespan number by editing the number to something greater. This will all depend on the use cases for the context.
  1. Before we can start creating the intent that can use the GET_NAME context, let's create a new entity for the Presidents' birthdays, called president-birthday, using JSON.

The following code shows the president-birthday entity:

[
{
"value": "December 14, 1799",
"synonyms": [ "George Washington" ]
},
{
"value": "October 30, 1735",
"synonyms": [ "John Adams" ]
}
]
  1. Once the president-birthday entity has been created, you can create the Get President Birthday intent, as shown in the following screenshot. Notice that, in the Contexts section, there is the GET_NAME context as the input context, which comes from the output context of the Get Name intent. By adding the GET_NAME context as the input, the Get President Birthday intent has access to all the parameters in the Get_Name intent.
  2. In the Action section, you can add the parameter called username, which maps to the parameter from the Get Name intent, #GET_NAME.username.

The following screenshot shows the get President Birthday intent, which shows the username variable mapping to #GET_NAME.username:

Get President Birthday intent
  1. You can create the text response using the $username variable, making the response more personable by using $username $president-birthday.original's birthday is on $president-birthday.

Contexts are a very powerful way to persist data across requests, but they come with drawbacks. First, if the intent contains the context, the intent will only get triggered if the request contains the required context and what the user says matches the intent template. For example, in the case of the Get President Birthday intent, the request must contain the GET_NAME context, and what the user says must also match the what is his birthday? or I want to know @president-birthday:president-birthday template. Lastly, the context lifespan can be limited to 10 minutes or the context can only be available through n number of requests. But an expiring context is not really ideal in a real-world application because you would not want to ask a user their name every time they use the voice application. In fact, you would want to recognize the user and greet them by their name. In Chapter 4Hosting, Securing, and Testing Fortune Cookie in the Cloud, you will learn how to manage your own context without relying on Dialogflow contexts by creating your own persistence.

主站蜘蛛池模板: 伊吾县| 香格里拉县| 怀化市| 高陵县| 汾西县| 江永县| 天柱县| 奎屯市| 伊通| 天镇县| 夏河县| 馆陶县| 勃利县| 南阳市| 四会市| 玉溪市| 措勤县| 灵寿县| 灵寿县| 卓尼县| 宣威市| 乐昌市| 叙永县| 海南省| 宜兴市| 海口市| 焦作市| 门源| 河池市| 吴桥县| 新化县| 兴城市| 三门峡市| 饶阳县| 马鞍山市| 大港区| 永新县| 通海县| 迁西县| 金溪县| 介休市|