【发布时间】:2017-07-24 18:42:00
【问题描述】:
我正在尝试开发 Alexa 技能,该技能可以找到用户所说的示例句子。我找到了这个 API(WordAPI),虽然当我打电话时,响应是未定义的。有人可以帮忙吗?
我的代码:
'use strict';
var Alexa = require('alexa-sdk');
var appId = 'this is valid';
var unirest = require('unirest');
var APP_STATES = {
START: "_STARTMODE",
TRANSLATE: "_TRANSLATE"
}
function getData(word){
unirest.get("https://wordsapiv1.p.mashape.com/words/" + word)
.header("X-Mashape-Key", "my key")
.header("Accept", "application/json")
.end(function (result) {
return JSON.parse(result.body);
});
}
exports.handler = function(event, context, callback){
var alexa = Alexa.handler(event, context);
alexa.appId = appId;
alexa.registerHandlers(newSessionHandlers, startStateHandler, translateStateHandler);
alexa.execute();
}
var newSessionHandlers = {
'LaunchRequest': function(){
this.handler.state = APP_STATES.START;
this.emitWithState("BeginState", true);
},
'Unhandled': function () {
this.emit(":tell", "Something went wrong");
},
}
var startStateHandler = Alexa.CreateStateHandler(APP_STATES.START, {
'BeginState': function(){
var message = "You will say a word and I will give you facts about it, would you like to continue ?";
this.emit(":ask", message, message);
},
'AMAZON.YesIntent': function(){
this.handler.state = APP_STATES.TRANSLATE;
this.emit(":ask", "Great, say a word !");
},
'AMAZON.NoIntent': function(){
this.emit(":tell", "Ok, see you later !");
},
'Unhandled': function () {
this.emit(":tell", "Something went wrong");
},
});
var translateStateHandler = Alexa.CreateStateHandler(APP_STATES.TRANSLATE, {
'GetWordIntent': function(){
var word = this.event.request.intent.slots.word.value;
console.log(getData(word));
this.emit(":tell", "You said " + word);
},
'Unhandled': function () {
this.emit(":tell", "Something went wrong");
},
});
当我尝试使用 console.log 功能时会出现问题。它返回未定义。
'GetWordIntent': function(){
var word = this.event.request.intent.slots.word.value;
console.log(getData(word));
this.emit(":tell", "You said " + word);
},
原始函数应该从调用返回解析数据。
function getData(word){
unirest.get("https://wordsapiv1.p.mashape.com/words/" + word)
.header("X-Mashape-Key", "my key")
.header("Accept", "application/json")
.end(function (result) {
return JSON.parse(result.body);
});
}
这还处于开发的早期阶段,我正在尝试 console.log 输出。这可能是一些我看不到的愚蠢错误。我替换了 appId 和 API 密钥。 API有效,我在其他场景中检查过。
任何线索或提示将不胜感激。
【问题讨论】:
-
目前的形式还不清楚。你能指出究竟是什么调用出错了,什么是未定义的?
-
好的,请稍等。我将编辑原始帖子。