1
votes

I can't seem to get HTTP requests to work in my alexa skill, here is the relevant sample code:

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

I have no idea what I am doing wrong, I think I am making the HTTP request correctly. I know the skill works without this (simply commenting out the last three lines and replacing with just this.emit(':tell', 'hello') works fine).

4

4 Answers

1
votes

Let me explain my below code...Here I have used https request in the "LaunchRequest" which in turns gives me a response and with that response and I'm making my alexa to speak

note: jsonplaceholder.typicode.com is very useful for testing your https req and response

Simply use this code in your aws online editor console make sure you type the right intent name and invocation name.

    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
           }
     }
0
votes

Alexa's official github page has a very thorough documentation on api calls. Check their Cooking list skill documentation it covers all of the get and post requests https://github.com/alexa/alexa-cookbook

and https://github.com/alexa/ main repo for other samples.

0
votes
0
votes

To me it looks like

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

should be

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

I can't see any .message in this

var gameResponse = JSON.parse(body);

callbackFunction(gameResponse);