Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Alexa skill for your daily spiritual quote
/* eslint-disable func-names */
/* eslint-disable dot-notation */
/* eslint-disable new-cap */
/* eslint quote-props: ['error', 'consistent']*/
/**
* This sample demonstrates a simple skill built with the Amazon Alexa Skills
* nodejs skill development kit.
* This sample supports en-US lauguage.
* The Intent Schema, Custom Slots and Sample Utterances for this skill, as well
* as testing instructions are located at https://github.com/alexa/skill-sample-nodejs-trivia
**/
'use strict';
const Alexa = require('alexa-sdk');
const AWS = require('aws-sdk');
const QUOTES = {
"Hindu": [
'Shut out the physical world, control the mind then you will be free',
'Lead me from the unreal to the real; ignorance to knowledge; and from death to immortality',
'You don\'t have a Soul; you are the soul. You have a body',
'Put your heart, mind and soul into even the smallest acts. This is the secret of success',
'Don\'t ruin other people\'s happiness because you can\'t find your own'
],
"Christian": [
'Hold material, goods and wealth on a flat palm and not in a clenched fist',
'I\'ve read the last page of the Bible and it\'s all going to turn out all right',
'Worry does not empty tomorrow of its sorrow; it empties today of its strength',
'Outside of Christ I am weak; in Christ I am strong',
'One touch of Christ is worth a lifetime of suffering'
],
"Buddhist": [
'Health is the greatest gift, contentment the greatest wealth, faithfulness the best relationship',
'Peace comes from within. Do not seek in without',
'There are only two mistakes one can make along the road to truth; not going all the way and not starting',
'It is better to conquer yourself than to win a thousand battles. Then the victory is yours',
'I never see what has been done; I only see what remains to be done'
],
"Islam": [
'Be patient. For what was written for you was written by the greatest of writers',
'And your lord says, call upon me and I will respond to you',
'Tears are prayers too, they travel to Allah when we can\'t speak',
'For every person you forgive you heal a wound of your own',
'There is nothing heavier in scales than good character'
],
"Jewish": [
]
}
const APP_ID = "amzn1.ask.skill.d6af1aac-1fd8-45bd-954b-6031b5427e98"
const newSessionHandlers = {
'LaunchRequest': function () {
const speechOutput = "Welcome to the skill";
const repromptSpeech = "Please tell me a command";
this.emit(':ask', speechOutput, repromptSpeech);
},
'QuoteIntent': function () {
let quote = ''
let genre = ''
console.log(this.event.request.intent.slots.RELIGION.value)
if (this.event.request.intent.slots.RELIGION.value != undefined) {
const religionKey = normalizeReligion(this.event.request.intent.slots.RELIGION.value)
const quotesList = QUOTES[religionKey]
console.log(religionKey)
console.log(quotesList)
quote = quotesList[getRandomIntInclusive(0, quotesList.length-1)];
genre = religionKey;
console.log("Slot value is ", this.event.request.intent.slots.RELIGION.value)
} else {
console.log("Generic quote")
quote = "Let go of your ego";
genre = 'Universal';
}
this.attributes['previousQuote'] = quote
this.attributes['previousQuoteGenre'] = genre
console.log('User Id ', this.event.session.user.userId)
saveToDynamo({
"previousQuote" : quote,
"previousQuoteGenre" : genre,
"userId": this.event.session.user.userId
}).then(data => {
this.emit(':ask', quote, quote);
}).catch(err => {
console.log(err);
this.emit(':ask', quote, quote);
});
},
'AMAZON.StopIntent': function () {
this.emit('SessionEndedRequest');
},
'AMAZON.CancelIntent': function () {
this.emit('SessionEndedRequest');
},
'SessionEndedRequest': function () {
console.log(`Session ended: ${this.event.request.reason}`);
const previousQuote = this.attributes['previousQuote']
const previousQuoteGenre = this.attributes['previousQuoteGenre']
console.log('Previous quote ', previousQuote)
console.log('Previous quote genre ', previousQuoteGenre)
console.log('User Id ', this.event.session.user.userId)
},
'Unhandled': function () {
const speechOutput = "Unhandled";
this.emit(':ask', speechOutput, speechOutput);
},
};
function saveToDynamo(stateObj) {
return new Promise((resolve, reject) => {
var params = {
TableName : 'SpiritualQuotes',
Item: {
"UserId": stateObj.userId,
"PreviousQuote": stateObj.previousQuote,
"PreviousQuoteGenre":stateObj.previousQuoteGenre
}
};
console.log(Object.keys(params))
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.put(params, function(err, data) {
if (err) {
console.log(err);
reject(err);
}
else {
console.log(data);
resolve(data)
}
});
});
}
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
}
function normalizeReligion(religion) {
if (religion === 'Hindu' || religion == 'Hindusim') {
return 'Hindu'
} else if (religion === 'Muslim' || religion == 'Islam' || religion === 'Islamic') {
return 'Islam'
} else if (religion === 'Christianity' || religion == 'Christian') {
return 'Christian'
} else if (religion === 'Buddhist' || religion == 'Buddhism') {
return 'Buddhist'
}
}
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(newSessionHandlers);
alexa.execute();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment