Created
January 14, 2018 17:56
-
-
Save NimaBoscarino/2ffca7d3e41487c3e15fbff134e054ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var https = require('https') | |
exports.handler = (event, context) => { | |
try { | |
if (event.session.new) { | |
// New Session | |
console.log("NEW SESSION") | |
} | |
switch (event.request.type) { | |
case "LaunchRequest": | |
// Launch Request | |
console.log(`LAUNCH REQUEST`) | |
context.succeed( | |
generateResponse( | |
buildSpeechletResponse("ayy what's up big huss?", true), | |
{} | |
) | |
) | |
break; | |
case "IntentRequest": | |
// Intent Request | |
console.log(`INTENT REQUEST`) | |
switch(event.request.intent.name) { | |
case "SetBricks": | |
fbPut("/weapon", "bricks").then(res => { | |
context.succeed( | |
generateResponse( | |
buildSpeechletResponse(`HIT THEM WITH THE BRICK AYO BURR BURR`, true), | |
{} | |
) | |
) | |
}) | |
break; | |
case "SetLemonade": | |
fbPut("/weapon", "lemonade").then(res => { | |
context.succeed( | |
generateResponse( | |
buildSpeechletResponse(`GUCCI GUCCI LEMONADE`, true), | |
{} | |
) | |
) | |
}) | |
break; | |
case "SetIce": | |
fbPut("/weapon", "ice").then(res => { | |
context.succeed( | |
generateResponse( | |
buildSpeechletResponse(`GOT THAT ICE IN MY VEINS BURR BURR!`, true), | |
{} | |
) | |
) | |
}) | |
break; | |
case "GetSubscriberCount": | |
var endpoint = "" // ENDPOINT GOES HERE | |
var body = "" | |
https.get(endpoint, (response) => { | |
response.on('data', (chunk) => { body += chunk }) | |
response.on('end', () => { | |
var data = JSON.parse(body) | |
var subscriberCount = data.items[0].statistics.subscriberCount | |
context.succeed( | |
generateResponse( | |
buildSpeechletResponse(`Current subscriber count is ${subscriberCount}`, true), | |
{} | |
) | |
) | |
}) | |
}) | |
break; | |
case "GetVideoViewCount": | |
var endpoint = "" // ENDPOINT GOES HERE | |
var body = "" | |
https.get(endpoint, (response) => { | |
response.on('data', (chunk) => { body += chunk }) | |
response.on('end', () => { | |
var data = JSON.parse(body) | |
var viewCount = data.items[0].statistics.viewCount | |
context.succeed( | |
generateResponse( | |
buildSpeechletResponse(`Current view count is ${viewCount}`, true), | |
{} | |
) | |
) | |
}) | |
}) | |
break; | |
case "GetVideoViewCountSinceDate": | |
console.log(event.request.intent.slots.SinceDate.value) | |
var endpoint = "" // ENDPOINT GOES HERE | |
var body = "" | |
https.get(endpoint, (response) => { | |
response.on('data', (chunk) => { body += chunk }) | |
response.on('end', () => { | |
var data = JSON.parse(body) | |
var viewCount = data.items[0].statistics.viewCount | |
context.succeed( | |
generateResponse( | |
buildSpeechletResponse(`Current view count is ${viewCount}`, true), | |
{} | |
) | |
) | |
}) | |
}) | |
break; | |
default: | |
throw "Invalid intent" | |
} | |
break; | |
case "SessionEndedRequest": | |
// Session Ended Request | |
console.log(`SESSION ENDED REQUEST`) | |
break; | |
default: | |
context.fail(`INVALID REQUEST TYPE: ${event.request.type}`) | |
} | |
} catch(error) { context.fail(`Exception: ${error}`) } | |
} | |
// Helpers | |
buildSpeechletResponse = (outputText, shouldEndSession) => { | |
return { | |
outputSpeech: { | |
type: "PlainText", | |
text: outputText | |
}, | |
shouldEndSession: shouldEndSession | |
} | |
} | |
generateResponse = (speechletResponse, sessionAttributes) => { | |
return { | |
version: "1.0", | |
sessionAttributes: sessionAttributes, | |
response: speechletResponse | |
} | |
} | |
var https = require('https'); | |
var firebaseHost = "nwhacks-637cf.firebaseio.com"; | |
function fbGet(key){ | |
return new Promise((resolve, reject) => { | |
var options = { | |
hostname: firebaseHost, | |
port: 443, | |
path: key + ".json", | |
method: 'GET' | |
}; | |
var req = https.request(options, function (res) { | |
res.setEncoding('utf8'); | |
var body = ''; | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
resolve(JSON.parse(body)) | |
}); | |
}); | |
req.end(); | |
req.on('error', reject); | |
}); | |
} | |
function fbPut(key, value){ | |
return new Promise((resolve, reject) => { | |
var options = { | |
hostname: firebaseHost, | |
port: 443, | |
path: key + ".json", | |
method: 'PUT' | |
}; | |
var req = https.request(options, function (res) { | |
res.setEncoding('utf8'); | |
var body = ''; | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
resolve(body) | |
}); | |
}); | |
req.end(JSON.stringify(value)); | |
req.on('error', reject); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment