Wit.ai php sdk

维护者

详细信息

github.com/garissman/wit

源代码

安装: 2

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 0

分支: 16

1.0 2024-08-29 20:52 UTC

This package is auto-updated.

Last update: 2024-09-06 20:07:30 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 garissman/wit

用法

使用低级Client

require_once __DIR__.'/vendor/autoload.php';

use Garissman\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 Garissman\Wit\Client;
use Garissman\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 Garissman\Wit\Model\Step\Action;
use Garissman\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 Garissman\Wit\Client;
use Garissman\Wit\ConverseApi;
use Garissman\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