Gowri
Gowri

Reputation: 1856

How to add session attributes in google assistant

I want to add some attributes to api request to use them in further. I'm sending request like this:

$data = array(
     "source" =>  "My text",
     "speech" =>  "My text",
     "displayText" =>"My text",
     "contextOut" => array()
 )

header('Content-Type: application/json');
echo json_encode($data);

How do I add my own custom parameters to this request?

Upvotes: 1

Views: 521

Answers (1)

Prisoner
Prisoner

Reputation: 50701

Since you're handling the JSON yourself, the best way to do this is to add the parameters you want in a Context. This Context will be sent back to your webhook for the lifespan (number of user requests) of the Context. You can re-send the Context and extend its life at any point, or just set it to a large lifespan. Contexts are only good for the same session - they don't span conversations.

You can create a context and send it in your reply with something like this:

$context = array(
    "name" => "my-context",
    "lifespan" => 99,
    "parameters" => array(
        "parameter_one" => "value_one",
        "parameter_two" => "value_two"
    )
);

$contexts = [$context];

$data = array(
     "source" =>  "My text",
     "speech" =>  "My text",
     "displayText" =>"My text",
     "contextOut" => $contexts
 )

In your requests, you'd look for the value in the extracted JSON body in an array at result.contexts.

Upvotes: 2

Related Questions