Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample Alexa Skill handler for Auto-Delegated Dialog
export class FindDogIntentHandler implements RequestHandler {
canHandle(handlerInput: HandlerInput): boolean {
const request = handlerInput.requestEnvelope.request;
return ((request.type === 'IntentRequest' && request.intent.name === 'FindADogIntent' && request.dialogState == 'COMPLETED')
|| (handlerInput.requestEnvelope.request.type === 'LaunchRequest'));
}
async handle(handlerInput: HandlerInput): Promise<Response> {
const { requestEnvelope, serviceClientFactory, responseBuilder } = handlerInput
const context = requestEnvelope.context;
let rb = responseBuilder.getResponse();
let currentIntent;
if (requestEnvelope.request.type === 'IntentRequest') {
currentIntent = requestEnvelope.request.intent;
}
if (context) {
const {apiAccessToken, apiEndpoint, device} = context.System;
const aas = new AlexaApiService(apiAccessToken, apiEndpoint, device, serviceClientFactory);
try {
const address: services.deviceAddress.ShortAddress = await aas.callAddressServiceClientApi();
console.log('[SUCCESS] - Retrieving address info', address.postalCode);
const { attributesManager } = handlerInput;
let sessionAttributes = attributesManager.getSessionAttributes();
sessionAttributes['postalCode'] = address.postalCode;
attributesManager.setSessionAttributes(sessionAttributes);
rb = responseBuilder
.speak(MessageHelper.randomMessage(RandomMessageTypes.WELCOME))
.getResponse();
} catch (error) {
// throw error here to be caught by the custom error handler
throw error;
}
} else {
// throw generic error if there is no context object (unlikely)
throw new Error('Context object not found')
}
return rb;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment