如何创建从 HTTP/HTTPS API 获取数据的 Alexa 技能(在 AWS Lambda 上为 Node.js 使用“Alexa Skills Kit")
我想为 Amazon Alexa 创建一项技能——当由语音命令触发时——通过 HTTPS 请求从 API 获取一些信息,并将结果用作对 Alexa 用户的口头回答.
I want to create a skill for Amazon Alexa that - when triggered by voice commands - gets some information from an API via a HTTPS request and uses the result as spoken answer to the Alexa user.
由于 node.js 的事件驱动概念和 适用于 Node.js 的 Alexa Skills Kit.从用户那里获取参数也不是那么容易.
There is a little challenge here (especially for node.js newbies) due to the event-driven concept of node.js and the internals of the Alexa Skills Kit for Node.js. And getting a hand on parameters from the user isn't that easy, either.
有人可以提供一些示例代码吗?
Can somebody provide some sample code to start with?
Preliminaries
- 要开始使用,您需要一个 Amazon 帐户,并且必须为该帐户启用 AWS.
- 然后在亚马逊网站上有一个很好的分步指南:https://developer.amazon.com/edw/home.html#/skills
- 它会逐步引导您完成创建技能"的过程.技能是 Alexa 用自然语言回答问题的能力.在此过程中,您还将创建一个 Lambda 函数(选择创建一个演示脚本应用程序,您将自动获得所有必需的库)
- 然后您可以在 AWS 控制台的 WebUI 中编辑代码).
- 技能"会在您所有的个人 Alexa 设备上自动启用,例如我在家中的 Amazon Echo dot.
- 请记住,您可以在 AWS 控制台的 AWS Cloudwatch 部分查看控制台输出.
当我创建我的第一个 Alexa Skill 时,我是新的 node.js、Lambda 和 Alexa Skills SDK.所以我遇到了一些问题.我想在此为下一个遇到相同问题的人记录解决方案.
While I created my first Alexa Skill I was new node.js, Lambda and the Alexa Skills SDK. So I ran into a few problems. I'd like to document the solutions here for the next person who runs into the same problem.
- 当您使用 https.get() 在 node.js 中发出 http get 请求时,您需要为结束回调提供一个处理程序,例如 res.on('end', function(res) {});
- 当您调用 this.emit(':tell', 'blabla'); 时,答案会从 Lambda 脚本发送回 Alexa 服务(这是来自AWS).但是在最终处理程序中this"不再是正确的this",您需要事先存储句柄(我使用 mythis 这样做有点歪,我确信有更聪明的解决方案,但它有效).
- When you make an http get request in node.js using https.get() you need to provide a handler for the end callback like res.on('end', function(res) {});
- The answer is sent back from the Lambda script to the Alexa Service when you call this.emit(':tell', 'blabla'); (this is what is used in the samples from AWS). But in the end-handler "this" isn't the right "this" anymore, you need to store the handle beforehand (I am doing this a little crookedly using mythis, I am sure there are smarter solutions, but it works).
如果我有以下代码片段,我可以轻松地节省两个小时的调试时间.:-)
I would have easily saved two hours of debugging had I had the following code snippet. :-)
我的示例 lambda 脚本已经从 API 获取了预先格式化的文本.如果您调用 XML/JSON 或任何需要更多处理答案的 API.
I my sample the lambda script already gets the preformatted text from the API. If you call an XML/JSON or whatever API you need to work with the answer a bit more.
'use strict';
const Alexa = require('alexa-sdk');
var https = require('https');
const APP_ID = ''; // TODO replace with your app ID (OPTIONAL).
const handlers = {
'functionwithoutdata': function() {
var responseString = '';
var mythis = this;
https.get('**YOURURL**?**yourparameters**', (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
responseString += d;
});
res.on('end', function(res) {
const speechOutput = responseString;
console.log('==> Answering: ', speechOutput);
mythis.emit(':tell', 'The answer is'+speechOutput);
});
}).on('error', (e) => {
console.error(e);
});
},
'functionwithdata': function() {
var mydata = this.event.request.intent.slots.mydata.value;
console.log('mydata:', mydata);
var responseString = '';
var mythis = this;
https.get('**YOURURL**?**yourparameters**&mydata=' + mydata, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
responseString += d;
});
res.on('end', function(res) {
const speechOutput = responseString;
console.log('==> Answering: ', speechOutput);
mythis.emit(':tell', 'The answer is'+speechOutput);
});
}).on('error', (e) => {
console.error(e);
});
}
};
exports.handler = (event, context) => {
const alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};