Skip to content

Instantly share code, notes, and snippets.

@mash
Created July 13, 2016 05:17
Show Gist options
  • Save mash/c3a363c7ad922457372f9ef9265c3d9a to your computer and use it in GitHub Desktop.
Save mash/c3a363c7ad922457372f9ef9265c3d9a to your computer and use it in GitHub Desktop.
/*
* Based on http://amzn.to/1LzFrj6
*/
var applicationId = "";
var clientkey = "";
var deviceid = "";
var ir = {
on: {"format":"raw","freq":38,"data":[6648,3228,873,2368,873,873,968,968,873,968,873,968,968,968,968,968,873,968,968,968,873,968,873,968,968,968,968,2451,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,873,2451,873,873,873,2451,873,2451,873,2451,873,2451,873,2451,873,2451,873,873,873,2451,968,2368,968,2368,873,2368,873,2368,873,2368,873,2368,873,2368,873,2368,873,873,873,968,873,968,873,968,968,968,968,968,873,968,873,968,873,873,968,968,968,2451,873,2451,873,968,873,968,968,2368,904,2368,904,2368,904,2368,904,904,904,904,904,2368,904,2368,904,904,904,904,904,904,904,2368,904,904,904,904,904,2368,904,904,904,904,904,2368,904,2368,904,904,904,2368,904,2368,904,904,904,2368,904,2368,904,904,904,2368,904,2368,904,904,904,904,904,2368,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,904,904,2368,904,2368,904,2368,904,904,904,904,904,2368,904,2368,904,2368,904,904,904,2368,904,904,904,2368,904,2368,904,904,904,904,904,904,904,2368,904,904,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,873,873,873,873,873,2368,873,2368,873,2368,873,2368,873,2368,873,2368,873,2368,873,2368,873,2368,873,2368,873,968,873,968,873,873,968,968,968,2451,873,873,873,873,873,873,873,2451,873,2451,873,2451,873,2451,873,873,873,2451,873,2451,873,873,873,873,873,968,873,2368,904,2368,904,2368,904,2368,904,904,904,2368,904,2368,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,904,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904]},
off: {"format":"raw","freq":38,"data":[6648,3228,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,904,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,904,904,904,904,2368,904,2368,904,2368,904,2368,904,904,904,904,904,2368,904,2368,904,904,904,904,904,904,904,2368,904,904,904,904,904,2368,904,904,904,904,904,2368,904,2368,904,904,904,2368,904,2368,904,904,904,2368,904,2368,904,904,904,2368,904,2368,904,904,904,904,904,2368,904,904,904,904,904,904,904,904,904,904,904,2368,904,2368,904,904,904,2368,904,2368,904,2368,904,904,904,904,904,2368,904,2368,904,2368,904,904,904,2368,904,904,904,2368,904,2368,904,904,904,904,904,904,904,2368,904,904,904,2368,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,904,815,968,968,968,968,968,815,968,815,968,968,968,968,968,968,2451,815,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,904,815,968,968,968,968,968,815,968,815,968,968,968,815,968,968,2451,815,2451,904,2451,815,2451,904,2451,815,2451,904,2451,904,2451,761,2537,904,2451,761,1037,761,1037,843,968,843,968,761,2537,761,1037,761,1037,761,1037,761,2537,761,2537,761,2537,761,2537,843,1002,843,2537,761,2537,761,1002,815,1002,815,1002,815,1002,815,2537,761,2537,761,2537,761,1002,815,2537,761,2537,761,2537,761,2537,761,1037,761,1037,761,1037,761,1037,761,1037,761,1037,761,1037,761,1037,761,1037,761,1037,761,1037,761,2537,761,2537,843,2537,761,2537,904,2451,761,2537,904,2451,904,2451,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904,904,904,2451,904,904,904,904,904,904,904,904,904,904,904,904,904,2451,904,904,904,2451,904,2451,904,2451,904,2451,904,2451,904,2451,904]}
};
// Route the incoming request based on type (LaunchRequest, IntentRequest,
// etc.) The JSON body of the request is provided in the event parameter.
exports.handler = function (event, context) {
try {
console.log("event.session.application.applicationId=" + event.session.application.applicationId);
if (event.session.application.applicationId !== applicationId) {
context.fail("Invalid Application ID");
return;
}
if (event.session.new) {
onSessionStarted({requestId: event.request.requestId}, event.session);
}
if (event.request.type === "LaunchRequest") {
onLaunch(event.request,
event.session,
function callback(sessionAttributes, speechletResponse) {
context.succeed(buildResponse(sessionAttributes, speechletResponse));
});
} else 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);
}
};
/**
* Called when the session starts.
*/
function onSessionStarted(sessionStartedRequest, session) {
console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId +
", sessionId=" + session.sessionId);
}
/**
* Called when the user launches the skill without specifying what they want.
*/
function onLaunch(launchRequest, session, callback) {
console.log("onLaunch requestId=" + launchRequest.requestId +
", sessionId=" + session.sessionId);
// Dispatch to your skill's launch.
getWelcomeResponse(callback);
}
/**
* Called when the user specifies an intent for this skill.
*/
function onIntent(intentRequest, session, callback) {
console.log("onIntent requestId=" + intentRequest.requestId +
", sessionId=" + session.sessionId);
var intent = intentRequest.intent,
intentName = intentRequest.intent.name;
// Dispatch to your skill's intent handlers
if ("ControlAC" === intentName) {
onControlACIntent(intent, session, callback);
}
else if ("AMAZON.HelpIntent" === intentName) {
getWelcomeResponse(callback);
}
else if ("AMAZON.StopIntent" === intentName) {
var speechOutput = "Turned off the air conditioner";
var repromptText = "";
var cardTitle = "Stop";
var shouldEndSession = true;
onControlOff(function () {
callback({},
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
}
else if ("AMAZON.CancelIntent" === intentName) {
handleSessionEndRequest(callback);
}
else {
throw "Invalid intent";
}
}
/**
* Called when the user ends the session.
* Is not called when the skill returns shouldEndSession=true.
*/
function onSessionEnded(sessionEndedRequest, session) {
console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId +
", sessionId=" + session.sessionId);
// Add cleanup logic here
}
// --------------- Functions that control the skill's behavior -----------------------
function getWelcomeResponse(callback) {
// If we wanted to initialize the session to have some attributes we could add those here.
var sessionAttributes = {};
var cardTitle = "Welcome";
var speechOutput = "Welcome to the IRKit skill. " +
"Please tell me to turn on or off the air conditioner";
// If the user either does not reply to the welcome message or says something that is not
// understood, they will be prompted again with this text.
var repromptText = "Please tell me to turn on or off the air conditioner";
var shouldEndSession = false;
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
function handleSessionEndRequest(callback) {
var cardTitle = "Session Ended";
var speechOutput = "Thank you for trying the IRKit skill. Have a nice day!";
// Setting this to true ends the session and exits the skill.
var shouldEndSession = true;
callback({}, buildSpeechletResponse(cardTitle, speechOutput, null, shouldEndSession));
}
function onControlACIntent(intent, session, callback) {
var cardTitle = intent.name;
var controlSlot = intent.slots.Control;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = true;
var speechOutput = "";
if (controlSlot) {
var control = controlSlot.value.toLowerCase();
if (control == "on" ||
control == "start") {
speechOutput = "Turned on the air conditioner";
repromptText = "";
onControlOn(function () {
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
}
else {
speechOutput = "Turned off the air conditioner";
repromptText = "";
onControlOff(function () {
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
}
} else {
speechOutput = "I'm not sure what you meant. Please try again";
repromptText = "Please tell me to turn on or off the air conditioner";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}
}
function onControlOn(callback) {
sendIR(ir.on, callback);
}
function onControlOff(callback) {
sendIR(ir.off, callback);
}
function sendIR(ir, callback) {
var https = require("https");
var qs = require("querystring");
var postData = qs.stringify({
clientkey: clientkey,
deviceid: deviceid,
message: JSON.stringify(ir)
});
var req = https.request({
host: "api.getirkit.com",
path: "/1/messages",
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Content-Length": postData.length
}
}, function (res) {
console.log("res.statusCode=" + res.statusCode);
callback();
});
req.write(postData);
req.end();
}
// --------------- Helpers that build all of the responses -----------------------
function buildSpeechletResponse(title, output, repromptText, shouldEndSession) {
return {
outputSpeech: {
type: "PlainText",
text: output
},
card: {
type: "Simple",
title: "SessionSpeechlet - " + title,
content: "SessionSpeechlet - " + output
},
reprompt: {
outputSpeech: {
type: "PlainText",
text: repromptText
}
},
shouldEndSession: shouldEndSession
};
}
function buildResponse(sessionAttributes, speechletResponse) {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment