Wit.ai PHP cURL执行bot功能?

时间:2017-01-19 09:44:03

标签: php curl bots

这是我第一次使用机器人。我决定使用wit.ai bot,使用PHP。我想要做的是为机器人设置回调函数,例如当用户要求天气时机器人将执行getWeather()。如果我使用cURL,如何将此功能传递给机器人?有可能吗?我在git上找到了一些SDK,但是对于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;

我发现了这个SDK https://github.com/tgallice/wit-php,但我无法使用ActionMapping它总是给出错误:

  

致命错误:Class' ActionMapping'找不到   第12行/Users/jack/Documents/www/bar/index.php

我的代码:

   <?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');

1 个答案:

答案 0 :(得分:4)

根据你的错误,你确定这些事情:

  

安装库:

首先,通过composer安装库,以便正确添加所有文件。也许这将是主要原因。

  

curl中传递的参数:

对于wheater,您使用了https://wit.ai/docs/http/20160526 API。此外,令牌部分缺少卷曲

并点击此功能:

$ 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
  }