Skip to content

Instantly share code, notes, and snippets.

@calebcgates
Last active October 24, 2017 16:29
Show Gist options
  • Save calebcgates/5fadecd16fce1228c9c620b82e2d598f to your computer and use it in GitHub Desktop.
Save calebcgates/5fadecd16fce1228c9c620b82e2d598f to your computer and use it in GitHub Desktop.
//
// Copyright Caleb Gates 2017
//
'use strict';
const AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
var lambda = new AWS.Lambda();
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
var Alexa = require("alexa-sdk");
var appId = 'amzn1.ask.skill.bf1d0fbb-b07b-4e99-ae33-2d33f583d390';
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
//alexa.dynamoDBTableName = 'TABLE_NAME';
alexa.appId = appId;
alexa.registerHandlers(newSessionHandlers);
alexa.execute();
};
var states = { // Currenlty Unused
LOBBYMODE: '_LOBBYMODE',
};
///// CCG VARIALBES
var helpMessage = ` " Using book mark is as simple as starting the app. ". `;
var newSessionHandlers = { //Only captures new new users.
'NewSession': function() {
var message = ` `;
var repromot = ` `;
//IF HELP FUNCTION
if(this.event.request.intent !== undefined && this.event.request.intent == "AMAZON.HelpIntent"){ //INVOKE WITH INTENT //UNDEFINED IS THE PROPER COMPARISON.
this.emitWithState("AMAZON.HelpIntent");
}
var code = getUniqueHash(this.session.user.userId);
var codeReadable = code.split("").join(". ");
codeReadable = codeReadable + ". ";
var d = new Date();
var n = d.getTime();
var params = {
FunctionName: 'readDynamoDB', // Paul, your lambda function we are going to invoke
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: `{ "userId" : "${this.session.user.userId}", "code":"${code}", "time":"${n}" }` //Gets saved as event variable
};
//this.emit == tell the user. Note: all wordings can change.
lambda.invoke(params, function(err, data) {
if (err) {
this.emit(':tell',` I'm currenlty having trouble accessing the book marking system. I appologise for the inconvenience. `);
} else {
if(data.response == "success"){ //amazon id exists, is paired with a chrome extension id -> set time stamp as bookmark
this.emit(':tell',` Bookmark complete `);
}else if(data.response == "newuser"){ //amazon id does not exists, put into database along with 4 letter code
this.emit(':tell',` Welcome to Book mark. To begin... Instructions... Please enter the following 4 letter code into the chrome extension. Your code is. ${codeReadable}` );
}else if(data.response == "stillunverified"){ //amazon id does exists, but is not paired with chrome extension id
this.emit(':tell',` I see you still havent verified your account. Please enter your 4 letter code into the chrome extension. Your code is. ${codeReadable}`);
}
}
});
},
"AMAZON.StopIntent": function() { //Can't get here
console.log("STOPINTENT");
this.emit(':tell', " Goodbye! ");
},
"AMAZON.CancelIntent": function() { //Can't get here
console.log("CANCELINTENT");
this.emit(':tell', "Goodbye!");
},
'AMAZON.HelpIntent': function() { //Alexa, ask book mark for help
var message = helpMessage;
this.emit(':ask', message, message);
},
'Unhandled': function() { //Couldn't possibly get here.
console.log("UNHANDLED");
this.emit(':ask', ` I'm not sure what you said, please try again. `);
},
};
function getUniqueHash(userId){
//very basic algorith to start. Take last 4 letter and numbers if a number convert to it's equatable letter. set all to uppercase.
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var lastFour = userId.substr(userId.length - 4);
lastFour = lastFour.split('');
lastFour = lastFour.map(function(character) {
if(!isNaN(parseInt(character))){
character = alphabet[parseInt(character)];
} character = character.toUpperCase();
return character;
});
lastFour = lastFour.join("");
return lastFour;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment