Skip to content

Instantly share code, notes, and snippets.

@AdonousTech
Last active October 19, 2019 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdonousTech/f25ecddf4e6aad14cf6011fbc81acea7 to your computer and use it in GitHub Desktop.
Save AdonousTech/f25ecddf4e6aad14cf6011fbc81acea7 to your computer and use it in GitHub Desktop.
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