简体   繁体   English

如何使用Amazon Alexa访问本地API

[英]How can I access a local API using Amazon Alexa

I intend to build a set of skills for Amazon Alexa that will integrate with a custom software suite that runs on a RaspberryPi in my home. 我打算为亚马逊Alexa构建一套技能,它将与我家中的RaspberryPi上运行的自定义软件套件集成。

I am struggling to figure out how I can make the Echo / Dot itself make an API call to the raspberry pi directly - without going through the internet, as the target device will have nothing more then an intranet connection - it will be able to receive commands from devices on the local network, but is not accessible via the world. 我正在努力弄清楚如何让Echo / Dot本身直接对raspberry pi进行API调用 - 无需通过互联网,因为目标设备将只有内联网连接 - 它将能够接收来自本地网络上的设备的命令,但无法通过世界访问。

From what I have read, the typical workflow is as follows 根据我的阅读,典型的工作流程如下

Echo -> Alexa Service -> Lambda

Where a Lambda function will return a blob of data to the Smart Home device; Lambda函数将向智能家居设备返回一大块数据; using this return value 使用此返回值

Is it possible, and how can I make the Alexa device itself make an API request to a device on the local network, after receiving a response from lambda? 是否有可能,在收到lambda的响应后,如何让Alexa设备本身向本地网络上的设备发出API请求?

I have the same problem and my solution is to use SQS as the message bus so that my RaspberryPi doesn't need to be accessible from the internet. 我有同样的问题,我的解决方案是使用SQS作为消息总线,以便我的RaspberryPi不需要从互联网访问。

Echo <-> Alexa Service <-> Lambda -> SQS -> RaspberryPi
                             A                 |
                             +------ SQS <-----+

This works fine as long as: 这个工作正常,只要:

  • you enable long polling (20sec) of SQS on the RaspberryPi and set the max messages per request to 1 您在RaspberryPi上启用SQS的长轮询(20秒)并将每个请求的最大消息数设置为1
  • you don't have concurrent messages going back and forth between Alexa and the RaspberryPi 你没有在Alexa和RaspberryPi之间来回传递的并发消息

This give the benefit of: 这样做的好处是:

  • with a max message size of 1 the SQS request will return as soon as one message is available in the queue, even before the long poll timeout is met 最大消息大小为1时,即使在满足长轮询超时之前,只要队列中有一条消息可用,SQS请求就会返回
  • with only 1 long polling at a time to SQS for the entire month this fit under the SQS free tier of 1 million requests 在SQS的整个月内,每次只进行1次长时间轮询,这符合SQS免费等级的100万次请求
  • no special firewall permission for accessing your RaspberryPi from the internet, so the RaspberryPi's connection from the lambda always "just works" 从互联网访问您的RaspberryPi没有特殊的防火墙权限,因此RaspberryPi与lambda的连接总是“正常”
  • more secure than exposing your RaspberryPi to the internet since there are no open ports exposed for malicious programs to attack 比将RaspberryPi暴露给互联网更安全,因为没有暴露的恶意程序攻击端口

You could try using AWS IoT: 您可以尝试使用AWS IoT:

Echo <-> Alexa Service <-> Lambda <-> IoT <-> RaspberryPi

I though about using this for my Alexa RasberryPi project but abandoned the idea since AWS IoT doesn't offer a permanent free tier. 我虽然将这个用于我的Alexa RasberryPi项目,但放弃了这个想法,因为AWS IoT不提供永久免费套餐。 But the free tier is no longer a concern since Amazon now offers Alexa AWS promotional credits. 但免费等级不再是一个问题,因为亚马逊现在提供Alexa AWS促销信用。 https://developer.amazon.com/alexa-skills-kit/alexa-aws-credits https://developer.amazon.com/alexa-skills-kit/alexa-aws-credits

One possibility is to install node-red on your rPi. 一种可能性是在rPi上安装node-red。 Node-red has plugins ( https://flows.nodered.org/node/node-red-contrib-alexa-local ) to simulate Philips hue and makes Alexa talk to it directly. Node-red有插件( https://flows.nodered.org/node/node-red-contrib-alexa-local )来模拟飞利浦色调,让Alexa直接与它对话。 It's an instant response. 这是一个即时回应。 The downside is that it only works for 3 commands: on , off , set to x % . 缺点是它只适用于3个命令: onoffset to x % Works great for software/devices that control lights, shades and air-con. 适用于控制灯光,阴影和空调的软件/设备。

It was answered in this forum a while ago and I'm afraid to tell you that situation hasn't changed since: 不久前在这个论坛上得到了答复,我害怕告诉你情况没有改变,因为:

Alexa is cloud based and requires access to the internet / Amazon servers to function, so you cannot use it only within the intranet without external access. Alexa基于云,需要访问互联网/亚马逊服务器才能运行,因此您无法仅在内部网中使用它而无需外部访问。

There are a couple workaround methods I've seen used. 我见过几种解决方法。

The first method is one that I've used: I setup If This Then That (IFTTT) to listen for a specific phrase from Alexa, then transmit commands through the Telegram secure chat/messaging service where I used a "chat bot" running on my raspberry PI to read and act on those messages. 第一种方法是我使用过的方法:我设置If This Then That(IFTTT)来侦听来自Alexa的特定短语,然后通过Telegram安全聊天/消息服务传输命令,我在其中使用“聊天机器人”运行我的覆盆子PI阅读并处理这些消息。

The second method I most recently saw would use IFTTT to add rows to a google spreadsheet which the raspberry pi could monitor and act on. 我最近看到的第二种方法是使用IFTTT向谷歌电子表格中添加行,树莓派可以监视和操作。

I wasn't particularly happy with the performance/latency of either of these methods but if I wrote a custom Alexa service using a similar methodology it might at least eliminate the IFTTT delay. 我对这两种方法的性能/延迟都不是特别满意,但如果我使用类似的方法编写自定义的Alexa服务,它至少可以消除IFTTT延迟。

You can achieve this by using proxy. 您可以使用代理来实现此目的。 BST has a tool for that , I currently use that one http://docs.bespoken.tools/en/latest/commands/proxy/ BST有一个工具,我目前使用那个http://docs.bespoken.tools/en/latest/commands/proxy/

So rather than using a Lambda you can use local machine. 因此,您可以使用本地计算机而不是使用Lambda

Essentially it becomes Echo -> Alexa Service -> Local Machine 基本上它变成了Echo -> Alexa Service -> Local Machine

Install npm bst to your local machine https://www.npmjs.com/package/bespoken-tools 将npm bst安装到本地计算机https://www.npmjs.com/package/bespoken-tools

npm install bespoken-tools --save

Go to your projects index.js folder and run proxy command 转到项目index.js文件夹并运行proxy命令

bst proxy lambda index.js

This will give you a url as follow: https://proxy.bespoken.tools?node-id=xxx-xxx-xxx-xxx-xxxxxxxx 这将为您提供以下网址: https://proxy.bespoken.tools?node-id=xxx-xxx-xxx-xxx-xxxxxxxxhttps://proxy.bespoken.tools?node-id=xxx-xxx-xxx-xxx-xxxxxxxx xxx-xxx-xxx-xxx https://proxy.bespoken.tools?node-id=xxx-xxx-xxx-xxx-xxxxxxxx

Now go to your alexa skill on developer.amazon and click to configure your skill. 现在转到developer.amazon上的alexa技能,然后单击以配置您的技能。

Choose your service endpoint as https and enter the url printed out by BST 选择您的服务端点为https,然后输入BST打印出的URL

在此输入图像描述

Then click save, and boooom your local machine becomes the final end point. 然后单击“保存”,然后将本地计算机作为最终结束点。

只需使用https://ngrok.com/等服务打开到您的rPi的SSH隧道,然后将其作为您的端点或lambda进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM