Alexa API skill - nodejs get request not executing

Mikmac

I'm working on my first Alexa skill and, as a starting point, would like Alexa to state data retrieved from a simple GET request (see lambda function below). For some reason, however, the request does not actually seem to be executing - nothing from inside request.get() is printing to the console and speechOutput is 'Outside Request' after the handler executes. I'm also new to looking through CloudWatch logs and have not been able to find any information about the network requests to even know if this is being attempted. Any help here would be welcome!

'use strict';
//Required node packages
const alexa = require('./node_modules/alexa-sdk');
const request = require('request');
// var https = require('https')

//this is the handler, when the lambda is invoked, this is whats called
exports.handler = function (event, context, callback) {
  const skill = alexa.handler(event, context);

  skill.appId = '<app_id>';
  skill.registerHandlers(handlers);
  skill.execute();
};

//Alexa handlers
const handlers = {
  'LaunchRequest': function () {
    console.log("inside of LaunchRequest");
    const speechOutput = "Hello from NASA!";
    this.response.speak(speechOutput).listen(speechOutput);
    this.emit(':responseReady');
  },

  //Entering our main, part finding function
  'GetAPOD': function () {
    const intent_context= this
    const speechOutput = getData()
    intent_context.response.speak(speechOutput).listen(speechOutput);
    intent_context.emit(':responseReady');

  },

  'Unhandled': function (){
    console.log("inside of unhandled");
    const speechOutput = "I didn't understand that.  Please try again";
    this.response.speak(speechOutput).listen(speechOutput);
    this.emit(':responseReady');

  }
};

const getData = function() {
  const url = "https://api.nasa.gov/planetary/apod?api_key=<key>"
  console.log("inside get data")
  request.get(url, function (error, response, body) {
    console.log("inside request")
    console.log('error', error) //Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
    console.log('body:', body); // Print the HTML for the Google homepage.
    return "complete request"
    return body
  });

  return "outside request"
}
Daniel Costello

I have found in the past that such API requests will get clobbered because they are not synchronous, like David stated. To resolve this, I have had to tuck the request in a promise to get it to resolve, something similar to this in your case:

Change your function to contain the promise:

function getData = function() {
 const url = "https://api.nasa.gov/planetary/apod?api_key=<key>"
 console.log("inside get data")
    return new Promise(function(resolve, reject) {
        request.get(url, function (error, response, body) {
            if (err) {
                reject(err);
            }

            if (body) {
                resolve(JSON.parse(body));
            }
        });
    });
}

Then change your intent handler to use the promise:

   //Entering our main, part finding function
  'GetAPOD': function () {
    getData()
   .then(function(body) {
    let speechOutput = body;
    intent_context.response.speak(speechOutput).listen(speechOutput);
    intent_context.emit(':responseReady');
    }

Something along these lines. You would need to play with it a bit to make sure the results are produced as you intend. Hope this helps. D

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Alexa skill Rest API

Wikipedia's API not working in Alexa skill using Request module

Nodejs - Get the instance_url from salesforce oauth response in Amazon Alexa skill or an API to get the instance_url

Alexa Skill NodeJS - Combine speak and addAudioPlayerPlayDirective

Problems in nodejs callback in custom alexa skill

Alexa Skill Certification - Validating amazon request

Alexa Skill An exception occurred while dispatching the request to the skill

Fetch to alexa skill activation api does not work

Alexa, get skill from LaunchRequest Intent

Alexa Skill: Get user location for "work"

Alexa Skill - Is possible to get what user said?

Alexa Skill request deserialization fails - json to SkillRequest object C#

Alexa Skill Messaging API vs Alert/Remainder API

Alexa skill that uses data from an external API with API-KEY

Alexa skill "Sorry, I am unable to fulfill your request on this device" In skill purchase on iOS device

Querying own skill for development purposes on Alexa through API calls

How to get the account info of the user when the user uses an Alexa Skill

Amazon Alexa Skill Lambda Node JS - Http GET not working

How to get and use confirmation 'yes' or 'no' for Alexa skill intent response

How to get amazon user email with Java in Alexa Skill

Why is this ajax post request executing a GET request with the data right before the post request is executed in Nodejs express?

Using sessionAttributes in Alexa Skill

Amazon alexa skill development

Alexa smarthome skill routines

Simple Alexa skill with logging

Amazon Polly in Alexa Skill?

Alexa skill development

Alexa not finding my Skill

Alexa Skill with Google OAuth