Last active
October 19, 2019 04:55
-
-
Save AdonousTech/f25ecddf4e6aad14cf6011fbc81acea7 to your computer and use it in GitHub Desktop.
Sample Alexa Skill handler for Auto-Delegated Dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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