Coder Social home page Coder Social logo

dialogflow's Introduction

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

dialogflow's People

Contributors

aleplusplus avatar cthulhuden avatar iboldurev avatar pattyland avatar pilot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dialogflow's Issues

Cant access returned Object property

HI i am using an example, i wan to extract the fulfilment and speech for output, i have tried var_dump which is showing object is private and i am unable to access any of property

i.e $response->result->fulfilment;

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

Implement proper Webhook models

Hi,

I've been browsing this library for the last couple of days trying to understand how to implement webhooks handling, without much luck.

I made a PR in order to add Webhook Request and Response models.

How to get speech response ?

Hi @iboldurev ,

I'm Rizal , based on my response below, how i'm going to fetch the red circle data ? it is speech under fulfillment ? FYI, i'm using PHP to decode it ? attached is my code snippet.

$client = new Client('xxx'); $queryApi = new QueryApi($client); $meaning = $queryApi->extractMeaning('Hello', [ 'sessionId' => '1234567890', 'lang' => 'en', ]); $response = new Query($meaning);

response

vendor/autoload.php is missing from sources?

I'm trying to use this package without composer's assistance.

Should I "require" each and every file separately?
Also, when doing that, the code complains that GuzzleHttp is missing - is this a separate package I should download and require?

thanks in advance,
Nimrod.

400 Bad Request` response

Hello Guys,
I just tried out the PHP SDK, used composer to install and when i try to use the first sample code which is

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

use ApiAi\Client;

try {
    $client = new Client('MY_CLIENT_ACCESS_TOKEN');

    $query = $client->get('query', [
        'query' => 'Hello',
    ]);

    $response = json_decode((string) $query->getBody(), true);
} catch (\Exception $error) {
    echo $error->getMessage();
}

i am getting the following error:

Client error: GET https://api.api.ai/v1/query?v=20150910&lang=en&query=Hello` resulted in a 400 Bad Request response: { "id": "abb5553d-69d6-41ba-bfba-cadc777536a8", "timestamp": "2016-11-30T09:29:32.553Z", "status": { "code": 4 (truncated...)`

Did the signature change/sdk needs to be updated because using POSTMAN i am able to query perfectly fine.

Workflow question

Hey,

thx for this package. Tested it and works great.
What I am not sure about is how the workflow should be.

In my teste I sent some text to my api.ai agent and I got some info from the json file back.
So the workflo would be:

messenger app -> sends text to my app -> I make a request to api.ai -> result from api.ai -> send back to messenger app

What I have read about api.ai it seems they use webhooks. This sounds like after using a service the result is sent back to api.ai so the workflow looks like:

messenger app -> sends text to my app -> I make a request to api.ai -> api.ai makes a call to my service (e.g get weather info) -> data gets back to api.ai -> then ?

So how do you handle webhook calls? Is that possible with your package? Am I getting this right? Was my problem clear ?=)

Thanks and greets

400 Bad request

Hey @iboldurev ,

today it started given me this error:

GuzzleHttp\\Exception\\ClientException: Client error: `GET https://api.api.ai/v1/query?v=20150910&query=fsdfds` resulted in a `400 Bad Request

When I try to query a simple message like:

 $query = $this->apiClient->get('query', [
      'query' => $message,
]);

I checked the apiai token and my code and I'm not sure why I get this error. Can you help me?

Thx Christoph

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.