20steps / api-ai-webhook-php
PHP 的 API AI Webhook 接口
dev-master
2017-08-16 09:53 UTC
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ~4.6
This package is not auto-updated.
Last update: 2024-09-29 03:41:49 UTC
README
这个库提供了一种方便的接口,用于开发 API AI 和 Google Assistant 的 Webhook。
信息
工作中。
功能
- 基本的序列化/反序列化
- 对 Google Assistant 的富响应的基本支持
用法
通过 composer 安装: composer require 20steps/api-ai-webhook-php
。
请求
当 API AI 触发您的 webhook 时,将会向您为应用程序指定的 URL 发送一个 HTTP 请求。
您可以通过以下方式获取请求的 JSON
主体
$rawData = $request->getContent(); // This is how you would retrieve this with Laravel or Symfony 2. $request = new \APIAI\Request\Request($rawData); $request = $request->fromData();
库期望原始请求数据,而不是解析后的 JSON,因为它需要验证请求签名。
您可以使用 instanceof
来确定请求类型,例如
if ($apiaiRequest instanceof IntentRequest) { // Handle intent here }
响应
您可以使用 Response
类构建 APIAI 响应。您可以可选地设置显示文本或添加基本的 Google Assistant 卡片。
这里有一些示例。
$response = new \APIAI\Response\Response('my-assistant'); $response->respond('Cooool. I\'ll lower the temperature a bit for you!') ->withDisplayText('Temperature decreased by 2 degrees') ->withCard('My card title','My formatted text')
要输出响应,只需使用 ->render()
函数即可,例如在 Laravel 中,您将创建响应如下
return response()->json($response->render());
在原生 PHP 中
header('Content-Type: application/json'); echo json_encode($response->render()); exit;