【发布时间】:2016-09-20 17:21:32
【问题描述】:
我正在尝试构建一个简单的 Alexa 技能,以使用 [Node.js ASK] (https://developer.amazon.com/public/community/post/Tx213D2XQIYH864/Announcing-the-Alexa-Skills-Kit-for-Node-js) 从 API 返回数据。我已将 http get 放在处理程序中,但 Alexa 在回调异步返回 API 数据之前完成了处理程序。
我一直在寻找答案,目前我的想法是:
- 不使用 node.js
- 想办法同步获取数据
- 我缺少一些简单的东西
代码核心:
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.registerHandlers(handler);
alexa.execute();
};
var handler = Alexa.CreateStateHandler(states.x, {
'intent': function() {
var options = {
host: baseURL,
path: pathURL
};
callback = function(response) {
var str = "";
response.on('data', function(piece) {
str += piece;
});
response.on('end', function() {
//does not get executed
this.emit(':tell', str, "test");
});
}
http.request(options, callback).end();
//this does get executed if I leave this here
this.emit(':tell'...);
};
【问题讨论】:
标签: alexa-skills-kit