【问题标题】:Calling a REST API from a Lambda function从 Lambda 函数调用 REST API
【发布时间】:2018-05-15 18:30:45
【问题描述】:

我正在尝试构建一种调用 REST API 来获取数据的技能。我正在使用 HelloWorld 示例并对其进行修改以满足我的需要。我正在使用请求节点 (node.js) 来发出请求。

但是,对于我来说,我无法让它工作。我在日志中看到调用了该函数并且返回了正确的结果,但是发送给 Alexa 的响应是空的!!知道我缺少什么吗?

const HelloWorldIntentHandler = {
  canHandle(handlerInput) {
    console.log("HelloWorldIntentHandler 1: ");
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
  },
  handle(handlerInput) {
    console.log("HelloWorldIntentHandler 2");
    var speechText = 'Hello World';

    Request.get(url, function(error, response, body) {
      console.log("I'm here")
      var data = JSON.parse(body)
      var result = data.records.totalNum
      if (result > 0) {
          speechText = "There are " + result + " matches";
      } else {
          speechText = "ERROR";
      }

      return handlerInput.responseBuilder
        .speak(speechText)
        .withSimpleCard('Hello World', speechText)
        .getResponse();
     });
  },
};

日志中的错误是

Error handled: speechOutput.trim is not a function

【问题讨论】:

    标签: alexa alexa-skills-kit alexa-skill


    【解决方案1】:

    我能够使用 Axios 而不是 Request 来让它工作。

    const HelloWorldIntentHandler = {
      canHandle(handlerInput) {
        console.log("HelloWorldIntentHandler 1: ");
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
          && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
      },
      async handle(handlerInput) {
        console.log("HelloWorldIntentHandler 2");
        var speechText = 'default';
    
        try {
          const response = await Axios.get(url);
          var result = response.data.totalRecs;
          if (result > 0) {
              speechText = "There are " + result + " matches";
          } else {
              speechText = "ERROR";
          }
          console.log("text=" + speechText);
          return handlerInput.responseBuilder
            .speak(speechText)
             .withSimpleCard('Hello World', speechText)
             .getResponse();
        } catch (error) {
          console.log(error);
        }
      },
    };
    

    【讨论】:

      【解决方案2】:

      对于它的价值,我遇到了同样的问题,并发现(困难的方式)我从我的 api 调用返回的响应包含 未正确格式化为 SSML 的文本 em>(在我的例子中,返回的字符串包含一个“&”)。

      所以我发现这个库似乎不仅有帮助,而且如果你不能 100% 确定你的结果,通常是一个好主意。

      见:https://www.npmjs.com/package/ssml-builder

      希望这对某人有所帮助。

      ~罗伯

      我想我最好也添加一个代码示例。这未经测试,但使用我在其中提到的库时,您的代码可能看起来像 ^^^。

      const Speech = require( 'ssml-builder' );
      
      const HelloWorldIntentHandler = {
          canHandle(handlerInput) {
              console.log("HelloWorldIntentHandler 1: ");
              return handlerInput.requestEnvelope.request.type === 'IntentRequest'
                     && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
          },
          handle(handlerInput) {
              console.log("HelloWorldIntentHandler 2");
              let speechText = 'Hello World';
      
              Request.get(url, function(error, response, body) {
                  console.log("I'm here");
                  let data = JSON.parse(body);
                  let result = data.records.totalNum;
                  if (result > 0) {
      
                      let speech = new Speech();
                      speech.say(`There are ${result} matches`);
                      speechText = speech.ssml(true);
      
                  } else {
                      speechText = "ERROR";
                  }
      
                  return handlerInput.responseBuilder
                                     .speak(speechText)
                                     .withSimpleCard('Hello World', speechText)
                                     .getResponse();
              });
          },
      };
      

      【讨论】:

        【解决方案3】:

        将您的 return 放入 if else 语句中。

          Request.get(url, function(error, response, body) {
            console.log("I'm here")
            var data = JSON.parse(body)
            var result = data.records.totalNum
        
            if (result > 0) {
              speechText = "There are " + result + " matches";
              return handlerInput.responseBuilder
                .speak(speechText)
                .withSimpleCard('Hello World', speechText)
                .getResponse();
              });
            } else {
              speechText = "ERROR";
              return handlerInput.responseBuilder
                 .speak(speechText)
                 .withSimpleCard('Hello World', speechText)
                 .getResponse();
              });
            }
          } 
        

        这将强制您的处理程序在您的 if else 语句中返回结果。

        【讨论】:

        • 我试过了,也没用。在向 alexa 发送响应时,我不确定如何进行异步调用。
        猜你喜欢
        • 2018-07-28
        • 2023-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多