如何创建从HTTP / HTTPS API获取数据的Alexa技能(对AWS Lambda上的Node.js使用“Alexa Skills Kit”) [英] How do I create an Alexa Skill that gets data from an HTTP/HTTPS API (using "Alexa Skills Kit" for Node.js on AWS Lambda)

查看:563
本文介绍了如何创建从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?

推荐答案

预赛




  • 要开始,您需要一个亚马逊帐户,并且您必须为该帐户启用AWS。

  • 然后亚马逊网站上有一个很好的分步指南: https://developer.amazon.com/edw/home.html#/skills

  • 它将引导您逐步完成创造技能的过程。技能是Alexa用自然语言回答问题的能力。
    在此过程中,您还可以创建一个Lambda函数(选择创建一个演示脚本应用程序,然后自动获得所有必需的库)

  • 然后您可以编辑代码在AWS控制台的WebUI中。)

  • 所有个人Alexa设备上都会自动启用技能,例如我家中的Amazon Echo dot。

  • 请记住,您可以在AWS控制台的AWS Cloudwatch部分查看控制台输出。

  • Preliminaries

    • To get started you need an Amazon account, and you must enable AWS for the account.
    • Then there is a nice step-by-step guide on the Amazon Website: https://developer.amazon.com/edw/home.html#/skills
    • It walks you through step-by-step through the process of creating a "skill". A skill is the ability for Alexa to answer questions in natural language. During this process you also create a Lambda function (select to create one of the demo script applications, and you get all required libraries automatically)
    • Then you can then edit the code in the WebUI of the AWS Console).
    • The "skill" is automatically enabled on all your personal Alexa Devices, like my Amazon Echo dot at home.
    • remember that you can look at the console output in your AWS Cloudwatch section of the AWS console.
    • 当我创建我的第一个Alexa技能时,我是新的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.


      1. 当您在进行http get请求时node.js使用 https.get()你需要为结束回调提供一个处理程序,如 res.on('end',function(res){});

      2. 当你打电话给 this.emit(':tell','blabla'); 时,答案会从Lambda脚本发回给Alexa服务。是来自AWS的样本中使用的内容。但是在最终处理程序这个不再是正确的这个,你需要预先存储句柄(我使用mythis做一点歪曲,我相信有更聪明的解决方案,但它确实有效)。

      1. 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) {});
      2. 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();
      };

      这篇关于如何创建从HTTP / HTTPS API获取数据的Alexa技能(对AWS Lambda上的Node.js使用“Alexa Skills Kit”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆