Git Product home page Git Product logo

api-ai-php's Introduction

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.

api-ai-php's People

Contributors

aleplusplus avatar iboldurev avatar pilot avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.