Search code examples
actions-on-googlegoogle-homedialogflow-es

How to design stateful conversation with Dialogflow


I'm trying to make an app to reserve meeting rooms in my office by Google Home and Dialogflow. Here's my plan:

Me: "OK Google, is there any available room now?"
Google Home: "Room 1 is available until 16:00."
Me:  "Book it."
Google Home: "Booked Room 1."

The current problem is how to make Google Home's response stateful. In my plan, when I say "Book it", Google Home has to remember Room 1. But I don't know how to make it happen.

I read documents of conversation API, but I haven't understand it's possible to preserve variables or states within the same conversation Id. https://developers.google.com/actions/reference/v1/conversation

Does anyone know about that?


Solution

  • It is absolutely possible, and even fairly easy, to preserve state as part of the conversation your users have with your Action. Dialogflow makes it particularly easy with what they call context, and it uses this in a few ways:

    1. As part of your fulfillment, you can set a Context, the lifetime (number of steps in the conversation) that context will be good for, and any parameter/value pairs for this context. Using your example, when you have the Action replying with the room number and time, you might set a "pending_request" context with the pair {"room": "1"} and {"time": "2017-11-15T16:00Z"} and a lifetime of 5.

    2. You can indicate as part of the Intent what Contexts must be set for that Intent to be selected in the conversation. So asking "Who is available?" while the "pending_request" context is active might trigger an Intent that looks who is available at that time that can meet at that room (perhaps because they're in the same building). But if the context is not active, it might trigger an Intent to see who is available right now that you can call (even if they're in a different building).

    3. The parameters that you set in the Context are available to you in the Intent that is called. So you'll be able to find out what room and time have been set in the fulfillment of the Intent.

    4. If you don't renew the Context, it will vanish after the selected number of exchanges. This means that after you inquire about the room, you could inquire if you have any appointments today (a question unrelated to the room or the time) and the phrase "Book it" would still have the context available to it.

    If you're using the node.js client library from Google, you can use app.getContext() and app.setContext(). If you're doing it in JSON, you need to provide the Context information directly in the response.

    Google also provides a more general app.data object that you can set properties on with the node.js client library, and these properties are retained during a conversation (although not between conversations). It uses Contexts behind the scenes, although it isn't quite as powerful as Contexts are since you can't use it as part of Intent matching.

    (As an aside - the link you provided was to version 1 of the API. That version has been deprecated and will be turned off in May 2018. It was also for the Actions API rather than Dialogflow. The equivalent documentation is at https://developers.google.com/actions/reference/rest/conversation-webhook, but that probably isn't what you want if you're using Dialogflow.)