【问题标题】:Alexa API skill - nodejs get request not executingAlexa API 技能 - nodejs 获取请求未执行
【发布时间】:2018-05-08 19:58:51
【问题描述】:

我正在编写我的第一个 Alexa skill,作为起点,我希望 Alexa 声明从简单的 GET 请求中检索到的数据(请参阅下面的 lambda 函数)。然而,由于某种原因,该请求实际上似乎并未执行 - request.get() 内部没有任何内容打印到控制台,并且在处理程序执行后,speechOutput 是“外部请求”。我也是查看 CloudWatch 日志的新手,并且无法找到有关网络请求的任何信息,甚至无法知道是否正在尝试这样做。欢迎在这里提供任何帮助!

'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"
}

【问题讨论】:

  • 您将getData() 视为同步和阻塞,但两者都不是。您观察到的行为实际上与您编写的代码相匹配。

标签: node.js request aws-lambda httprequest alexa-skills-kit


【解决方案1】:

我过去发现此类 API 请求会被破坏,因为它们不是同步的,就像 David 所说的那样。为了解决这个问题,我不得不将请求放入一个承诺中以解决它,在你的情况下类似于这样:

更改您的函数以包含承诺:

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));
            }
        });
    });
}

然后更改您的意图处理程序以使用承诺:

   //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');
    }

类似的东西。您需要稍微尝试一下,以确保按您的预期产生结果。希望这可以帮助。 D

【讨论】:

  • 谢谢 - 这工作!只是在函数声明中将“错误”更改为“错误”
  • 太棒了!如果您使用自己的技能查询/更新 dynamoDb,这也是必需的。
  • 更新这个答案,因为我写了这个我发现了王朝。看看这个。它简化了这个过程并提供了 Promise 支持。更容易!它是我现在使用的:dynastyjs.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多