Unofficial php sdk for Api.ai
PHP
Latest commit 5c1110c Oct 10, 2016 @iboldurev committed on GitHub Merge pull request #4 from aleplusplus/master
Add more info to README.md
Permalink
Failed to load latest commit information.
src Each query should have language Aug 19, 2016
.gitignore Initial commit Jun 21, 2016
LICENSE Initial commit Jun 21, 2016
README.md Add more info to README.md Oct 9, 2016
composer.json Fix: namespace to ApiAi (composer psr-4) Jun 28, 2016

README.md

Api.ai PHP sdk

version Downloads

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

Api.ai: Build brand-unique, natural language interactions for bots, applications and devices.

Install:

Via composer:

$ composer require iboldurev/api-ai-php

Usage:

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

Usage:

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

The Dialog class provides an easy way to use the query 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, $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;
    }
}

And using it in the Dialog class.

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();
}

Some examples are describe in the iboldurev/api-ai-php-example repository.