在 DialogFlow 中使用 PHP fulfillment 时如何请求 name/location 权限?

How do I ask for name/location permissions when using PHP fulfillment in DialogFlow?

在 DialogFlow 中,我构建了一个带有指向 PHP 脚本的 fulfillment webhook 的代理。现在,我需要获取用户的位置。

我可以在 this page about helpers 上看到,有一些函数可以调用 askForPermission 或 askForPermissions。如果我在 javascript 中使用 Google 框架上的 Actions 构建它,这些在应用程序框架内工作。

PHP是否有类似的支持?我找不到关于它的任何文档。

我的 fullfilments 目前是这样工作的,在计算 $speech、$displayText 和 $contextOut 的值后,每个操作都以:

$response = Array(
  'speech' => $speech,
  'displayText' => $displayText,
  'contextOut' => $contextOut    // when necessary
);

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

我想我需要将 'data' 元素附加到 $response,但是触发权限请求的语法是什么?

你是对的,你需要附加一个 "data" 元素,其中包含带有请求信息的嵌套元素。如果您单击 helper is documented 所在的 JSON 选项卡,则会记录此内容,但如果您使用的是 Action SDK,也会出现这种情况。如果您使用的是 Dialogflow(看起来像您),那么您的回复需要稍微修改一下。

您需要添加一个包含权限请求的 richResponse containing a dummy text response, along with a systemIntent 对象。它需要是这样的:

$response = Array(
  'speech' => 'PLACEHOLDER',
  'data' => Array(
    'google' => Array(
      'richResponse' => Array(
        'items' => Array(
          Array(
            'simpleResponse' => Array(
              'textToSpeech' => 'PLACEHOLDER'
            )
          )
        )
      ),
      'systemIntent' => Array(
        'intent' => 'actions.intent.PERMISSION',
        'data' => Array(
          '@type' => 'type.googleapis.com/google.actions.v2.PermissionValueSpec',
          'optContext' => 'To locate you',
          'permissions' => Array(
            'DEVICE_PRECISE_LOCATION'
          )
        )
      )
    )
  )
);