tgallice / wit-php
Wit.ai php SDK
0.3.1
2016-08-10 14:37 UTC
Requires
- php: >=5.5.0
- guzzlehttp/guzzle: ^6.2
Requires (Dev)
- phpspec/phpspec: ~2.0
This package is not auto-updated.
Last update: 2024-09-14 18:26: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存储库中找到。