3
votes

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?

1
Found answer through amazon developer forums , i have to return promise instead of handlerInput for async. Please refer to this github.com/alexa/skill-sample-nodejs-city-guide , it is a perfect example of async http/https API consumption using ask sdkv2.amit2091

1 Answers

2
votes

You should make the handle function as asynchronous function as it needs to wait for the api response, here is an example:

const HelloWorldIntentHandler = {
canHandle(handlerInput) {
    return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
        && Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
},
async handle(handlerInput) {
const response = await httpGet();
return handlerInput.responseBuilder
        .speak('Example')
        .getResponse();
}}

function httpGet() {
  return new Promise((resolve, reject) => {
  request.get(URL, (error, response, body) => {
    console.log('error:', error); 
    console.log('statusCode:', response.statusCode);
    resolve(JSON.parse(body));
  });
});
}