【问题标题】:AWS Lambda not able to make REST call to external APIAWS Lambda 无法对外部 API 进行 REST 调用
【发布时间】:2017-08-10 12:12:20
【问题描述】:

我正在使用 nodeJS 代码使用请求模块进行休息调用。我也使用了回调函数,但是请求函数没有被执行。

我的流程转到函数 searchTSTData,但请求方法没有得到执行。

从回调函数中,我只得到了我在 searchTSTData 函数中初始化的 responseString = 'Yet to make query rest'。它不会根据 API 返回的响应进行更新,该响应应该是错误或成功响应字符串。

我已将模块包含在 zip 中,因为 lambda 不会引发错误并通过测试。 此外,我确信请求模块无法像在 Cloudwatch 日志中那样工作,我没有看到我在请求中写入的任何 console.logs。

请指出我哪里出错了。我是 NodeJS 的新手。

这里是代码-

'use strict';
const request = require('request');
const Alexa = require('alexa-sdk');
const APP_ID = 'amzn1.ask.skill.80a49cf5-254c-123a-a456-98745asd21456';  

const languageStrings = {
    'en': {
        translation: {
            TST: [
                'A year on Mercury is just 88 days long.',
            ],
            SKILL_NAME: 'TEST',
            GET_TST_MESSAGE: "Here's your TST: You searched for ",
            HELP_MESSAGE: 'You can say get me a TST, or, you can say exit... What can I help you with?',
            HELP_REPROMPT: 'What can I help you with?',
            STOP_MESSAGE: 'Goodbye!',
        },
    },
};

const handlers = {
    'LaunchRequest': function () {
        this.emit('GetTST');
    },
    'GetNewTSTIntent': function () {
        this.emit('GetTST');
    },
    'GetTST': function () {
        // Get a random space fact from the space facts list
        // Use this.t() to get corresponding language data
        const inputValue = this.event.request.intent.slots.Search.value;
        var finalResponse = "Some error occurred in code. Please try again later.";
        console.log('Input recieved as '+inputValue);

        searchTSTData(inputValue, function (response){
        console.log('trying to call');
                        finalResponse = response;                                                    
         });

         console.log("after function call");

        // Create speech output
        const speechOutput = this.t('GET_TST_MESSAGE')  + inputValue+". Here are the results " +finalResponse;
        this.emit(':tellWithCard', speechOutput, this.t('SKILL_NAME'), speechOutput);
    },
    'AMAZON.HelpIntent': function () {
        const speechOutput = this.t('HELP_MESSAGE');
        const reprompt = this.t('HELP_MESSAGE');
        this.emit(':ask', speechOutput, reprompt);
    },
    'AMAZON.CancelIntent': function () {
        this.emit(':tell', this.t('STOP_MESSAGE'));
    },
    'AMAZON.StopIntent': function () {
        this.emit(':tell', this.t('STOP_MESSAGE'));
    },
};

exports.handler = function (event, context) {
    const alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    // To enable string internationalization (i18n) features, set a resources object.
    alexa.resources = languageStrings;
    alexa.registerHandlers(handlers);
    alexa.execute();
};


function searchTSTData(searchString,callback){
    var responseString = 'Yet to make query rest';

    request({
    url: 'https://api.google.com/getresultsInJson',
    method: 'GET'
    }, function (error, response, body) {
            if (error) {
                responseString = 'Error received from rest api. Please try again after some time.';
                } else if(response.statusCode===200){
                responseString = 'Sucess Success';
                }else{
                responseString = 'Nothing is working'; 
                }
            });
            callback(responseString);
           }

【问题讨论】:

标签: node.js amazon-web-services request aws-lambda alexa


【解决方案1】:

您的 lambda 方法是否在 VPC 中?检查这个http://docs.aws.amazon.com/lambda/latest/dg/vpc.html你需要给它外部访问权限

【讨论】:

    【解决方案2】:

    当您调用 this.emit() 时,Alexa-SDK 会结束您的 lambda 事件循环。

    在您的示例中,您调用的是异步工作的request()。在调用request()(通过searchTSTData())之后,您会立即发出this.emit(),这会结束事件循环并使响应未处理。

    为了处理您的回复,您希望保持对this.emit() 的呼叫:

    const handlers = {
      'GetTST': function () {
        searchTSTData(inputValue, (response) => {
          const speechOutput = response;
          this.emit(':tell', speechOutput);
        });
      }
    };
    

    在此处阅读有关 nodejs 下 lambda 函数的编程模型:http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

    【讨论】:

      猜你喜欢
      • 2022-10-23
      • 2021-05-24
      • 2018-08-12
      • 2020-11-21
      • 2018-08-13
      • 1970-01-01
      • 2021-02-08
      • 2021-07-29
      • 1970-01-01
      相关资源
      最近更新 更多