简体   繁体   中英

Make Alexa Skill Speak From JSON file via HTTP Work Properly

if I call the doHttp function it get the data in the log without an issue. I just cant seem to get the data to return and be spoken allowed. I'm using visual studio code using nodejs. I am rather new to this, so I know i'm missing something.

const url = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY";

const linkIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'linkIntent';
 },
handle(handlerInput) {

var data = doHttp();
var speechText = data;

return handlerInput.responseBuilder
  .speak(speechText)
  .withSimpleCard('Card title', speechText)
  .getResponse();
  },
};


function doHttp() {
  var data2 = '';
  https.get(url, (resp) => {
    let data = '';
    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
      data += chunk;
    });
    // The whole response has been received. Print out the result.
    resp.on('end', () => {
      data2 = JSON.parse(data).title;
    });
  }).on("error", (err) => {
    console.log("Error: " + err.message);
  });
  return data2;
 }


//Working function
function doHttp() {
  https.get(url, (resp) => {
    let data = '';
    // A chunk of data has been recieved.
    resp.on('data', (chunk) => {
      data += chunk;
    });
    // The whole response has been received. Print out the result.
    resp.on('end', () => {
      console.log(JSON.parse(data).title);
    });
  }).on("error", (err) => {
    console.log("Error: " + err.message);
  });
}

The HTTP request is an asynchronous function, your code won't wait for the response to come. You can wrap the http functon call inside a promise and return it. Then can apply async/await in handle input function. Below is a sample code using weather API.

const url =
  "https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22";

const linkIntentHandler = {
  canHandle(handlerInput) {
    return (
      handlerInput.requestEnvelope.request.type === "IntentRequest" &&
      handlerInput.requestEnvelope.request.intent.name === "linkIntent"
    );
  },
  async handle(handlerInput) {
    const data = await doHttp();
    console.log("data in handle input ", data);
    const speechText = data;
    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .withSimpleCard(speechText, speechText)
      .getResponse();
  }
};

function doHttp() {
  var data2 = "";
  return new Promise((resolve, reject) => {
    https
      .get(url, resp => {
        let data = "";
        // A chunk of data has been recieved.
        resp.on("data", chunk => {
          data += chunk;
        });
        // The whole response has been received. Print out the result.
        resp.on("end", () => {
          console.log("data ", data);
          data2 = JSON.parse(data).weather[0].description;
          console.log("weather ", data2);
          resolve(data2);
        });
      })
      .on("error", err => {
        console.log("Error: " + err.message);
      });
  });
}

Use the below link to know more about making external API call. https://developer.amazon.com/blogs/alexa/post/4a46da08-d1b8-4d8e-9277-055307a9bf4a/alexa-skill-recipe-update-call-and-get-data-from-external-apis

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM