var alexa = require('alexa-app');
var request = require('request-promise');
var express = require('express');

var express_app = express();

var app = new alexa.app('connect');
app.express({ expressApp: express_app });

app.intent('PlayIntent', {
    "utterances": [
        "play",
        "resume",
        "continue"
    ]
},
    function (req, res) {
        request.put("https://api.spotify.com/v1/me/player/play").auth(null, null, true, req.sessionDetails.accessToken);
        res.say('Playing');
    }
);

app.intent('PauseIntent', {
    "utterances": [
        "pause"
    ]
},
    function (req, res) {
        request.put("https://api.spotify.com/v1/me/player/pause").auth(null, null, true, req.sessionDetails.accessToken);
        res.say('Paused');
    }
);

app.intent('GetDevicesIntent', {
    "utterances": [
        "devices",
        "list"
    ]
},
    function (req, res) {
        var devices = [];
        request.get({
            url: "https://api.spotify.com/v1/me/player/devices",
            auth: {
                "bearer": req.sessionDetails.accessToken
            },
            json: true
        },
            function (error, response, body) {
                if (error != null) console.log('error:', error);
                devices = body.devices;
            });
        res.say("I found these devices:");
        for (var i = 0; i < devices.length; i++) {
            res.say(devices[i].name);
        }
    }
);

express_app.use(express.static(__dirname));
express_app.get('/', function(req, res) {
    res.redirect('https://github.com/thorpelawrence/alexa-spotify-connect');
});

var port = process.env.PORT || 8888;
console.log("Listening on port " + port);
express_app.listen(port);

module.exports = app;

@@ -913,32 +913,39 @@
 s = %5B%5D;%0A        
+return 
 request.get(%7B%0A  
@@ -1123,33 +1123,33 @@
 : true%0A        %7D
-,
+)
 %0A            fun
@@ -1141,24 +1141,30 @@
             
+.then(
 function (er
@@ -1165,97 +1165,15 @@
 on (
-error, response, body) %7B%0A                if (error != null) console.log('error:', error);
+data) %7B
 %0A   
@@ -1221,16 +1221,8 @@
     
-    %7D);%0A
     
@@ -1268,16 +1268,24 @@
         
+  
+      
 for (var
@@ -1323,32 +1323,40 @@
 ) %7B%0A            
+        
 res.say(devices%5B
@@ -1373,17 +1373,138 @@
         
+ 
-%7D
+       %7D%0A            %7D)%0A            .catch(function (err) %7B%0A                console.log('error:', error);%0A            %7D);
 %0A    %7D%0A)
@@ -1579,16 +1579,17 @@
 function
+ 
 (req, re

Migrate get devices intent to promise