Skip to content

Instantly share code, notes, and snippets.

@nitobuendia
Last active May 27, 2018 14:19
Show Gist options
  • Save nitobuendia/84ce7521778c98fb65c4a3fda5e11e75 to your computer and use it in GitHub Desktop.
Save nitobuendia/84ce7521778c98fb65c4a3fda5e11e75 to your computer and use it in GitHub Desktop.
[My Motivational Coach] - A DialogFlow-powered add-on for Google Home Assistant that replies with a randomly generated motivational quote.
/**
* @fileoverview Handles a DialogFlow request and returns a motivational quote.
*
* In order to deploy, run:
* gcloud beta functions deploy getMotivationQuote --trigger-http
*/
const /** boolean */ EQUAL_PROBABILITY = true;
/**
* Quotes are not owned by author or code.
* Just a sample extracted from multiple sources.
*/
const /** !Array */ QUOTE_LIST = [
[ // Music and songs.
[ // Chojin (translated).
[ // Rie cuando puedas.
`Smile whenever you can. Cry whenever you need it.`,
],
],
],
[ // TV Shows.
[ // How I Met Your Mother.
`Your problem is all you do is think, think think.
I am teaching you how to do, do do.`,
],
],
[ // Movies.
[ // The pursuit of happiness.
`Don't ever let somebody tell you you can't do something.
You got a dream, you gotta protect it.
People who can't do something themselves,
they wanna tell you you can't do it.
If you want something, go get it.`,
],
[ // Star Wars.
`Do or do not. There is no try.`,
],
],
[ // Books and book authors.
[ // Jack Welch - Winning.
`Every day is a new challenge.`,
],
],
[ // Personalities.
[ // Albert Einstein.
`During crisis, only imagination is more important than wisdom.`,
`Become not a man of success, but a man of value.`,
],
[ // Warren Buffet.
`It takes twenty years to build a reputation and five minutes to run it.`,
],
[ // Steve Jobs.
`Keep hungry. Stay foolish.`,
],
],
];
/**
* Deep flattens an array.
* Merges all sub levels of an array into one array.
* @param {!Array} array Array to be flatten.
* @return {!Array} Flatten array with all values at array level.
*/
const flattenDeepArray = (arrayToFlat) => {
return arrayToFlat.reduce((accumulator, currentValue) =>
Array.isArray(currentValue) ?
accumulator.concat(flattenDeepArray(currentValue)) :
accumulator.concat(currentValue), []);
};
/**
* Gets a random number using a uniform distribution.
* @param {number} min Lower range for number generation.
* @param {number} max Upper range for number generation.
* @return {number} Random number uniformly generated.
*/
const getRandomInt = (min, max) => {
return Math.floor(Math.random() * (max - min)) + min;
};
/**
* Exports a given text as a JSON response for DialogFlow.
* @param {string} text Text to export as JSON for response.
* @param {!HTTPResponse} response HTTP Response generate from request.
* @return {!HTTPResponse} HTTP Response with given types and JSON data.
*/
function exportAsJson(text, response) {
if (response.json) { // DialogFlow API V2.
response.json({ fulfillmentText: text});
} else { // DialogFlow API V1.
response.setHeader('Content-Type', 'application/json');
response.send(JSON.stringify({
'speech': text,
'displayText': text,
}));
}
return response;
};
/**
* Handles HTTP Requests and produces a response with a quote in JSON.
* @param {!HTTPRequest} request Node HTTP DialogFlow Request.
* @param {!HTTPResponse} response Node HTTP DialogFlow Response.
*/
exports.getMotivationQuote = function getMotivationQuote (request, response) {
let quoteText = QUOTE_LIST;
if (EQUAL_PROBABILITY) quoteText = flattenDeepArray(quoteText);
while(Array.isArray(quoteText)) {
const randomSelection = getRandomInt(0, quoteText.length - 1);
quoteText = quoteText[randomSelection];
}
response = exportAsJson(quoteText, response);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment