这是我第一次和机器人打交道。我决定使用PHP,使用wit.ai。我要做的是为bot设置回调函数,例如,当用户询问机器人是否会执行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 --它总是给出错误:
致命错误:在第12行的/Users/jack/Documents/www/bar/index.php中找不到类'ActionMapping‘
我的代码:
<?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');发布于 2017-01-27 12:19:42
根据你的错误,你确定这些事情:
安装库:
首先,通过composer安装库,以便所有文件都能正确添加。也许这将是主要原因。
以卷曲形式传递的参数:
对于惠特,您已经使用了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
}https://stackoverflow.com/questions/41738467
复制相似问题