如何在Alexa Skill中与其他Echo设备交互?以Kitchen、Bedroom为例
Alexa Skill跨设备启动与交互实现方案
一、功能可行性
这个功能完全可行。Alexa Skill支持通过定向指令将交互流转到指定Echo设备,核心依赖Alexa的Device Targeting能力和权限体系:
- 需先获取用户授权,允许Skill访问其关联的Echo设备列表
- 可通过REST API动态获取目标设备(如Bedroom的Echo)的设备ID
- 利用Alexa的
Speak或Dialog定向指令,将后续交互输出到指定设备
二、Node.js实现步骤
1. 配置Skill权限
在Alexa开发者控制台,为Skill开启Device Address和Profile权限,确保能获取用户设备列表。
2. 从REST API获取目标设备ID
// 调用外部API获取Bedroom设备的deviceId async function getTargetDeviceId() { const response = await fetch('你的REST API地址'); const data = await response.json(); return data.targetDeviceId; }
3. 发送定向交互指令
使用ask-sdk-core构建包含定向指令的响应:
const Alexa = require('ask-sdk-core'); const LaunchRequestHandler = { canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest'; }, async handle(handlerInput) { const targetDeviceId = await getTargetDeviceId(); // 构建定向到Bedroom设备的响应 return handlerInput.responseBuilder .speak('即将在卧室的Echo上继续对话') // 发送APL渲染指令到目标设备 .addDirective({ type: 'Alexa.Presentation.APL.RenderDocument', token: 'bedroom-dialog', target: { type: 'Alexa.DirectiveTarget.DeviceId', payload: { targetDeviceId } }, document: { type: 'APL', version: '1.7', mainTemplate: { items: [{ type: 'Text', text: '现在在卧室的Echo上进行交互' }] } } }) // 触发目标设备的对话流程 .addDirective({ type: 'Dialog.Delegate', target: { type: 'Alexa.DirectiveTarget.DeviceId', payload: { targetDeviceId } }, updatedIntent: { name: '你的自定义意图名称', slots: {} } }) .getResponse(); } }; // 注册处理器并导出Skill(省略其他配置)
三、Python实现步骤
1. 配置Skill权限
同Node.js,在开发者控制台开启设备访问权限。
2. 从REST API获取目标设备ID
import requests def get_target_device_id(): response = requests.get('你的REST API地址') data = response.json() return data['targetDeviceId']
3. 构建定向响应
使用ask-sdk-python库实现:
from ask_sdk_core.skill_builder import SkillBuilder from ask_sdk_core.dispatch_components import AbstractRequestHandler from ask_sdk_core.utils import is_request_type class LaunchRequestHandler(AbstractRequestHandler): def can_handle(self, handler_input): return is_request_type("LaunchRequest")(handler_input) def handle(self, handler_input): target_device_id = get_target_device_id() # 构建APL定向渲染指令 apl_directive = { "type": "Alexa.Presentation.APL.RenderDocument", "token": "bedroom-dialog", "target": { "type": "Alexa.DirectiveTarget.DeviceId", "payload": {"targetDeviceId": target_device_id} }, "document": { "type": "APL", "version": "1.7", "mainTemplate": { "items": [{"type": "Text", "text": "现在在卧室的Echo上进行交互"}] } } } # 构建对话定向指令 dialog_directive = { "type": "Dialog.Delegate", "target": { "type": "Alexa.DirectiveTarget.DeviceId", "payload": {"targetDeviceId": target_device_id} }, "updatedIntent": {"name": "你的自定义意图名称", "slots": {}} } return handler_input.response_builder.speak( "即将在卧室的Echo上继续对话" ).add_directive(apl_directive).add_directive(dialog_directive).response sb = SkillBuilder() sb.add_request_handler(LaunchRequestHandler()) # 导出Skill(省略其他配置)
关键注意事项
- 必须确保用户已授权Skill访问其设备,否则无法获取设备ID或发送定向指令
- 目标设备需处于在线状态,否则指令无法生效
- 并非所有Alexa指令都支持定向,需参考官方文档确认支持范围
内容的提问来源于stack exchange,提问作者Blcknx
相关产品推荐
相关产品推荐

