alexa-js / alexa-app-server

An Alexa app server for alexa-app.
MIT License
401 stars 116 forks source link

How can i make a http request? #75

Closed NullP0interEx closed 7 years ago

NullP0interEx commented 7 years ago

Can you please add an example skill with a http request? I want to query an API but i think my way is not so perfect.

`var alexa = require('alexa-app'); var async = require('async'); var request = require('request');

// Allow this module to be reloaded by hotswap when changed module.change_code = 1;

// Define an alexa-app var app = new alexa.app('hello_world');

async.parallel([ function(callback) { var options = { url: 'https://xxxxxxxxxx.de/ddd', timeout: 3000 } request(options, function(err, response, body) { // JSON body if(err) { console.log(err); callback(true); return; } obj = JSON.parse(body); callback(false, obj); }); }], function(err, results) { app.launch(function(req, res) { sayToday(results[0], req, res); });

  app.intent('DayIntent', {
    "slots": { "DAY": "DAY_OF_WEEK"},
    "utterances": ["xxxxx"]
  }, function(req, res) {
      if(err) { 
        res.say("sdfsdfsdfsdf"); 
        return; 
      }

      //parse JSON
  });

}

);`

dblock commented 7 years ago

There're a thousand ways to do this, and it's a bit beyond the scope of this project, here's one with request-promise.

app.intent('ReposIntent', {}, function(req, res) {

  var options = {
    uri: 'https://api.github.com/user/repos',
    qs: {
        access_token: 'xxxxx xxxxx'
    },
    json: true
  };

  return rp(options)
    .then(function (repos) {
        res.say('User has ' + repos.length + ' repos.');
    });
});