【问题标题】:How to add session attributes in google assistant如何在谷歌助手中添加会话属性
【发布时间】:2018-03-22 07:02:52
【问题描述】:
我想在 api 请求中添加一些属性以进一步使用它们。我正在发送这样的请求:
$data = array(
"source" => "My text",
"speech" => "My text",
"displayText" =>"My text",
"contextOut" => array()
)
header('Content-Type: application/json');
echo json_encode($data);
如何将我自己的自定义参数添加到此请求中?
【问题讨论】:
标签:
actions-on-google
dialogflow-es
【解决方案1】:
由于您自己处理 JSON,因此最好的方法是在 Context 中添加您想要的参数。此上下文将在上下文的生命周期(用户请求数)内发送回您的 webhook。您可以随时重新发送上下文并延长其寿命,或者只是将其设置为较长的寿命。上下文只适用于同一会话 - 它们不跨越对话。
您可以创建一个上下文并将其发送到您的回复中,如下所示:
$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
)
在您的请求中,您将在result.contexts 的数组中查找提取的 JSON 正文中的值。