Skip to content

beshoo/wit-php

 
 

Repository files navigation

Wit.ai PHP sdk

Scrutinizer Code Quality Code Coverage Build Status

This is an unofficial php sdk for Wit.ai and it's still in progress...

Wit.ai: Easily create text or voice based bots that humans can chat with on their preferred messaging platform.

## Install:

Via composer:

$ composer require tgallice/wit-php

Usage:

Using the low level 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);

You can used the Api class which provided some shortcut to call the wit api:

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

use Tgallice\Wit\Client;
use Tgallice\Wit\Api;

$client = new Client('app_token');
$api = new Api($client);

$intent = $api->getIntentByText('Hello I live in London');

Conversation

The Conversation class provides an easy way to use the converse api and execute automatically the chaining steps :

First, you need to create an ActionMapping class to customize the actions behavior.

namespace Custom;

class MyActionMapping extends ActionMapping
{
    /**
     * @inheritdoc
     */
    public function action($sessionId, $actionName, Context $context)
    {
        return call_user_func_array(array($this, $actionName), array($sessionId, $context));
    }

    /**
     * @inheritdoc
     */
    public function say($sessionId, $message, Context $context)
    {
        echo $message;
    }

     ....
}

And using it in the Conversation class.

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

use Tgallice\Wit\Client;
use Tgallice\Wit\Api;
use Tgallice\Wit\Api\Conversation;
use Custom\MyActionMapping;

$client = new Client('app_token');
$api = new Api($client);
$actionMapping = new MyActionMapping();
$conversation = new Conversation($api, $actionMapping);

$context = $conversation->converse('session_id', 'Hello I live in London');

Conversation::converse() return the last available Context.

Some examples are describe in the tgallice/php-wit-example repository.

TODO

  • Create dedicated Api class for Intent
  • Create dedicated Api class for Entity
  • Response Model

About

Wit.ai php sdk

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%