davidniki02 / api-ai-php
API.ai php SDK
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 20:51:50 UTC
README
这是对Api.ai非官方php SDK的分支 - iboldurev/api-ai-php
在这个分支中,我们增加了创建、更新和删除意图的功能。具体来说,它现在更好地支持POST查询,以及PUT和DELETE动词。
Api.ai: Build brand-unique, natural language interactions for bots, applications and devices.
安装
通过composer
$ composer require davidniki02/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(); }
使用上下文创建一个示例意图
private function createIntent($context, $issue, $response){ try { $client = new \ApiAi\Client('<developer token>'); $query = $client->post('intents', [ "name" => "issue name", "auto" => true, "templates" => [ $issue ], "contexts" => [ $context ], "userSays" => [ [ "data" => [ ["text" => $issue] ], "isTemplate" => false, "count" => 0 ], ], "responses" => [ ["speech" => $response] ] ]); $r = json_decode((string) $query->getBody(), true); if ($r['status']['code'] != "200") return false; return $r; } catch (\Exception $error) { return false; } }
用法
使用低级 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(); }
一些示例可以在davidniki02/api-ai-php-example 存储库中找到。