ogrethegreat / api-ai-php
API.ai php 开发工具包
0.2.5
2016-08-20 08:27 UTC
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ^6.2
This package is not auto-updated.
Last update: 2024-09-20 19:24:56 UTC
README
这是一个为 Api.ai 开发的非官方 php 开发工具包,仍在开发中...
Api.ai: Build brand-unique, natural language interactions for bots, applications and devices.
安装
通过 composer
$ composer require iboldurev/api-ai-php
用法
使用低级 Client
require_once __DIR__.'/vendor/autoload.php'; use ApiAi\Client; try { $client = new Client('access_token'); $query = $client->get('query', [ 'query' => 'Hello', ]); $response = json_decode((string) $query->getBody(), true); } catch (\Exception $error) { echo $error->getMessage(); }
用法
使用低级 Query
require_once __DIR__.'/vendor/autoload.php'; use ApiAi\Client; use ApiAi\Model\Query; use ApiAi\Method\QueryApi; try { $client = new Client('access_token'); $queryApi = new QueryApi($client); $meaning = $queryApi->extractMeaning('Hello', [ 'sessionId' => '1234567890', 'lang' => 'en', ]); $response = new Query($meaning); } catch (\Exception $error) { echo $error->getMessage(); }
对话
Dialog
类提供了一个简单的方式来使用 query
api 并自动执行链式步骤
首先,您需要创建一个 ActionMapping
类来自定义动作的行为。
namespace Custom; class MyActionMapping extends ActionMapping { /** * @inheritdoc */ public function action($sessionId, $action, $parameters, $contexts) { return call_user_func_array(array($this, $action), array($sessionId, $parameters, $contexts)); } /** * @inheritdoc */ public function speech($sessionId, $speech, $contexts) { echo $speech; } /** * @inheritdoc */ public function error($sessionId, $error) { echo $error; } }
然后在 Dialog
类中使用它。
require_once __DIR__.'/vendor/autoload.php'; use ApiAi\Client; use ApiAi\Method\QueryApi; use ApiAi\Dialog; use Custom\MyActionMapping; try { $client = new Client('access_token'); $queryApi = new QueryApi($client); $actionMapping = new MyActionMapping(); $dialog = new Dialog($queryApi, $actionMapping); // Start dialog .. $dialog->create('1234567890', 'Привет', 'ru'); } catch (\Exception $error) { echo $error->getMessage(); }
一些示例可以在 iboldurev/api-ai-php-example 仓库中找到。