raulmangolin / wit-php
Wit.ai php SDK
1.0.0
2021-02-02 16:41 UTC
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: *
Requires (Dev)
- phpspec/phpspec: ~2.0
This package is auto-updated.
Last update: 2024-09-29 05:56:18 UTC
README
这是Wit.ai的非官方php SDK,仍在开发中...
Wit.ai: Easily create text or voice based bots that humans can chat with on their preferred messaging platform.
## 安装
通过composer
$ composer require tgallice/wit-php
使用方法
使用低级Client
require_once __DIR__.'/vendor/autoload.php'; use Tgallice\Wit\Client; $client = new Client('app_token'); $response = $client->get('/message', [ 'q' => 'Hello I live in London', ]); // Get the decoded body $intent = json_decode((string) $response->getBody(), true);
你可以使用Message API类来提取句子的意义
require_once __DIR__.'/vendor/autoload.php'; use Tgallice\Wit\Client; use Tgallice\Wit\MessageApi; $client = new Client('app_token'); $api = new MessageApi($client); $meaning = $api->extractMeaning('Hello I live in London');
会话
Conversation类提供了一个简单的方式来使用converse API并自动执行链式步骤
首先,你需要创建一个ActionMapping类来定制动作行为。
namespace Custom; use Tgallice\Wit\Model\Step\Action; use Tgallice\Wit\Model\Step\Message; class MyActionMapping extends ActionMapping { /** * @inheritdoc */ public function action($sessionId, Context $context, Action $step) { return call_user_func_array(array($this, $step->getAction()), array($sessionId, $context)); } /** * @inheritdoc */ public function say($sessionId, Context $context, Message $step) { echo $step->getMessage(); } .... }
然后在Conversation类中使用它。
require_once __DIR__.'/vendor/autoload.php'; use Tgallice\Wit\Client; use Tgallice\Wit\ConverseApi; use Tgallice\Wit\Conversation; use Custom\MyActionMapping; $client = new Client('app_token'); $api = new ConverseApi($client); $actionMapping = new MyActionMapping(); $conversation = new Conversation($api, $actionMapping); $context = $conversation->converse('session_id', 'Hello I live in London');
Conversation::converse()返回最后一个可用的Context。
一些示例可以在tgallice/php-wit-example仓库中找到。