1
votes

I'm writing my first NodeJS app for Google Home (using DialogFlow - formerly API.ai).

I'm looking at the doc on this page: https://developers.google.com/actions/reference/v1/dialogflow-webhook

but I don't see any way to set session variables.

My current test program sets speech like this:

 speechText = "I'm not sure that character exists!";
 callback(null, {"speech": speechText});

In DialogFlow, my JSON after running looks like this, and it looks like maybe the "contexts" is where the session state would go?

{
    "id": "3a66f4d1-830e-48fb-b72d-12711ecb1937",
    "timestamp": "2017-11-24T23:03:20.513Z",
    "lang": "en",
    "result": {
        "source": "agent",
        "resolvedQuery": "test word",
        "action": "MyAction",
        "actionIncomplete": false,
        "parameters": {
            "WordNumber": "400"
        },
        "contexts": [],
        "metadata": {
            "intentId": "a306b829-7c7a-46fb-ae1d-2feb1c309124",
            "webhookUsed": "true",
            "webhookForSlotFillingUsed": "false",
            "webhookResponseTime": 752,
            "intentName": "MyIntentName"
        },
        "fulfillment": {
            "messages": [{
                "type": 0,
                "speech": ""
            }]
        },
        "score": 1
    },
    "status": {
        "code": 200,
        "errorType": "success",
        "webhookTimedOut": false
    },
    "sessionId": "fe0b7d9d-7a55-45db-9be9-75149ff084fe"
}

I just noticed from a chat bot course that I bought that you can set up Contexts like this, but still not sure exactly how the contexts get set and passed back and forth between the response of one call of my program to the request in the next call of my program (defined via "webhook"). enter image description here When I added the contexts above, DialogFlow wouldn't recognize my utterance any longer and was giving me the DefaultFallback response. When I remove them, my AWS Lambda get's called.

1

1 Answers

1
votes

For starters, the documentation page you're looking at refers to a deprecated version of the API. The page that talks about the current version of the api (v2) is https://developers.google.com/actions/dialogflow/webhook. The deprecated version will only be supported for another 6 months or so.

You're on the right track using Contexts! If you were using Google's actions-on-google node.js library, there would be some additional options - but they all use Contexts under the scenes. (And since they do use Contexts under the scenes - you should make sure you pick Context names that are different from theirs.) You can also use the sessionId and keep track of things in a local data store (such as DynamoDB) indexed against that SessionID. But enough about other options...

A Context consists of three elements:

  1. A name.
  2. A lifetime - for how many messages from the user will this context be sent back to you. (But see below about re-sending contexts.)
  3. An object of key-value strings.

You'll set any contexts in the JSON that you return as an additional parameter named contextOut. This will be an array of contexts. So your response may look something like this:

var speechText = "I'm not sure that character exists!";
var sessionContext = {
  name: "session_variables",
  lifespan: 5,
  parameters: {
    "remember": "one",
    "something": "two"
  }
};
var contextOut = [sessionContext];
var response = {
  speech: speechText,
  contextOut: context
};
callback(null, response);

This will include a context named "session_variables" that stores two such variables. It will be returned for the next 5 messages sent to your webhook. You can, however, add this to every message you send, and the latest lifetime and parameters will be the ones that are sent back next time.

You'll get these contexts in the JSON sent to you in the result.contexts array.

The "Context" field on the Intent screen is used for an additional purpose in Dialogflow beyond just preserving session information. This indicates that the Intent is only triggered if the specified Context exists (lifetime > 0) when the phrase tries to be matched with it (or when handling a fallback intent). If you're using a webhook, the "Context Out" field is ignored if you send back contexts yourself.

This lets you do things like ask a particular question and set a Context (possibly with parameters) to indicates that some answers should be understood as being replies to the question you just asked.