Monday, January 30, 2017

Wit.ai PHP cURL execute bot function?

Leave a Comment

It is my first time working with bots. I decided to use wit.ai bot, using PHP. What I am trying to do is to set callback function for the bot, for example when user ask for weather the bot will execute getWeather(). How can I pass this function to the bot if I am using cURL? Is it possible to do that? I found some SDKs on git but all of them are unofficial for wit.ai.

$ch = curl_init(); $headr = array(); $headr[] = "Authorization: Bearer XXXXXXXXXXXXXXXX";  curl_setopt($ch, CURLOPT_URL,"https://api.wit.ai/message?v=20170118&q=what is weather in London ?"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  $server_output = curl_exec ($ch);  curl_close ($ch);  echo $server_output; 

i've found this SDK https://github.com/tgallice/wit-php, but i am not able to use ActionMapping its always given error :

Fatal error: Class 'ActionMapping' not found in /Users/jack/Documents/www/bar/index.php on line 12

my code :

   <?php     require_once __DIR__.'/vendor/autoload.php';      use Tgallice\Wit\Client;     use Tgallice\Wit\ConverseApi;     use Tgallice\Wit\Conversation;      use Tgallice\Wit\Model\Step\Action;     use Tgallice\Wit\Model\Step\Message;      class MyActionMapping extends ActionMapping     {         /**          * @inheritdoc          */         public function action($sessionId, Context $context, Action $step)         {             return call_user_func_array(array($this, $step->getAction()), array($sessionId, $context));         }          /**          * @inheritdoc          */         public function say($sessionId, Context $context, Message $step)         {             echo $step->getMessage();         }      }      $client = new Client('XXX');     $api = new ConverseApi($client);     $actionMapping = new MyActionMapping();     $conversation = new Conversation($api, $actionMapping);  $context = $conversation->converse('session_id', 'Hello I live in London'); 

2 Answers

Answers 1

According to your error, are you sure about these things:

Install library:

First of all, install library via composer so that all files should add correctly. Maybe it will be the main cause.

Parameter passed in curl:

For wheater, you have used https://wit.ai/docs/http/20160526 API. Also here token part is missing form curl

and hit this function :

$ curl -XPOST 'https://api.wit.ai/converse?v=20160526&session_id=123abc&q=weather%20in%20Brussels' \       -H "Content-Type: application/json" \       -H "Accept: application/json" \       -H 'Authorization: Bearer $TOKEN' Response:    {     "type": "merge",     "entities": {"location": [{"body": "Brussels",                                "value": {"type": "value",                                          "value": "Brussels",                                          "suggested": true},                                "start": 11,                                "end": 19,                                "entity": "location"}]},     "confidence": 1   } 

Answers 2

The error clearly tells you the problem,
The ActionMapping that you are extending is not found, so you will need to import it:
add this use Tgallice\Wit\ActionMapping;

require_once __DIR__.'/vendor/autoload.php';  use Tgallice\Wit\Client; use Tgallice\Wit\ConverseApi; use Tgallice\Wit\Conversation; use Tgallice\Wit\ActionMapping; use Tgallice\Wit\Model\Step\Action; use Tgallice\Wit\Model\Step\Message;  class MyActionMapping extends ActionMapping {     /**      * @inheritdoc      */     public function action($sessionId, Context $context, Action $step)     {         return call_user_func_array(array($this, $step->getAction()), array($sessionId, $context));     }      /**      * @inheritdoc      */     public function say($sessionId, Context $context, Message $step)     {         echo $step->getMessage();     }  }  $client = new Client('XXX'); $api = new ConverseApi($client); $actionMapping = new MyActionMapping(); $conversation = new Conversation($api, $actionMapping);  $context = $conversation->converse('session_id', 'Hello I live in London'); 

By the way if you took some time to explore some examples you would notice that.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment