【问题标题】:Actions on Google Fulfillment Response对 Google Fulfillment Response 的操作
【发布时间】:2017-07-21 09:14:36
【问题描述】:

当您收到请求时,您的履行必须返回响应

我有一个 HTTPS 端点,它接收通过谷歌助手发送的命令(我的 Fulfillment URL)。但我想为每个发出的请求向用户返回一个文本

例如: 用户请求:“告诉‘应用名称’来做等等等等”
助理回答:“好的,当然”

如本文所述 --> https://developers.google.com/actions/components/fulfillment (RESPONSE FORMAT) ,我已根据上面链接中所说的格式对 json 文件进行了编码 但它说您的履行必须返回响应

响应.JSON

"finalResponse": {
     "richResponse": {
          "items": [
               {
                 "simpleResponse": {
                 "textToSpeech": "sure thing",
                 "displayText": "Sure, thing!"
                  }
           ]
     }
}

在我的 Fulfillment 终点,我执行了上述操作。

fulfillment.php

$file = [
 "expectUserResponse" => false,
 "finalResponse" => [
 "richResponse" => [
  "items" => [
    [
      "simpleResponse" => [
        "textToSpeech" => "Sure thing!",
        "displayText" => "Sure, thing?"
        ]
      ]
     ]
    ]
  ]
];

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

如何在 php 中将此文件返回给谷歌助手? 我正在使用 PHP :)

【问题讨论】:

  • 将您的 $data 变量设为关联数组。通过使用浏览器访问上述 URL 来验证响应。
  • 如何把它变成一个关联数组?我需要上述格式的json文件
  • 请阅读this。是的,json_encode 会将关联数组转换为 json 对象。你自己检查过回复吗?

标签: php actions-on-google


【解决方案1】:

对于初学者 - 这不是有效的 JSON。有效的 JSON 看起来像这样:

{
    "finalResponse": {
        "richResponse": {
            "items": [{
                "simpleResponse": {
                    "textToSpeech": "sure thing",
                    "displayText": "Sure, thing!"
                }
            }]
        }
    }
}

注意左方括号和右方括号表示这是一个 JSON 对象。您发送的是一个已编码为 JSON 字符串的字符串。使用 PHP,创建有效 JSON 的最简单方法是使用 json_encode 并将嵌套关联数组传递给它。所以像这样的东西会生成它。

<?php
header('Content-Type: application/json');
$a = [
  "finalResponse" => [
    "richResponse" => [
      "items" => [
        [
          "simpleResponse" => [
            "textToSpeech" => "Sure thing!",
            "displayText" => "Sure, thing?"
          ]
        ]
      ]
    ]
  ]
];
echo json_encode($a);

注意header()必须在任何东西之前,即使是空行,已经发送。

但是...这可能不是您唯一的问题。

这看起来并不包含您应该回复的所有字段。如果您只发送finalResponse,那么您可能还需要将expectUserResponse 设置为false。

(你确定每次都结束对话吗?)

【讨论】:

  • 我用您的 awnser 更新了代码,但助手仍然说“APP NAME,现在没有响应,请稍后再试”是的,我想暂时结束对话以进行测试。
  • 更新答案以包含有关 header() 的信息
  • 我也这样做了,结果一样。我访问了 php 页面,也没有任何 php 错误。我仍然收到“APP NAME,现在没有响应,请稍后再试”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 2022-08-16
  • 1970-01-01
相关资源
最近更新 更多