📅  最后修改于: 2023-12-03 15:41:20.253000             🧑  作者: Mango
在本文中,我们将探讨如何使用 TypeScript 和 Alexa Skill 来编辑房间中的灯光。我们将使用 Alexa Skills Kit SDK for TypeScript 来构建此技能。
我们将使用 yarn 来安装依赖。
yarn add ask-sdk-core ask-sdk-model
我们使用 Skill Builder 来构建技能。
import { SkillBuilders } from 'ask-sdk-core';
const builder = SkillBuilders.custom();
export const handler = builder.lambda();
我们可以使用 SDK 提供的 IntentRequestHandler
来处理请求。我们使用 Slot
来接收房间名称和灯光名称。
import { RequestHandler } from 'ask-sdk-core';
const ControlLightIntentHandler: RequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'ControlLightIntent';
},
handle(handlerInput) {
const { requestEnvelope, serviceClientFactory } = handlerInput;
const { intent } = requestEnvelope.request;
const roomName = intent.slots.roomName.value;
const lightName = intent.slots.lightName.value;
const lightStatus = intent.slots.lightStatus.value;
// TODO: Implement.
}
};
我们使用第三方服务例如 Phillips Hue 来控制灯光。使用 SDK 中的 ServiceClientFactory
类来连接。
const phillipsHueService = (await serviceClientFactory.getHouseholdList())[0];
const endpoints = await serviceClientFactory.getEndpointsForDirective('Alexa.PowerController', phillipsHueService.endpointId);
最后,我们发送指示来控制灯光。我们使用 Alexa.Directive
和 Alexa.PowerController
接口。我们还使用 Controller
以及 Endpoint
来表示要控制的灯光。
const controller = {
type: 'AlexaInterface',
interface: 'Alexa',
version: '3'
};
const endpoint = {
endpointId: endpoints[0].endpointId
};
const directive = {
header: {
namespace: 'Alexa.PowerController',
name: 'TurnOn',
messageId: sendMessageId(),
payloadVersion: '3'
},
endpoint,
payload: {}
};
const response = handlerInput.responseBuilder
.addDirective(directive)
.getResponse();
return response;
import { SkillBuilders } from 'ask-sdk-core';
import { RequestHandler } from 'ask-sdk-core';
const ControlLightIntentHandler: RequestHandler = {
canHandle(handlerInput) {
return (
handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
handlerInput.requestEnvelope.request.intent.name === 'ControlLightIntent'
);
},
async handle(handlerInput) {
const { requestEnvelope, serviceClientFactory } = handlerInput;
const { intent } = requestEnvelope.request;
// Get the room, light and state from the intent.
const roomName: string = intent.slots.roomName.value;
const lightName: string = intent.slots.lightName.value;
const lightState: string = intent.slots.lightState.value;
// Get the Phillips Hue service.
const phillipsHueService = (await serviceClientFactory.getHouseholdList())[0];
// Find the endpoint for the specified light.
const endpoints = await serviceClientFactory.getEndpointsForDirective('Alexa.PowerController', phillipsHueService.endpointId);
const endpoint = {
endpointId: endpoints[0].endpointId
};
// Create the directive.
const directive = {
header: {
namespace: 'Alexa.PowerController',
name: lightState === 'on' ? 'TurnOn' : 'TurnOff',
messageId: sendMessageId(),
payloadVersion: '3'
},
endpoint,
payload: {}
};
// Send the directive to turn the light on or off.
const response = handlerInput.responseBuilder
.addDirective(directive)
.getResponse();
return response;
}
};
const sendMessageId = (): string => {
return 'Message ID';
};
const builder = SkillBuilders.custom();
export const handler = builder.addRequestHandlers(ControlLightIntentHandler).lambda();
你现在已经学会了如何使用 TypeScript 和 Alexa Skill 通过 Phillips Hue 来控制灯光。你现在可以基于此代码构建您自己的 Alexa 技能。