Software apps and online services | ||||||
| ||||||
|
I built a fact skill as a "bolt on" skill to my Mystic Mirror project (the mirror is going to my kids room; I wanted a skill that they can use to learn history). This skill will give the use a fact about African American history. The link to the published skill is here: http://alexa.amazon.com/spa/index.html#skills/dp/B01JL5H21O
Follow the linked instructions below to install your own Fact skill (or you can reference the code attached and install this skill).
index.js
JavaScriptUpload to Lambda (with second js file). Be sure to update the placeholder for the Skill ID
/**
Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file 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.
*/
/**
* This simple sample has no external dependencies or session management, and shows the most basic
* example of how to create a Lambda function for handling Alexa Skill requests.
*
* Examples:
* One-shot model:
* User: "Alexa, ask Black History Facts for a fact"
* Alexa: "Here's your Black History fact: ..."
*/
/**
* App ID for the skill
*/
var APP_ID = 'YOUR_SKILL_ID'; //OPTIONAL: replace with "amzn1.echo-sdk-ams.app.[your-unique-value-here]";
/**
* Array containing Black History facts.
*/
var FACTS = [
"Spurred by growing racial violence in the early twentieth century, and particularly by race riots in Springfield Illinois in 1908, a group of African American leaders joined together to form a new permanent civil rights organization, the National Association for the Advancement of Colored People (NAACP).",
"Jack Johnson became the first African-American man to hold the World Heavyweight Champion boxing title in 1908. He held on to the belt until 1915.",
"Thurgood Marshall was the first African American ever appointed to the United States Supreme Court. He was appointed by President Lyndon B. Johnson, and served on the Supreme Court from 1967 to 1991.",
"George Washington Carver developed 300 derivative products from peanuts among them cheese, milk, coffee, flour, ink, dyes, plastics, wood stains, soap, linoleum, medicinal oils and cosmetics.",
"Hiram Rhodes Revels was the first African American ever elected to the United States Senate. He represented the state of Mississippi from February 1870 to March 1871.",
"Shirley Chisholm was the first African American woman elected to the House of Representatives. She was elected in 1968 and represented the state of New York. She broke ground again four years later in 1972 when she was the first major party African-American candidate and the first female candidate for president of the United States.",
"In 1992, Dr. Mae Jemison became the first African American woman to go into space aboard the space shuttle Endeavor. During her 8-day mission she worked with U.S. and Japanese researchers, and was a co-investigator on a bone cell experiment.",
"Bessie Coleman was the first licensed African-American pilot in the world. She received aviation instruction in France.",
"Arthur Ashe was the first African-American to also win the U.S. Open in 1968, to win the men's singles at Wimbledon in 1975, and the first to be inducted into the International Tennis Hall of Fame in 1985.",
"Lee Elder was the first African-American golfer to play in the Masters Tournament in 1975. He has won 4 PGA tournaments and 8 Senior PGA tournaments in his career.",
"Madame C.J. Walker invented specialized hair products for African-American hair and became the first American woman to become a millionaire.",
"Diahann Carroll was the first African-American woman to have her own television series, Julia in 1968. It was a controversial, yet Nielsen top ten rated show about a single working mother raising her child.",
"Ralph J. Bunche , a politician and a U.N. diplomat, was the first African-American to win the Nobel Peace prize in 1950 for mediating the Arab-Israeli truce.",
"The African Methodist Episcopal Church, founded by Richard Allen, became the first national black church in the United States in 1816.",
"Henry Ossian Flipper was the first African-American to graduate from West Point academy in 1877 and became the first black commander when he was assigned to the 10th Cavalry, a Buffalo Soldier regiment.",
"Marian Anderson, a gifted contralto singer, was the first African-American to perform with the New York Metropolitan Opera in 1955.",
"The Tuskegee Airmen were the first African-American pilots in the U.S. armed forces. Beginning in 1941, select groups of extensively tested and rigorously trained African-Americans were trained at The Tuskegee Institute in Alabama.",
"Jocelyn Elders was the first African-American and the second woman to serve as the United States Surgeon General.",
"Booker T. Washington was the first African-American to be honored on a U.S. stamp, in 1940.",
"Elijah McCoy invented an automatic lubricator for oiling steam engines in 1872. The term, the real McCoy, is believed to be a reference about the reliability of Elijah McCoy's invention.",
"Garrett Augustus Morgan invented, among many other things, a 3-way automatic stop sign, which he sold to General Electric. It was used in the U.S. until the 3-light traffic sign was developed.",
"Dr. Charles Drew discovered techniques to store blood and developed blood banks.",
"Lewis Howard Latimer invented the carbon filament for light bulbs in 1881.",
"Track and Field star, Jesse Owens broke many records at the 1936 Olympic games in Berlin, including becoming the first athlete to win four gold medals in one Olympiad.",
"Music composer and producer, Quincy Jones is the most Grammy-nominated artist in the history of the awards with 76 nominations and 26 awards.",
"Golfer, Tiger Woods is the youngest person and the first African-American to win the Masters Tournament in 1997 and by a record breaking lead of 12 strokes.",
"On May 17, 1954, the U.S. Supreme Court delivered its verdict in Brown v. Board of Education, ruling unanimously that racial segregation in public schools violated the 14th Amendments mandate of equal protection.",
"On December 1, 1955, an African-American woman named Rosa Parks was riding a city bus in Montgomery, Alabama when the driver told her to give up her seat to a white man. Parks refused, and was arrested for violating the citys racial segregation ordinances.",
"Sidney Poitier was the first African-American to win an Academy Award for the Best Actor in 1963.",
"Jackie Robinson was the first black player to play in Major League Baseball",
"Harriet Tubman was nicknamed Moses, as she helped hundreds of slaves escape to the northern U.S and Canada.",
"Black History Month began as Negro History Week, which was created in 1926 by Carter G. Woodson, a noted African American historian, scholar, educator, and publisher. It became a month-long celebration in 1976. The month of February was chosen to coincide with the birthdays of Frederick Douglass and Abraham Lincoln."
];
/**
* The AlexaSkill prototype and helper functions
*/
var AlexaSkill = require('./AlexaSkill');
/**
* SpaceGeek is a child of AlexaSkill.
* To read more about inheritance in JavaScript, see the link below.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance
*/
var Fact = function () {
AlexaSkill.call(this, APP_ID);
};
// Extend AlexaSkill
Fact.prototype = Object.create(AlexaSkill.prototype);
Fact.prototype.constructor = Fact;
Fact.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
//console.log("onSessionStarted requestId: " + sessionStartedRequest.requestId + ", sessionId: " + session.sessionId);
// any initialization logic goes here
};
Fact.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
//console.log("onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
handleNewFactRequest(response);
};
/**
* Overridden to show that a subclass can override this function to teardown session state.
*/
Fact.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
//console.log("onSessionEnded requestId: " + sessionEndedRequest.requestId + ", sessionId: " + session.sessionId);
// any cleanup logic goes here
};
Fact.prototype.intentHandlers = {
"GetNewFactIntent": function (intent, session, response) {
handleNewFactRequest(response);
},
"AMAZON.HelpIntent": function (intent, session, response) {
response.ask("You can say tell me a Black History fact, 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 fact from the list and returns to the user.
*/
function handleNewFactRequest(response) {
// Get a random fact from the list
var factIndex = Math.floor(Math.random() * FACTS.length);
var randomFact = FACTS[factIndex];
// Create speech output
var speechOutput = "Here's your Black History fact: " + randomFact;
var cardTitle = "Your Black History Fact";
response.tellWithCard(speechOutput, cardTitle, speechOutput);
}
// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {
// Create an instance of the Black History skill.
var fact = new Fact();
fact.execute(event, context);
};
/**
Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file 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.
*/
'use strict';
function AlexaSkill(appId) {
this._appId = appId;
}
AlexaSkill.speechOutputType = {
PLAIN_TEXT: 'PlainText',
SSML: 'SSML'
}
AlexaSkill.prototype.requestHandlers = {
LaunchRequest: function (event, context, response) {
this.eventHandlers.onLaunch.call(this, event.request, event.session, response);
},
IntentRequest: function (event, context, response) {
this.eventHandlers.onIntent.call(this, event.request, event.session, response);
},
SessionEndedRequest: function (event, context) {
this.eventHandlers.onSessionEnded(event.request, event.session);
context.succeed();
}
};
/**
* Override any of the eventHandlers as needed
*/
AlexaSkill.prototype.eventHandlers = {
/**
* Called when the session starts.
* Subclasses could have overriden this function to open any necessary resources.
*/
onSessionStarted: function (sessionStartedRequest, session) {
},
/**
* Called when the user invokes the skill without specifying what they want.
* The subclass must override this function and provide feedback to the user.
*/
onLaunch: function (launchRequest, session, response) {
throw "onLaunch should be overriden by subclass";
},
/**
* Called when the user specifies an intent.
*/
onIntent: function (intentRequest, session, response) {
var intent = intentRequest.intent,
intentName = intentRequest.intent.name,
intentHandler = this.intentHandlers[intentName];
if (intentHandler) {
console.log('dispatch intent = ' + intentName);
intentHandler.call(this, intent, session, response);
} else {
throw 'Unsupported intent = ' + intentName;
}
},
/**
* Called when the user ends the session.
* Subclasses could have overriden this function to close any open resources.
*/
onSessionEnded: function (sessionEndedRequest, session) {
}
};
/**
* Subclasses should override the intentHandlers with the functions to handle specific intents.
*/
AlexaSkill.prototype.intentHandlers = {};
AlexaSkill.prototype.execute = function (event, context) {
try {
console.log("session applicationId: " + event.session.application.applicationId);
// Validate that this request originated from authorized source.
if (this._appId && event.session.application.applicationId !== this._appId) {
console.log("The applicationIds don't match : " + event.session.application.applicationId + " and "
+ this._appId);
throw "Invalid applicationId";
}
if (!event.session.attributes) {
event.session.attributes = {};
}
if (event.session.new) {
this.eventHandlers.onSessionStarted(event.request, event.session);
}
// Route the request to the proper handler which may have been overriden.
var requestHandler = this.requestHandlers[event.request.type];
requestHandler.call(this, event, context, new Response(context, event.session));
} catch (e) {
console.log("Unexpected exception " + e);
context.fail(e);
}
};
var Response = function (context, session) {
this._context = context;
this._session = session;
};
function createSpeechObject(optionsParam) {
if (optionsParam && optionsParam.type === 'SSML') {
return {
type: optionsParam.type,
ssml: optionsParam.speech
};
} else {
return {
type: optionsParam.type || 'PlainText',
text: optionsParam.speech || optionsParam
}
}
}
Response.prototype = (function () {
var buildSpeechletResponse = function (options) {
var alexaResponse = {
outputSpeech: createSpeechObject(options.output),
shouldEndSession: options.shouldEndSession
};
if (options.reprompt) {
alexaResponse.reprompt = {
outputSpeech: createSpeechObject(options.reprompt)
};
}
if (options.cardTitle && options.cardContent) {
alexaResponse.card = {
type: "Simple",
title: options.cardTitle,
content: options.cardContent
};
}
var returnResult = {
version: '1.0',
response: alexaResponse
};
if (options.session && options.session.attributes) {
returnResult.sessionAttributes = options.session.attributes;
}
return returnResult;
};
return {
tell: function (speechOutput) {
this._context.succeed(buildSpeechletResponse({
session: this._session,
output: speechOutput,
shouldEndSession: true
}));
},
tellWithCard: function (speechOutput, cardTitle, cardContent) {
this._context.succeed(buildSpeechletResponse({
session: this._session,
output: speechOutput,
cardTitle: cardTitle,
cardContent: cardContent,
shouldEndSession: true
}));
},
ask: function (speechOutput, repromptSpeech) {
this._context.succeed(buildSpeechletResponse({
session: this._session,
output: speechOutput,
reprompt: repromptSpeech,
shouldEndSession: false
}));
},
askWithCard: function (speechOutput, repromptSpeech, cardTitle, cardContent) {
this._context.succeed(buildSpeechletResponse({
session: this._session,
output: speechOutput,
reprompt: repromptSpeech,
cardTitle: cardTitle,
cardContent: cardContent,
shouldEndSession: false
}));
}
};
})();
module.exports = AlexaSkill;
{
"intents": [
{
"intent": "GetNewFactIntent"
},
{
"intent": "AMAZON.HelpIntent"
},
{
"intent": "AMAZON.StopIntent"
},
{
"intent": "AMAZON.CancelIntent"
}
]
}
GetNewFactIntent a fact
GetNewFactIntent a black history fact
GetNewFactIntent tell me a fact
GetNewFactIntent give me a fact
GetNewFactIntent tell me history
GetNewFactIntent give me history
GetNewFactIntent give me a history fact
GetNewFactIntent tell me a history fact
GetNewFactIntent tell me trivia
GetNewFactIntent give me some information
GetNewFactIntent tell me something
GetNewFactIntent give me something
Darian Johnson
8 projects • 140 followers
Technologist. Music lover. Fitness enthusiast. Movie buff. Fan of sci-fi and comic books.
Comments