forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an Alexa Skill example. (pulumi#9)
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
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |