exports.index = function(req, res) {
  var sessionId;
  var userId;
  console.log(req.body);
  if(req.body.request.type === 'LaunchRequest') {
    alexa.launchRequest(req.body);
    // TODO For now, we don't care about the session or the user id, we will refactor this later.
    sessionId = alexa.sessionId;
    userId = alexa.userId;
    alexa.response('Welcome to Sonos. What can I play for you?', {
      title: 'Alexa-Sonos',
      subtitle: 'Welcome to the Sonos app',
      content: 'Some commands are "Play favorite xxx" or "Play playlist xxx"'
    }, false, function (error, response) {
      if(error) {
        return res.status(500).jsonp({message: error});
      }
      return res.jsonp(response);
    });
  } else if(req.body.request.type === 'IntentRequest') {
    alexa.intentRequest(req.body);
    // TODO For now, we don't care about the session or the user id, we will refactor this later.
    sessionId = alexa.sessionId;
    userId = alexa.userId;
    var intent = alexa.intentName;
    var slots = alexa.slots;
    if(intent === 'PlayPlaylist') {
      options.path='/family%20room/playlist/'+encodeURIComponent(req.body.request.intent.slots.Playlist.value);
      http.request(options, function(response){
        console.log(response.body);
        alexa.response('Playlist requested.', {
          title: 'Alexa-Sonos',
          subtitle: 'Playlist requested',
          content: 'Playlist requested.'
        }, true, function (error, response) {
          if(error) {
            return res.status(500).jsonp(error);
          }
          return res.jsonp(response);
        });
      }).end();
    } else if(intent === 'PlayFavorite') {
      console.log('favorite');
      options.path='/family%20room/favorite/'+encodeURIComponent(req.body.request.intent.slots.Favorite.value);
      http.request(options, function(response){
        alexa.response('Playing '+req.body.request.intent.slots.Favorite.value, {
          title: 'Alexa-Sonos',
          subtitle: 'Favorite requested: '+req.body.request.intent.slots.Favorite.value,
          content: 'Favorite requested.'
        }, true, function (error, response) {
          if(error) {
            return res.status(500).jsonp(error);
          }
          return res.jsonp(response);
        });
      }).end();
    } else if(intent === 'StartMusic') {
      console.log('start');
      options.path='/family%20room/play';
      console.log(options.path);
      http.request(options, function(response){
        alexa.response('Playing', {
          title: 'Alexa-Sonos',
          subtitle: 'Music started',
          content: 'Music started.'
        }, true, function (error, response) {
          if(error) {
            return res.status(500).jsonp(error);
          }
          return res.jsonp(response);
        });
      }).end();
    } else if(intent === 'StopMusic') {
      console.log('stop');
      options.path='/family%20room/pause';
      http.request(options, function(response){
        console.log(response.body);
        alexa.response('Pausing', {
          title: 'Alexa-Sonos',
          subtitle: 'Music stopped',
          content: 'Music stopped.'
        }, true, function (error, response) {
          if(error) {
            return res.status(500).jsonp(error);
          }
          return res.jsonp(response);
        });
      }).end();
    } else {
      alexa.response('Unknown intention, please try a different command.', {
        title: 'Alexa-Sonos',
        subtitle: 'Unknown intention.',
        content: 'Unknown intention, please try a different command.'
      }, false, function (error, response) {
        if(error) {
          return res.status(500).jsonp(error);
        }
        return res.jsonp(response);
      });
    }
  } else {
    alexa.sessionEndedRequest(req.body);
    // TODO For now, we don't care about the session or the user id, we will refactor this later.
    sessionId = alexa.sessionId;
    userId = alexa.userId;
    var sessionEndReason = alexa.reason;
    alexa.response(function (error, response) {
      if(error) {
        return res.status(500).jsonp(error);
      }
      return res.jsonp(response);
    });
  }
};
exports.index = function(req, res) {
  var sessionId;
  var userId;
  if(req.body.request.type === 'LaunchRequest') {
    alexa.launchRequest(req.body);
    // TODO For now, we don't care about the session or the user id, we will refactor this later.
    sessionId = alexa.sessionId;
    userId = alexa.userId;
    alexa.response('Welcome to the Twitch app, you can say things like is this streamer live, or how many viewers does streamer have.', {
      title: 'Alexa-Twitch',
      subtitle: 'Welcome to the Twitch app',
      content: 'Some commands are "Is summit1g live?" or "How many viewers does summit1g have?"'
    }, false, function (error, response) {
      if(error) {
        return res.status(500).jsonp({message: error});
      }
      return res.jsonp(response);
    });
  } else if(req.body.request.type === 'IntentRequest') {
    alexa.intentRequest(req.body);
    // TODO For now, we don't care about the session or the user id, we will refactor this later.
    sessionId = alexa.sessionId;
    userId = alexa.userId;
    var intent = alexa.intentName;
    var slots = alexa.slots;
    if(intent === 'GetUserLive') {
      twitch.checkUserLive(slots, function (error, isLive) {
        if(error) {
          console.log(error);
          return res.status(500).jsonp({message: 'Could not process intention.'});
        }
        if(isLive) {
          alexa.response('The streamer ' + slots.Streamer.value + ' is live.', {
            title: 'Alexa-Twitch',
            subtitle: 'Stream is live',
            content: 'The streamer ' + slots.Streamer.value + ' is live.'
          }, false, function (error, response) {
            if(error) {
              return res.status(500).jsonp(error);
            }
            return res.jsonp(response);
          });
        } else {
          alexa.response('The streamer ' + slots.Streamer.value + ' is offline.', {
            title: 'Alexa-Twitch',
            subtitle: 'Stream is offline',
            content: 'The streamer ' + slots.Streamer.value + ' is offline.'
          }, false, function (error, response) {
            if(error) {
              return res.status(500).jsonp(error);
            }
            return res.jsonp(response);
          });
        }
      });
    } else if(intent === 'GetViewerCount') {
      twitch.getViewerCount(slots, function (error, viewerCount) {
        if(error) {
          console.log(error);
          return res.status(500).jsonp({message: 'Could not process intention.'});
        }
        alexa.response('The view count for streamer ' + slots.Streamer.value + ' is ' + viewerCount, {
          title: 'Alexa-Twitch',
          subtitle: slots.Streamer.value + ' - ' + viewerCount + ' view count.',
          content: 'The view count for streamer ' + slots.Streamer.value + ' is ' + viewerCount
        }, false, function (error, response) {
          if(error) {
            return res.status(500).jsonp(error);
          }
          return res.jsonp(response);
        });
      });
    } else {
      alexa.response('Unknown intention, please try a different command.', {
        title: 'Alexa-Twitch',
        subtitle: 'Unknown intention.',
        content: 'Unknown intention, please try a different command.'
      }, false, function (error, response) {
        if(error) {
          return res.status(500).jsonp(error);
        }
        return res.jsonp(response);
      });
    }
  } else {
    alexa.sessionEndedRequest(req.body);
    // TODO For now, we don't care about the session or the user id, we will refactor this later.
    sessionId = alexa.sessionId;
    userId = alexa.userId;
    var sessionEndReason = alexa.reason;
    alexa.response(function (error, response) {
      if(error) {
        return res.status(500).jsonp(error);
      }
      return res.jsonp(response);
    });
  }
};
Example #3
0
router.post('/archive', function (req, res) {
  if(!(req.body.request && req.body.request.type)) {
    return res.status(400).jsonp({message: 'Probably not an Amazon Echo request.'});
  }
  var sessionId;
  var userId;
  if(req.body.request.type === 'LaunchRequest') {
    alexa.launchRequest(req.body);
    sessionId = alexa.sessionId;
    userId = alexa.userId;
    alexa.response('Welcome to the bass drive app, you can say check for update to see if a new archive download is available.', {
      title: 'Bassdrive',
      subtitle: 'Welcome to the Bassdrive app.',
      content: 'Say "check for update" to find out if a new archive is available.'
    }, false, function (error, response) {
      if(error) {
        return res.status(500).jsonp({message: error});
      }
      return res.jsonp(response);
    });
  } else if (req.body.request.type === 'IntentRequest') {
    alexa.intentRequest(req.body);
    sessionId = alexa.sessionId;
    userId = alexa.userId;
    var intent = alexa.intentName;
    var slots = alexa.slots;
    if(intent === 'UpdateCheck') {
      bassdrive.getListings(6, bassdrive.OVERFIEND, function (error, listings) {
        if(error) {
          console.log(error);
          return res.status(500).jsonp({message: 'Could not process intention.'});
        }
        var latestListing = listings[0];
        var latestListingHuman = latestListing.time_ago;
        var latestListingDate = moment(latestListing.published_at).utc().format('dddd, MMMM Do YYYY');
        var latestListingTime = moment(latestListing.published_at).utc().format('hA');
        alexa.response('The latest archive is dated as ' + latestListingHuman + ' on ' + latestListingDate + ' at ' + latestListingTime + ', coordinated universal time.', {
          title: 'Bassdrive',
          subtitle: 'Latest Archive',
          content: latestListingHuman + ' - ' + latestListingDate
        }, true, function (error, response) {
          if(error) {
            return res.status(500).jsonp(error);
          }
          return res.jsonp(response);
        });
      });
    } else {
      alexa.response('Unknown intention, please try a different command.', {
        title: 'Bassdrive',
        subtitle: 'Unknown intention.',
        content: 'Unknown intention, please try a different command.'
      }, true, function (error, response) {
        if(error) {
          return res.status(500).jsonp(error);
        }
        return res.jsonp(response);
      });
    }
  } else {
    alexa.sessionEndedRequest(req.body);
    sessionId = alexa.sessionId;
    userId = alexa.userId;
    var sessionEndReason = alexa.reason;
    alexa.response(function (error, response) {
      if(error) {
        return res.status(500).jsonp(error);
      }
      return res.jsonp(response);
    });
  }
});