使用 HTTP API 跨域检索 Wit.ai 机器人文本响应
Retrieving a Wit.ai Bot Text Response using the HTTP API Over Cross Domain
我目前正在与 Wit.ai 合作 webpage in CodePen.io。我想知道是否可以使用 HTTP API.
检索 Wit.ai 机器人的文本响应 ("Bot says")
例如:如果用户要提示机器人:
How's it going?
我希望在使用此消息进行 API 调用后,能够检索机器人的典型响应:
I am well, thank you human.
我已浏览 HTTP API Documentation。这似乎是使用 'POST' 到 api.wit.ai/converse 完成的。 JSON 响应包含一个 "msg" 字段,这正是我所需要的!这是我目前使用的代码:
$.ajax({
url: baseUrl + "converse",
data: {
'q': text, // The message to send the bot
'session_id': "123abc",
'access_token' : accessToken // Authorisation key for using our bot
},
dataType: 'json',
crossDomain: true,
method: 'POST',
success: function(data) {
prepareResponse(data);
},
error: function() {
respond(messageInternalError);
}
});
但是,Wit.Ai暂时不支持CORS,跨域请求的唯一方法是使用JSONP,它只适用于GET请求。正如所料,上面的代码会导致 HTTP 400 错误。
谁能确认是否可以使用 HTTP API 检索机器人对用户消息的文本响应?我目前正在做的事情有变通办法吗?
同样的问题,
我确实找到了解决方法,因为我是从我的 nodejs 应用程序调用它的。
运行 改为卷曲,
这是代码。
var sys = require('util')
var exec = require('child_process').exec;
var child;
child = exec("curl -XPOST 'https://api.wit.ai/converse?v=20160330&session_id=123abc&q=screw%20http%20api%20calls' \ -H \"Content-Type: application/json\" \ -H \"Accept: application/json\" \ -H 'Authorization: Bearer $token'", function (error, stdout, stderr) {
res.send(stdout)
//sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
});
我最近遇到了同样的问题。为了成功地进行对话,我必须先创建一个本地服务器,它将处理所有用户请求并将它们发送到 Wit 的服务器。
已使用 node.js
、express
、nodemon
和 sync-request
。
这是 server.js 文件:
var express = require('express')
var request = require('sync-request');
var port = 8001;
var app = express()
app.get('/converse', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
var message = req.query.q;
var sessionID = req.query.session_id;
var response = request('POST', 'https://api.wit.ai/converse?q=' + message + '&session_id=' + sessionID, {
'headers': {
'Content-Type': 'application/json; charset=utf8',
"Accept": "application/json",
"Authorization": "Bearer $place-app-token"
}
});
res.send(JSON.parse(response.getBody('utf8')));
})
app.listen(port);
服务器将发送 POST
请求,其中包含 Wit 需要的 headers
配置。
用户将向本地服务器发送GET
请求。按照文档中的建议使用 jQuery
:https://wit.ai/docs/http/20160526#cross-domain-link
这是 front-end 请求:
$.ajax({
url: 'http://127.0.0.1:8001/converse',
data: {
'q': text,
'session_id': sessionID
},
method: 'GET',
crossDomain: true,
success: function(response) {
console.log(response, response.msg);
}
});
我目前正在与 Wit.ai 合作 webpage in CodePen.io。我想知道是否可以使用 HTTP API.
检索 Wit.ai 机器人的文本响应 ("Bot says")例如:如果用户要提示机器人:
How's it going?
我希望在使用此消息进行 API 调用后,能够检索机器人的典型响应:
I am well, thank you human.
我已浏览 HTTP API Documentation。这似乎是使用 'POST' 到 api.wit.ai/converse 完成的。 JSON 响应包含一个 "msg" 字段,这正是我所需要的!这是我目前使用的代码:
$.ajax({
url: baseUrl + "converse",
data: {
'q': text, // The message to send the bot
'session_id': "123abc",
'access_token' : accessToken // Authorisation key for using our bot
},
dataType: 'json',
crossDomain: true,
method: 'POST',
success: function(data) {
prepareResponse(data);
},
error: function() {
respond(messageInternalError);
}
});
但是,Wit.Ai暂时不支持CORS,跨域请求的唯一方法是使用JSONP,它只适用于GET请求。正如所料,上面的代码会导致 HTTP 400 错误。
谁能确认是否可以使用 HTTP API 检索机器人对用户消息的文本响应?我目前正在做的事情有变通办法吗?
同样的问题, 我确实找到了解决方法,因为我是从我的 nodejs 应用程序调用它的。 运行 改为卷曲, 这是代码。
var sys = require('util')
var exec = require('child_process').exec;
var child;
child = exec("curl -XPOST 'https://api.wit.ai/converse?v=20160330&session_id=123abc&q=screw%20http%20api%20calls' \ -H \"Content-Type: application/json\" \ -H \"Accept: application/json\" \ -H 'Authorization: Bearer $token'", function (error, stdout, stderr) {
res.send(stdout)
//sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
});
我最近遇到了同样的问题。为了成功地进行对话,我必须先创建一个本地服务器,它将处理所有用户请求并将它们发送到 Wit 的服务器。
已使用 node.js
、express
、nodemon
和 sync-request
。
这是 server.js 文件:
var express = require('express')
var request = require('sync-request');
var port = 8001;
var app = express()
app.get('/converse', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
var message = req.query.q;
var sessionID = req.query.session_id;
var response = request('POST', 'https://api.wit.ai/converse?q=' + message + '&session_id=' + sessionID, {
'headers': {
'Content-Type': 'application/json; charset=utf8',
"Accept": "application/json",
"Authorization": "Bearer $place-app-token"
}
});
res.send(JSON.parse(response.getBody('utf8')));
})
app.listen(port);
服务器将发送 POST
请求,其中包含 Wit 需要的 headers
配置。
用户将向本地服务器发送GET
请求。按照文档中的建议使用 jQuery
:https://wit.ai/docs/http/20160526#cross-domain-link
这是 front-end 请求:
$.ajax({
url: 'http://127.0.0.1:8001/converse',
data: {
'q': text,
'session_id': sessionID
},
method: 'GET',
crossDomain: true,
success: function(response) {
console.log(response, response.msg);
}
});