Advertisement
Guest User

lambda nodejs xbox on

a guest
Jan 23rd, 2017
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. exports.handler = function (event, context) {
  4.     try {
  5.        
  6.         if (event.session.new) {
  7.             onSessionStarted({requestId: event.request.requestId}, event.session);
  8.         }
  9.  
  10.         if (event.request.type === "IntentRequest") {
  11.             onIntent(event.request,
  12.                 event.session,
  13.                 function callback(sessionAttributes, speechletResponse) {
  14.                     context.succeed(buildResponse(sessionAttributes, speechletResponse));
  15.                 });
  16.         } else if (event.request.type === "SessionEndedRequest") {
  17.             onSessionEnded(event.request, event.session);
  18.             context.succeed();
  19.         }
  20.     } catch (e) {
  21.         context.fail("Exception: " + e);
  22.     }
  23. };
  24.  
  25. function onSessionStarted(sessionStartedRequest, session) {
  26.     console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId
  27.         + ", sessionId=" + session.sessionId);
  28.  
  29. }
  30.  
  31. function onIntent(intentRequest, session, callback) {
  32.     console.log("onIntent requestId=" + intentRequest.requestId
  33.         + ", sessionId=" + session.sessionId);
  34.  
  35.     var intent = intentRequest.intent,
  36.         intentName = intentRequest.intent.name;
  37.  
  38.     if (intentName == 'TurnOn') {
  39.         handleTurnOn(intent, session, callback);
  40.     }
  41.     else {
  42.         throw "Invalid intent";
  43.     }
  44. }
  45.  
  46. function onSessionEnded(sessionEndedRequest, session) {
  47.     console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId
  48.         + ", sessionId=" + session.sessionId);
  49. }
  50.  
  51. function handleTurnOn(intent, session, callback) {
  52.     callback(session.attributes,
  53.         xboxOn(process.env.IP, process.env.ID, "Xbox turned on!", "", "true"));
  54. }
  55.  
  56. function xboxOn(ip, id, output, repromptText, shouldEndSession) {
  57.  
  58.   var dgram = require('dgram');
  59.  
  60.   // Open socket
  61.   var socket = dgram.createSocket('udp4');
  62.  
  63.   // Create payload
  64.   var powerPayload = new Buffer('\x00' + String.fromCharCode(id.length) + id + '\x00'),
  65.       powerPayloadLength = new Buffer(String.fromCharCode(powerPayload.length)),
  66.       powerHeader = Buffer.concat([new Buffer('dd0200', 'hex'), powerPayloadLength, new Buffer('0000', 'hex')]),
  67.       powerPacket = Buffer.concat([powerHeader, powerPayload]);
  68.  
  69.   // Send
  70.   socket.send(powerPacket, 0, powerPacket.length, 5050, ip, function(err) {
  71.       console.log(err);
  72.     socket.close();
  73.   });
  74.  
  75.     return {
  76.         outputSpeech: {
  77.             type: "PlainText",
  78.             text: output
  79.         },
  80.         reprompt: {
  81.             outputSpeech: {
  82.                 type: "PlainText",
  83.                 text: repromptText
  84.             }
  85.         },
  86.         shouldEndSession: shouldEndSession
  87.     };
  88. }
  89.  
  90. // ------- Helper functions to build responses -------
  91.  
  92. function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
  93.     return {
  94.         outputSpeech: {
  95.             type: "PlainText",
  96.             text: output
  97.         },
  98.         card: {
  99.             type: "Simple",
  100.             title: title,
  101.             content: output
  102.         },
  103.         reprompt: {
  104.             outputSpeech: {
  105.                 type: "PlainText",
  106.                 text: repromptText
  107.             }
  108.         },
  109.         shouldEndSession: shouldEndSession
  110.     };
  111. }
  112.  
  113. function buildSpeechletResponseWithoutCard(output, repromptText, shouldEndSession) {
  114.     return {
  115.         outputSpeech: {
  116.             type: "PlainText",
  117.             text: output
  118.         },
  119.         reprompt: {
  120.             outputSpeech: {
  121.                 type: "PlainText",
  122.                 text: repromptText
  123.             }
  124.         },
  125.         shouldEndSession: shouldEndSession
  126.     };
  127. }
  128.  
  129. function buildResponse(sessionAttributes, speechletResponse) {
  130.     return {
  131.         version: "1.0",
  132.         sessionAttributes: sessionAttributes,
  133.         response: speechletResponse
  134.     };
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement