【问题标题】:https Request in Alexa SkillAlexa Skill 中的 https 请求
【发布时间】:2019-03-07 22:21:00
【问题描述】:

我似乎无法让 HTTP 请求在我的 alexa 技能中工作,这里是相关的示例代码:

var https = require('https');

...

function getTreeFact(callbackFunction){


  var url = 'https://alexa.phl.chs.network/treefacts/index.php';

  https.get(url, function(res){
      var body = '';

      res.on('data', function(chunk){
          body += chunk;
      });

      res.on('end', function(){
          var gameResponse = JSON.parse(body);
          callbackFunction(gameResponse);
      });
  }).on('error', function(e){
    // Handle error
  });
}

...

this.getTreeFact(function (responseMessage){
    this.emit(':tell', responseMessage.message);
});

我不知道我做错了什么,我认为我正确地发出了 HTTP 请求。我知道没有这个技能可以工作(只需注释掉最后三行并用 this.emit(':tell', 'hello') 替换就可以了)。

【问题讨论】:

    标签: node.js https alexa alexa-skills-kit


    【解决方案1】:

    让我解释一下我下面的代码...这里我在“LaunchRequest”中使用了 https 请求 这反过来又给了我一个回应,有了这个回应,我就让我的 alexa 说话了

    注意:jsonplaceholder.typicode.com 对于测试你的 https 请求和响应非常有用

    只需在您的 aws 在线编辑器控制台中使用此代码,确保您键入正确的意图名称和调用名称。

        exports.handler = (event, context, callback) => {
            var speechResult;
        switch (event.request.type) {
    
             case "LaunchRequest":
                 var resultis;
                 const querystring = require('querystring');                                                                                                                                                                                                
                 const https = require('https');
    
    
                var postData = querystring.stringify({
                        'msg' : 'Hello World!'
                });
    
         var options = {
            hostname: 'jsonplaceholder.typicode.com',
            path: '/posts',
            method: 'POST',
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Content-Length': postData.length
         }
     };
    
          var req = https.request(options, (res) => {
           //console.log('statusCode:', res.statusCode);
          //console.log('headers:', res.headers);
    
         res.on('data', (d) => {
             //console.log("my data is "+d);
             var obj = JSON.parse(d);
             var resul = obj.msg;
             resultis = JSON.stringify(resul);
             context.succeed(generateResponse(buildSpeechletResponse(resultis, true)));
         });
     });
    
        req.on('error', (e) => {
            console.error(e);
        });
         req.write(postData);
            req.end();
         break;
         case "IntentRequest":
              switch (event.request.intent.name) {
            case "MyIntent":
                 var a = "are you ready";
                 context.succeed(generateResponse(buildSpeechletResponse(a, true)))
                 break;
               }
          break;
         }
      }
           //Alexa Speech function
    
          buildSpeechletResponse = (outputText, shouldEndSession) => {
                return {
                  outputSpeech: {
                     type: "PlainText",
                     text: outputText
                  },
                  shouldEndSession: shouldEndSession
              }
        }
            generateResponse = (speechletResponse) => {
                  return {
                      version: "1.0",
                      response: speechletResponse
               }
         }
    

    【讨论】:

      【解决方案2】:

      Alexa 的官方 github 页面有关于 api 调用的非常详尽的文档。检查他们的烹饪列表技能文档,它涵盖了所有的获取和发布请求https://github.com/alexa/alexa-cookbook

      https://github.com/alexa/ 其他示例的主仓库。

      【讨论】:

      • 我尝试使用他们拥有的代码,但它仍然给我同样的错误。我以为这是我正在访问的网站,但仅使用 google.com 也不起作用……我只是希望它实际上会显示错误消息
      • 我尝试在浏览器上使用您的代码访问该网站,但它似乎已关闭,尝试 google.com 将无法正常工作,因为它不会输出 JSON。尝试使用 this link 进行测试
      【解决方案3】:

      你最好使用promise版本,例如

      https://github.com/request/request-promise

      【讨论】:

        【解决方案4】:

        在我看来是这样的

        this.emit(':tell', responseMessage.message);
        

        应该是

        this.emit(':tell', responseMessage);
        

        我在此看不到任何.message

        var gameResponse = JSON.parse(body);
        
        callbackFunction(gameResponse);
        

        【讨论】:

          猜你喜欢
          • 2018-10-13
          • 2018-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多