My code is not running, can anybody help. Unable to speak out the text, can i return handler input response. The test function is a http call which may take tieme.
function test(url, number)
{
return 5;
}
function speak(handlerInput) {
return handlerInput.responseBuilder
.getResponse();
}
const NumberFactIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'NumberFactIntent';
},
handle(handlerInput) {
const theNumber = handlerInput.requestEnvelope.request.intent.slots.number.value;
const repromptOutput = " Would you like another fact?";
const URL = "http://numbersapi.com";
test(URL, theNumber).then(function (data) {
console.log("data is " + data);
handlerInput.responseBuilder
.speak("Test Data")
.reprompt(repromptOutput)
return speak(handlerInput);
}).catch(function (data) {
console.log("error is " + data);
handlerInput.responseBuilder
.speak(`I wasn't able to find a fact for ${theNumber}` )
.reprompt(repromptOutput)
return speak(handlerInput);
});
}
};
First of all your
testfunction doesn't return a promise. I don't know if this is intentional and you just cut api calling code to make it simpler, but it should return a promise if you want to usethenon it.If it returns a promise in your full example, then what are you missing is adding a return before
test. Also you should returnhandlerInputfrom inside of your promise. Code should look like this (i'll remove some of the code, that is not relevant):Now you might be wondering why do you need those
return's. This is because JS functions implicitly returnsundefined, so in this case you have to explicitly tell what should be returned fromhandlefunction. Same applies to inside of the promise.