Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- exports.handler = function (event, context) {
- try {
- if (event.session.new) {
- onSessionStarted({requestId: event.request.requestId}, event.session);
- }
- if (event.request.type === "IntentRequest") {
- onIntent(event.request,
- event.session,
- function callback(sessionAttributes, speechletResponse) {
- context.succeed(buildResponse(sessionAttributes, speechletResponse));
- });
- } else if (event.request.type === "SessionEndedRequest") {
- onSessionEnded(event.request, event.session);
- context.succeed();
- }
- } catch (e) {
- context.fail("Exception: " + e);
- }
- };
- function onSessionStarted(sessionStartedRequest, session) {
- console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId
- + ", sessionId=" + session.sessionId);
- }
- function onIntent(intentRequest, session, callback) {
- console.log("onIntent requestId=" + intentRequest.requestId
- + ", sessionId=" + session.sessionId);
- var intent = intentRequest.intent,
- intentName = intentRequest.intent.name;
- if (intentName == 'TurnOn') {
- handleTurnOn(intent, session, callback);
- }
- else {
- throw "Invalid intent";
- }
- }
- function onSessionEnded(sessionEndedRequest, session) {
- console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId
- + ", sessionId=" + session.sessionId);
- }
- function handleTurnOn(intent, session, callback) {
- callback(session.attributes,
- xboxOn(process.env.IP, process.env.ID, "Xbox turned on!", "", "true"));
- }
- function xboxOn(ip, id, output, repromptText, shouldEndSession) {
- var dgram = require('dgram');
- // Open socket
- var socket = dgram.createSocket('udp4');
- // Create payload
- var powerPayload = new Buffer('\x00' + String.fromCharCode(id.length) + id + '\x00'),
- powerPayloadLength = new Buffer(String.fromCharCode(powerPayload.length)),
- powerHeader = Buffer.concat([new Buffer('dd0200', 'hex'), powerPayloadLength, new Buffer('0000', 'hex')]),
- powerPacket = Buffer.concat([powerHeader, powerPayload]);
- // Send
- socket.send(powerPacket, 0, powerPacket.length, 5050, ip, function(err) {
- console.log(err);
- socket.close();
- });
- return {
- outputSpeech: {
- type: "PlainText",
- text: output
- },
- reprompt: {
- outputSpeech: {
- type: "PlainText",
- text: repromptText
- }
- },
- shouldEndSession: shouldEndSession
- };
- }
- // ------- Helper functions to build responses -------
- function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
- return {
- outputSpeech: {
- type: "PlainText",
- text: output
- },
- card: {
- type: "Simple",
- title: title,
- content: output
- },
- reprompt: {
- outputSpeech: {
- type: "PlainText",
- text: repromptText
- }
- },
- shouldEndSession: shouldEndSession
- };
- }
- function buildSpeechletResponseWithoutCard(output, repromptText, shouldEndSession) {
- return {
- outputSpeech: {
- type: "PlainText",
- text: output
- },
- reprompt: {
- outputSpeech: {
- type: "PlainText",
- text: repromptText
- }
- },
- shouldEndSession: shouldEndSession
- };
- }
- function buildResponse(sessionAttributes, speechletResponse) {
- return {
- version: "1.0",
- sessionAttributes: sessionAttributes,
- response: speechletResponse
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement