This package provides various extensions to improve development with Alexa Skills Kit SDK v2.
npm install @corux/ask-extensions
The Alexa Extensions consist of two parts: generic code blocks for re-use in different skills and extensions to the ask-sdk framework.
The intention of the decorator framework is to provide an easily recognizable syntax to define request handlers.
Decorators replace the canHandle()
function of traditional request handlers.
To use the decorators, the handler must inherit from BaseRequestHandler
.
import {
BaseRequestHandler, IExtendedHandlerInput,
Fallback, Intents, Request,
} from "@corux/ask-extensions";
@Intents("AMAZON.StopIntent", "AMAZON.CancelIntent")
class StopIntentHandler extends BaseRequestHandler {
public handle(handlerInput: IExtendedHandlerInput): Response {
...
}
}
@Request("LaunchRequest")
class LaunchHandler extends BaseRequestHandler {
public handle(handlerInput: IExtendedHandlerInput): Response {
...
}
}
@Fallback()
class FallbackHandler extends BaseRequestHandler {
public handle(handlerInput: IExtendedHandlerInput): Response {
...
}
}
Use the extended response builder to conditionally build up a skill response.
class Handler extends BaseRequestHandler {
public handle(handlerInput: IExtendedHandlerInput): Response {
return handlerInput.getResponseBuilder()
.speakIfSupported(...)
.addRenderTemplateDirectiveIfSupported(...)
.addHintDirectiveIfSupported(...)
.addAplDirectiveIfSupported(...)
.if(checkCondition, (builder) => { ... })
.getResponse();
}
}
See a list of available handlers here.
See a list of available interceptors here.
The localization interceptor configures i18next for usage in the application. Localization files will be loaded from the filesystem.
SkillBuilders.custom()
.addRequestInterceptors(
new LocalizationInterceptor("./i18n/{{lng}}.json"),
)
The i18next t
function is added to both the IExtendedHandlerInput
and the request attributes.
class Handler extends BaseRequestHandler {
public handle(handlerInput: IExtendedHandlerInput): Response {
handlerInput.attributesManager.getRequestAttributes().t("key");
handlerInput.t("key");
}
}
SkillBuilders.custom()
.addRequestInterceptors(
new LogInterceptor(),
)
.addResponseInterceptors(
new LogInterceptor(),
)
Retrieves the device's timezone setting. The interceptor uses Luxon by default.
To access the settings service, the skill must be configured with an ApiClient.
SkillBuilders.custom()
.addRequestInterceptors(
new TimezoneInterceptor(),
)
.withApiClient(new DefaultApiClient())