Skip to content

Commit

Permalink
Add an Alexa Skill example. (pulumi#9)
Browse files Browse the repository at this point in the history
This example demonstrates how to build and deploy an AWS Lambda
function that implements an Alexa Skill. The particular skill supports
two intents: the launch intent and "GetFortune", both of which choose a
random entry from a user-supplied fortune file for Alexa to speak.
  • Loading branch information
pgavlin committed Jan 18, 2018
1 parent a1a34e9 commit 0626734
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions alexa-fortune/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: alexa-fortune
runtime: nodejs
description: A fortune teller for Alexa.
42 changes: 42 additions & 0 deletions alexa-fortune/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as pulumi from "pulumi";
import * as aws from "@pulumi/aws";

// First, create a config object and read the following user-provided values:
// - The Alexa app ID for our skill
// - The path to the file that contains the fortunes our skill will return.
const config = new pulumi.Config("alexa-fortune:config");
const alexaAppId = config.require("app-id");
const fortunesFilePath = config.require("fortunes");

// Next, create the lambda function we'll use to serve fortunes. We'll do this by first writing our handler, then
// creating the function resource itself.
async function getFortune(): Promise<string> {
const fs = await import("fs");
const fortunes = fs.readFileSync(fortunesFilePath, "utf8").split("\n%\n");
const fortune = fortunes[Math.floor(Math.random() * fortunes.length)];
return fortune.replace(/\s+/g, " ");
}
async function handler(event: any, context: aws.serverless.Context, callback: (error: any, result: any) => void): Promise<any> {
const alexa = (await import("alexa-sdk")).handler(event, <any>(context), callback);
alexa.appId = alexaAppId;
alexa.registerHandlers({
"LaunchRequest": function() {
this.emit("GetFortune");
},
"GetFortune": async function() {
this.emit(":tell", await getFortune());
},
});
alexa.execute();
};
const func = new aws.serverless.Function("lambda", {policies: []}, handler);

// Next, grant Alexa permission to invoke the lambda function we just created.
const alexaPermission = new aws.lambda.Permission("alexa-permission", {
action: "lambda:InvokeFunction",
function: func.lambda,
principal: "alexa-appkit.amazon.com",
});

// Finally, export the ARN of the lambda so that we can hook it up to our Alexa skill.
export const lambdaArn = func.lambda.arn;
21 changes: 21 additions & 0 deletions alexa-fortune/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "alexa-fortune",
"version": "1.0",
"main": "bin/index.js",
"typings": "bin/index.d.ts",
"scripts": {
"build": "tsc"
},
"devDependencies": {
"@types/alexa-sdk": "^1.0.11",
"@types/node": "^8.0.27",
"typescript": "^2.1.4"
},
"dependencies": {
"alexa-sdk": "^1.0.25"
},
"peerDependencies": {
"@pulumi/aws": "*",
"@pulumi/cloud": "*"
}
}
11 changes: 11 additions & 0 deletions alexa-fortune/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node"
},
"files": [
"index.ts"
]
}

0 comments on commit 0626734

Please sign in to comment.