google助手中如何添加session属性

How to add session attributes in google assistant

我想向 api 请求添加一些属性以进一步使用它们。我正在发送这样的请求:

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

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

如何向此请求添加我自己的自定义参数?

由于您要自己处理 JSON,因此最好的方法是在 Context 中添加您想要的参数。此上下文将在上下文的生命周期(用户请求数)内发送回您的 webhook。您可以 re-send 上下文并随时延长其寿命,或者将其设置为较长的​​寿命。上下文仅适用于同一会话 - 它们不会跨越对话。

您可以创建上下文并将其发送到您的回复中,如下所示:

$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 正文中的值。