如果您阅读了我的那篇《Amazon Alexa and AWS Lambda文章,那么您已经知道如何使用 Apex 来部署自定义的 Alexa skill。在这篇文章里,您将学习如何使用 Serverless 框架将函数部署到 AWS Lambda,并通过 Amazon Echo 使用语音命令进行调用。
Alexa 是亚马逊非常棒的尝试,为我们的客厅引入了一个个人的话音控制助手,与苹果 Siri 或 Google 的服务相当。那些语音控制设备都是界面相关的,因此我显然更喜欢通过像 Alexa 这样的名称来呼叫服务,而不是整天说 OK Google。
Alexa 中支持的功能被称为 skill(技能),必须使用亚马逊的 Alexa 和 Echo 的配套应用程序激活。在引擎盖之下,它们只是配置的语音命令的列表,其中包含用于变量的占位符和 AWS Lambda 函数的 HTTPS 请求或直接调用。
亚马逊支持两种后端类型的 Alexa Skill:AWS Lambda 或使用 HTTPS 的自定义端点。由于没有人喜欢维护基础架构,所以让我们来使用 AWS Lambda。 当然,您不想使用 AWS 控制台手动配置您的设置; 您应该使用工具来设置所有资源。
我已经描述了如何使用 Apex 将功能部署到 AWS,但是 Serverless 将使旧的教程的一些步骤过时,您可以更快地开始开发您的自定义技能。
首先创建一个新项目:
$ > npm install -g serverless
$ > mkdir serverless-alexa-skill && cd serverless-alexa-skill
$ > serverless create --template aws-nodejs
Serverless: Generating boilerplate...
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v1.8.0
-------'
Serverless: Successfully generated boilerplate for template: "aws-nodejs"
Serverless: NOTE: Please update the "service" property in serverless.yml with your service name
Serverless 现在将创建一个 serverless.yml
配置文件和一个 handler.js
示例函数。使用 Amazon API Gateway 时,默认功能正常工作,但是当您打算使用 Amazon Alexa 时,这些功能大都是无用的配置。只需使用您喜爱的编辑器打开文件,并用以下行替换内容:
'use strict';
module.exports.answer = (event, context, callback) => {
callback(null, {done: true})
}
为了设置 Serverless 部署和使用回答函数,您还需要更新 serverless.yml
配置文件。只需将此配置替换为 serverless 的内容:
service: serverless-alexa-skill
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: eu-west-1
functions:
answer:
handler: handler.answer
events:
- alexaSkill
package:
exclude:
- node_modules/**
当您现在运行 sls deploy
时,serverless
框架将创建一个 Amazon CloudFormation 堆栈,并将您的功能部署到 AWS Lambda。
$ > sls deploy
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading service .zip file to S3 (7.76 MB)...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.........
Serverless: Stack update finished...
Service Information
service: serverless-alexa-skill
stage: dev
region: eu-west-1
api keys:
None
endpoints:
None
functions:
answer: serverless-alexa-skill-dev-answer
部署完成后,您可以使用命令行中的 sls
命令调用该函数:
$ > sls invoke -f answer
{
"done": true
}
基本上,这就是您处理自定义 Alexa Skill 要求的基础内容。
Amazon 要求您的 Lambda 函数返回 JSON 数据,以为 Alexa 处理信息。响应的最小结构归结为 - 当然 - ,Alexa 会说的句子、一些关于在您的手机上的 Alexa 伴侣应用程序中显示和评分答案的信息。
'use strict';
module.exports.answer = (event, context, callback) => {
callback(null, {
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "Alexa responds with this text"
},
"card": {
"content": "Message for the Alexa companion app.",
"title": "Title for the Message",
"type": "Simple"
},
"shouldEndSession": true
},
"sessionAttributes": {}
})
}
您将看到最后的函数在下一步之后如何看起来,或者您可以仔细观察 GitHub 代码库 当然...
要测试和使用 Alexa 技能,您需要注册一个免费的 Amazon 开发者帐户。登录到您的帐户后,您可以访问 Alexa 部分,并开始使用 Alexa Skills kit 来创建您的技能。
现在所有的自定义 Alexa 技能都需要通过命令来调用,例如,他们的名字。只要你不写一个用于商标(trademark)的技能,你需要配置一个名称和调用命令两个字,例如, Example App:
使用此配置,您的 Echo 将响应语音命令,如 Alexa, open Example App
和 Alexa, ask Example App
。
所有的 Alexa 技能都有一个所谓的交互模型,它列出了所有支持的命令及其参数的类型定义。要开始一个简单的技能,只需配置一个意图(intent
),并复制并粘贴一个典型技能的默认意图 intent
。
以下配置注册一个功能标识符 anwser
,其中一个名为 item
的参数和 AMAZON.NUMBER
作为其类型。
{
"intents": [
{
"intent": "answer",
"slots": [
{
"name": "item",
"type": "AMAZON.NUMBER"
}
]
},
{
"intent": "AMAZON.HelpIntent"
},
{
"intent": "AMAZON.StopIntent"
},
{
"intent": "AMAZON.CancelIntent"
}
]
}
Amazon 在命令中支持很多类型的插槽(slot
)。对于这个第一个示例应用程序,使用 AMAZON.NUMBER
是很好的,但还有更多的可用。
示例语句列表定义了您的技能将响应的所有请求。每一行都以意图 intent
标识符开始 - 在这种情况下,它是 answer
- 之后是您计划对 Alexa 说的句子。
answer what is {item}
answer what's {item}
连同您的技能的初始配置,Alexa 将会识别一个命令,只要您说出匹配模式 Alexa 的内容:Alexa, ask Example App what is {XYZ}
。
在亚马逊配置向导的下一个屏幕上,系统会提示您部署的函数的 AWS Lambda ARN
,以响应 Alexa 的请求。
ARN 函数将由您的 Amazon 帐户 ID 和 AWS Lambda 功能的名称组成。您可以使用命令行中的 sls
命令检索该函数的名称:
$ > sls info
...
functions:
answer: serverless-alexa-skill-dev-answer
要获取 Amazon 帐户 ID,只需使用 aws
命令行工具并运行:
$ > aws sts get-caller-identity --output text --query 'Account'
1234567890
您现在拥有帐户 ID 和功能名称,您可以轻松连接这两个值来获取 ARN 标识符。
arn:aws:lambda:eu-west-1:1234567890:function:serverless-alexa-skill-dev-answer
现在只需在您的应用程序配置中输入 ARN
,并继续使用安装向导为您的新的 Alexa 技能。
幸运的是,亚马逊有一个叫做 Alexa 服务模拟器(Service Simulator
)的东西,所以如果你打算在半夜调试和增强你的 Alex 技能,你就不必在房子里叫醒其他人。
如果你设置一切正确,你应该能够输入一个 What is 5?
的话语? 并显示已部署功能的静态响应。在这种情况下,Alexa 将会根据之前设置的 JSON 结构做出响应:Alexa responds with this text
。
每当遇到一些奇怪的行为时,请使用 sls logs -f
来查看 AWS Lambda 函数的所有日志。
当然,有一个自定义的 Alex 技能没有什么好处,它将始终以与你的问题相同的答案来回应。
用一点逻辑来调用你的 handler.js 代码,并让 Alexa 检查提供的输入,并对特殊项目使用不同的句子。 除了检查输入参数之外,这是为事件结构添加一些基本检查,以确保您的函数不会抛出异常的好时机:
'use strict'
const assert = require('assert')
const answer = (title, message) => {
return {
"version": "1.0",
"response": {
"outputSpeech": {
"type": "PlainText",
"text": message
},
"card": {
"content": message,
"title": title,
"type": "Simple"
},
"shouldEndSession": true
},
"sessionAttributes": {}
}
}
module.exports.answer = (event, context, callback) => {
try {
assert(event.session)
assert(event.session.application)
assert(event.request)
assert(event.request.intent)
assert(event.request.intent.name.toLowerCase() === 'answer')
assert(event.request.intent.slots.item.value)
} catch (e) {
callback(null, answer(
"Invalid request",
"Sorry, but I cannot handle your request"
))
}
var item = event.request.intent.slots.item.value
if (item * 1 === 42) {
callback(null, answer(
"42",
"42 is the answer to the Ultimate Question of Life, the Universe, and Everything!"
))
} else {
callback(null, answer(
"Asked for " + item,
"I don't know anything about " + item
))
}
}
在您部署代码之后,Alexa 可以回复不同的答案的问题 Alexa, ask Example App what is 5
和 Alexa, ask Example App what is 42
。 不是很棒吗?
您是否注意到,亚马逊的开发者控制台不需要为此更新?一旦您的函数配置好了,甚至发布了,您可以随时更新 AWS Lambda 功能,并增强您的 API 的功能,而无需一次又一次地进行配置或审阅过程。
除了 Alexa Service Simulator,
,您还可以使用设备进行测试,并在部署新版本的 Lambda 函数时开始与 Alexa 交谈。
如果您计划将其发布到 Alexa Skill Store,Amazon 向导中的最后两个步骤将涵盖有关您的技能的发布信息。只要你只是测试一个技能,这都是不需要的,现在,你们都有了开发自己的 Alexa 技能的基础知识。 玩的开心!
当然,GitHub 可以使用所有用于此示例的代码!查看我的 GitHub:serverless-alexa-skill!有几个框架和工具包,为您的 Node.js 技能开发提供了一个很好的起点。
原文链接:https://sbstjn.com/serverless-alexa-skill-for-amazon-echo-with-aws-lambda.html
观光\评论区