With this skill, you can ask Alexa to tell you an Inspirational Quote to start a better day.
Setup AWS Lambda- Go to the AWS Console and click on the Lambda link.
Note: Ensure you are in us-east or you won't be able to use Alexa with Lambda.
- Click on the
Create a Lambda Function
orGet Started Now
button.
- Skip the blueprint.
- Name the Lambda Function "
inspire
".
- Select the runtime as
Node.js
- Create
AlexaSkill.js
andindex.js
files from the respective codes included in the code section below. Remember to change the App ID inindex.js
.
- Select the above two files and then create a zip file.
- Select Code entry type as "
Upload a .ZIP file
" and then upload the .zip file to the Lambda.
- Keep the Handler as
index.handle.
- Create a
basic execution role
and click Create.
- Leave the Advanced settings as the defaults.
- Click "Next" and review the settings then click "Create Function".
- Click the "Event Sources" tab and select "Add event source".
- Set the Event Source type as
Alexa Skills kit
and Enable it now. Click Submit.
- Copy the ARN from the top right to be used later in the Alexa Skill Setup.
- Go to the Alexa Console and click
Add a New Skill
.
- Set "
Inspiratonal Quote
" as the skill name and "inspire
" as the invocation name, this is what is used to activate your skill. For example you would say: "Alexa, ask Inspire to tell an inspirational quote".
- Select the Lambda ARN for the skill Endpoint and paste the ARN copied from above. Click Next.
- Copy the Intent Schema from the Intent Schema include in the code section below.
- Copy the Sample Utterances from the Sample Utterances included in the code section below. Click Next.
- Go back to the skill Information tab and copy the App Id. Paste the App ID into the
index.js
file for the variableAPP_ID,
then update the Lambda source zip file with this change and upload to Lambda again, this step makes sure the Lambda function only serves request from authorized source.
- You are now able to start testing your sample skill! You should be able to go to the Alexa Skills webpage and see your skill enabled.
- In order to test it, try to say some of the Sample Utterances from the Examples section below.
- Your skill is now saved and once you are finished testing you can continue to publish your skill.
.
Code (node.js)The code setup the skill ID and API options.
var APP_ID = "your app id";
var http = require("http");
var optionsGet = {
host : 'api.forismatic.com',
port : 80,
path : '',
method : 'GET'
};
Here are the various intent handlers.
Inspire.prototype.intentHandlers = {
"GetQuote": function (intent, session, response) {
getQuote(intent, session, response);
},
"AMAZON.HelpIntent": function (intent, session, response) {
helpTheUser(intent, session, response);
},
"AMAZON.StopIntent": function (intent, session, response) {
var speechOutput = "Goodbye";
response.tell(speechOutput);
},
"AMAZON.CancelIntent": function (intent, session, response) {
var speechOutput = "Goodbye";
response.tell(speechOutput);
}
};
Following is the main getQuote function. It setup to API path and make a REST call. Upon successful getting data, it will construct the returned inspirational quote. It will tell if there is no quote returned or an error occurs.
function getQuote(intent, session, response) {
var speechText = "",
repromptText = "",
speechOutput,
repromptOutput;
optionsGet.path = "/api/1.0/?method=getQuote&key=457653&format=json&lang=en";
// do the GET request
var reqGet = http.request(optionsGet, function(res) {
console.log("statusCode: ", res.statusCode);
// uncomment it for header details
// console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('GET result:\n');
process.stdout.write(d);
console.info('\n\nCall completed');
var obj = JSON.parse(d);
speechText = obj.quoteText;
if (speechText == "[]") {
speechText = "Sorry. There in inspirational quote. Please try again later.";
}
repromptText = "<speak>" + obj.quoteText + "</speak>";
speechOutput = {
speech: speechText,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
repromptOutput = {
speech: repromptText,
type: AlexaSkill.speechOutputType.SSML
};
response.tell(speechText);
// response.ask(speechOutput, repromptOutput);
});
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
speechText = "Sorry. There in inspirational quote. Please try again later.";
repromptText = "Sorry. There in inspirational quote. Please try again later.";
speechOutput = {
speech: speechText,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
repromptOutput = {
speech: repromptText,
type: AlexaSkill.speechOutputType.SSML
};
response.tell(speechOutput, repromptOutput);
});
}
This function guide the user how to interact with the skill.
function helpTheUser(intent, session, response) {
var speechText = "You can ask for an inspirational quote. " +
"Now, what can I help you with?";
var repromptText = "<speak> I'm sorry I didn't understand that. You can ask for an inspiration quote. " +
"Now, what can I help you with? </speak>";
var speechOutput = {
speech: speechText,
type: AlexaSkill.speechOutputType.PLAIN_TEXT
};
var repromptOutput = {
speech: repromptText,
type: AlexaSkill.speechOutputType.SSML
};
response.ask(speechOutput, repromptOutput);
}
.
Voice UIThis is a very simple one-shot model Voice UI.
.
Skill LinkHere is the published link to this skill.
Comments