MyGit

iboldurev/dialogflow

Fork: 63 Star: 165 (更新于 2024-05-26 01:31:32)

license: MIT

Language: PHP .

Unofficial php sdk for Dialogflow

最后发布版本: 0.3.0 ( 2017-10-28 03:45:03)

官方网址 GitHub网址

DialogFlow PHP sdk

version Downloads

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

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

Install:

Via composer:

$ composer require iboldurev/dialogflow

Usage:

Using the low level Client:

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

use DialogFlow\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 DialogFlow\Client;
use DialogFlow\Model\Query;
use DialogFlow\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();
}

Usage

Using the low level asynchronous api:

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

use DialogFlow\Client;
use DialogFlow\Model\Query;
use DialogFlow\Method\QueryApi;
use GuzzleHttp\HandlerStack;
use React\EventLoop\Factory;
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter;

$loop = Factory::create();
$reactGuzzle = new \GuzzleHttp\Client([
    'base_uri' => Client::API_BASE_URI . Client::DEFAULT_API_ENDPOINT,
    'timeout' => Client::DEFAULT_TIMEOUT,
    'connect_timeout' => Client::DEFAULT_TIMEOUT,
    'handler' => HandlerStack::create(new HttpClientAdapter($loop))
]);

$client = new Client('bc0a6d712bba4b3c8063a9c7ff0fa4ea', new DialogFlow\HttpClient\GuzzleHttpClient($reactGuzzle));
$queryApi = new QueryApi($client);

$queryApi->extractMeaningAsync('Hello', [
    'sessionId' => '123456789',
    'lang' => 'en'
])->then(
    function ($meaning) {
        $response = new Query($meaning);
    },
    function ($error) {
        echo $error;
    }
);

$loop->run();

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 DialogFlow\Client;
use DialogFlow\Method\QueryApi;
use DialogFlow\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();
}

最近版本更新:(数据更新于 2024-06-01 17:19:24)

2017-10-28 03:45:03 0.3.0

2016-08-20 17:03:18 0.2.5

主题(topics):

ai, api, api-client, apiai

iboldurev/dialogflow同语言 PHP最近更新仓库

2024-07-03 02:11:59 laravel/laravel

2024-06-26 20:49:20 snipe/snipe-it

2024-06-12 14:59:19 doctrine/dbal

2024-06-11 19:29:09 coollabsio/coolify

2024-05-31 16:22:53 symfony/var-dumper

2024-05-31 16:19:47 symfony/symfony