Skip to content

Instantly share code, notes, and snippets.

@mohamedmansour
Created February 18, 2017 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohamedmansour/68d58e8626fe5a246248da3e94426740 to your computer and use it in GitHub Desktop.
Save mohamedmansour/68d58e8626fe5a246248da3e94426740 to your computer and use it in GitHub Desktop.
Hackathon - Amazon Alexa Backend using Lambda
var http = require('http');
exports.handler = function (event, context) {
try {
console.log("event.session.application.applicationId=" + event.session.application.applicationId);
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 ("HypeIntent" === intentName) {
onHypeIntent(intent, session, callback);
}
else if ("SwitchVideoIntent" === intentName) {
onSwitchVideoIntent(intent, session, 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
}
var validHypes = {
'lol': 1,
'hype': 1,
'omg': 0,
'oh my god': 0,
'are you serious': -1,
'bad': -1,
'wtf': -1,
'this is funny': 1,
'that is cool': 1,
'this is amazing': 1,
'legendary': 1,
'amazing': 1
};
// --------------- Functions that control the skill's behavior -----------------------
function onHypeIntent(intent, session, callback) {
var sessionAttributes = {};
var hypeSlot = intent.slots.Hypes.value;
console.log(hypeSlot);
if (validHypes[hypeSlot] !== undefined) {
http.get('http://hypenbc.mybluemix.net/api/hype?type='+hypeSlot+'&emotion=' + validHypes[hypeSlot], function(res) {
callback(sessionAttributes, buildSpeechletResponse("Hyped", " ", " ", false));
}).on('error', function(e) {
console.log("API error: " + e.message);
});
}
else {
console.log("Invalid Hype: " + hypeSlot);
callback(sessionAttributes, buildSpeechletResponse("Hyped", " ", " ", false));
}
}
var validVideos = {
'warcraft': true,
'Mr. robot': true
};
function onSwitchVideoIntent(intent, session, callback) {
var sessionAttributes = {};
var videoSlot = intent.slots.Videos.value;
console.log(videoSlot);
if (validVideos[videoSlot]) {
http.get('http://hypenbc.mybluemix.net/api/watch?video='+videoSlot, function(res) {
callback(sessionAttributes, buildSpeechletResponse("Hyped", "Alright, one moment please.", " ", false));
}).on('error', function(e) {
console.log("API error: " + e.message);
});
}
else {
console.log("Invalid Video: " + videoSlot);
callback(sessionAttributes, buildSpeechletResponse("Hyped", " ", " ", false));
}
}
function getWelcomeResponse(callback) {
// If we wanted to initialize the session to have some attributes we could add those here.
var sessionAttributes = {};
callback(sessionAttributes,
buildSpeechletResponse("Welcome", "I am going to watch with you, say hype, when something exciting happens!", ' ', false));
}
// --------------- 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