I am trying to develop Alexa skill, which would find sample sentences to the word user says. I found API for this (WordAPI), although when I am making a call, response is undefined. Can someone help ?
My code:
'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");
},
});
The problem occurs when I am trying to console.log function. It returns undefined.
'GetWordIntent': function(){
var word = this.event.request.intent.slots.word.value;
console.log(getData(word));
this.emit(":tell", "You said " + word);
},
Original function should return parsed data from the call.
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);
});
}
This is in really early stage of development, I am trying to console.log output. It is probably some silly mistake which I can not see. I replaced appId and API key. API works, I checked it in other scenario.
Any clue or tip would be very appreciated.
you don't return any value from your getData
function
try
function getData(word){
return 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);
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.