I have created an Alexa skill, which needs to fetch data from an API.
Following the sample aws-nodejs-factskill
, I have created the skill:
exports.handler = async function (event, context) {
console.log(`REQUEST++++${JSON.stringify(event)}`);
if (!skill) {
skill = Alexa.SkillBuilders.custom()
.addRequestHandlers(
customHandler,
HelpHandler,
ExitHandler,
FallbackHandler,
SessionEndedRequestHandler,
)
.addErrorHandlers(ErrorHandler)
.create();
}
return skill.invoke(event, context);
};
const customHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'customIntent');
},
handle(handlerInput) {
const myRequest = "size=1&lat=<lat>&long=<lon>";
var speechOutput = "Default Message";
callAPI(myRequest, (myResult) => {
console.log("sent : " + myRequest);
console.log("received : " + JSON.stringify(myResult));
speechOutput = JSON.stringify(myResult).replace(/['"]+/g, '');
return handlerInput.responseBuilder
.speak(speechOutput)
.withSimpleCard(SKILL_NAME, speechOutput)
.getResponse();
});
},
};
But the response back to Alexa is null, and the session is being ended with an error.
{
"body": {
"version": "1.0",
"response": {},
"sessionAttributes": {},
"userAgent": "ask-node/2.0.7 Node/v8.10.0"
}
Lambda
Session ended with reason: ERROR
The response from the API call is fine. And I am able to see the response in the console.
If I use the same code and return builder outside, it's working fine.
const customHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'customIntent');
},
handle(handlerInput) {
const myRequest = "size=1&lat=<lat>&long=<lon>";
var speechOutput = "Default Message";
locateNearestDealer(myRequest, (myResult) => {
console.log("sent : " + myRequest);
console.log("received : " + JSON.stringify(myResult));
speechOutput = JSON.stringify(myResult).replace(/['"]+/g, '');
});
return handlerInput.responseBuilder
.speak(speechOutput)
.withSimpleCard(SKILL_NAME, speechOutput)
.getResponse();
},
};
What exactly am I doing wrong and how do I fix this?