Eric Gregory
Created April 25, 2016

Random Muse

Randomized ideas for writers, artists, and anyone else who needs a place to start.

31
Random Muse

Things used in this project

Software apps and online services

Alexa Skills Kit
Amazon Alexa Alexa Skills Kit

Story

Read more

Code

index.js

JavaScript
The Node.js code that runs the Random Muse skill.
/**
Copyright 2016 Eric Gregory

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * App ID for the skill
 */
var APP_ID = "amzn1.echo-sdk-ams.app.446e94c5-4f5b-4301-bf53-578e80681820";

/**
 * Array containing ideas.
 */
var IDEAS = [
  "A vending machine. ",
  "Flowers in the sand. ",
  "Counter-clockwise motion. ",
  "A dim star. ",
  "A smile. ",
  "A skull. ",
  "The Moon. ",
  "A mixed-use development. ",
  "A lapsed friendship. ",
  "The ocean at night. ",
  "Unquestioning trust. ",
  "Clapping hands. ",
  "An unfair decision. ",
  "An old ceremony. ",
  "A house. ",
  "Broken window shades. ",
  "A journalist. ",
  "Gravity. ",
  "Graffiti. ",
  "An anonymous letter. ",
  "An envelope full of money. ",
  "Hesitant goodbyes. ",
  "A game with shifting rules. ",
  "A cleanup. ",
  "Fossils. ",
  "A misinterpreted joke. ",
  "A bus. ",
  "A student. ",
  "A theft. ",
  "A mortgage. ",
  "A teacher. ",
  "The question. ",
  "Cousins. ",
  "An unsteady ladder. ",
  "A lose-lose scenario. ",
  "An auction. ",
  "An unlikely number. ",
  "The earliest known record. ",
  "A game with no winner. ",
  "Mutual influence. ",
  "Oil. ",
  "Ink. ",
  "A last hurrah. ",
  "The last, best chance. ",
  "An unfair match-up. ",
  "A revisionist history. ",
  "A second iteration. ",
  "A fortunate discovery. ",
  "Excessive politeness. ",
  "Thwarted ambition. ",
  "Hapless regulation. ",
  "A new job. ",
  "Avoidable debt. ",
  "Time in prison. ",
  "A new world. ",
  "A global project. ",
  "A regrettable date. ",
  "A disloyal animal. ",
  "Special privileges. ",
  "Dancers. ",
  "Night life. ",
  "The service industry. ",
  "A waiter. ",
  "A waitress. ",
  "Life during wartime. ",
  "A new law. ",
  "Hunger. ",
  "Irreconcilable differences. ",
  "Salvaged oak. ",
  "An umbrella. ",
  "An eclipse. ",
  "A blind spot. ",
  "An invisible college. ",
  "A pufferfish. ",
  "A coded message. ",
  "A landslide. ",
  "Sunburn. ",
  "A train station. ",
  "An airport. ",
  "An abandoned city. ",
  "The unutterable. ",
  "A secret room. ",
  "Tax evasion. ",
  "Absolute moral authority. ",
  "Late-night fast food. ",
  "A path through the forest. "
];

/**
 * The AlexaSkill prototype and helper functions
 */
var AlexaSkill = require('./AlexaSkill');

/**
 * RandomMuse is a child of AlexaSkill.
 */
var RandomMuse = function () {
    AlexaSkill.call(this, APP_ID);
};

// Extend AlexaSkill
RandomMuse.prototype = Object.create(AlexaSkill.prototype);
RandomMuse.prototype.constructor = RandomMuse;

RandomMuse.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
    console.log("RandomMuse onSessionStarted requestId: " + sessionStartedRequest.requestId
        + ", sessionId: " + session.sessionId);
    // any initialization logic goes here
};

RandomMuse.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
    console.log("RandomMuse onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
    handleNewIdeaRequest(response);
};

/**
 * Overridden to show that a subclass can override this function to teardown session state.
 */
RandomMuse.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
    console.log("RandomMuse onSessionEnded requestId: " + sessionEndedRequest.requestId
        + ", sessionId: " + session.sessionId);
    // any cleanup logic goes here
};

RandomMuse.prototype.intentHandlers = {
    "GetNewIdeaIntent": function (intent, session, response) {
        handleNewIdeaRequest(response);
    },

    "AMAZON.HelpIntent": function (intent, session, response) {
        response.ask("You can ask me to give you a random idea, or, you can say exit... What can I help you with?", "What can I help you with?");
    },

    "AMAZON.StopIntent": function (intent, session, response) {
        var speechOutput = "Goodbye";
        response.tell(speechOutput);
    },

    "AMAZON.CancelIntent": function (intent, session, response) {
        var speechOutput = "Goodbye";
        response.tell(speechOutput);
    }
};

/**
 * Gets a random new idea from the list and returns to the user.
 */
function handleNewIdeaRequest(response) {

    // Get a randomized set of ideas from the idea list

    var ideaIndex = Math.floor(Math.random() * IDEAS.length);
    var ideaIndexTwo = Math.floor(Math.random() * IDEAS.length);
    var ideaIndexThree = Math.floor(Math.random() * IDEAS.length);

    // Prevent the same idea from occurring more than once

    while (ideaIndex == ideaIndexTwo) {
        var ideaIndexTwo = Math.floor(Math.random() * IDEAS.length);
    }

    while (ideaIndexTwo == ideaIndexThree) {
        var ideaIndexThree = Math.floor(Math.random() * IDEAS.length);
    }

    while (ideaIndex == ideaIndexThree) {
        var ideaIndexThree = Math.floor(Math.random() * IDEAS.length);
    }

    var ideaOne = IDEAS[ideaIndex];
    var ideaTwo = IDEAS[ideaIndexTwo];
    var ideaThree = IDEAS[ideaIndexThree];

    // Create speech output

    var speechOutput = ideaOne + ideaTwo + ideaThree;

    response.tellWithCard(speechOutput, "Random Muse", speechOutput);
}

// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {
    // Create an instance of the RandomMuse skill.
    var randomMuseSkill = new RandomMuse();
    randomMuseSkill.execute(event, context);
};

Credits

Eric Gregory

Eric Gregory

1 project • 1 follower

Comments