UNPKG

intentful

Version:

Create Custom Skills with less headache

262 lines (261 loc) 12.1 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Locale = void 0; const ask_sdk_core_1 = require("ask-sdk-core"); const fs = __importStar(require("fs")); const helpers_1 = require("./helpers"); const types_1 = require("../types"); class Locale extends types_1.ModelProvider { constructor(props) { super(); this.localeValue = props.locale; this.props = props; this.intents = {}; this.props.cancelHandler = this.props.cancelHandler ? this.props.cancelHandler : (input) => { console.log('Inside default cancel handler'); return input.responseBuilder.speak('Goodbye').reprompt('Until again!').getResponse(); }; this.props.sessionEndedHandler = this.props.sessionEndedHandler ? this.props.sessionEndedHandler : (input) => { console.log('Inside default session ended handler'); return input.responseBuilder.speak('Ending session, goodbye').reprompt('Until again!').getResponse(); }; this.props.fallbackHandler = this.props.fallbackHandler ? this.props.fallbackHandler : (input) => { console.log('Inside default fallbackHandler'); return input.responseBuilder.speak("I am sorry I don't understand.").reprompt('Fell through!').getResponse(); }; this.props.helpHandler = this.props.helpHandler ? this.props.helpHandler : (input) => { console.log('Inside default help handler'); return input.responseBuilder.speak("I am sorry I can't help you.").reprompt("I am sorry I can't help you.").getResponse(); }; this.props.launchHandler = this.props.launchHandler ? this.props.launchHandler : (input) => { console.log('Inside default Launch Handler'); return input.responseBuilder.speak('Hello, welcome to the skill!').reprompt('Welcome').getResponse(); }; this.props.errorHandler = this.props.errorHandler ? this.props.errorHandler : (input, error) => { console.error('Inside default Error Handler', JSON.stringify(error), JSON.stringify(input), error); return input.responseBuilder.speak('Something went wrong').getResponse(); }; } toJSON() { return this.model(); } addCustomIntent(intent) { if (this.intents[intent.props.intentName]) { throw new Error(`${intent.props.intentName} already defined`); } this.intents[intent.props.intentName] = intent; return this; } getString() { return this.localeValue; } model() { const intents = Object.keys(this.intents).map((intentName) => this.intents[intentName]); const dialogIntents = intents.filter((intent) => intent.usesDialog()); const dialog = dialogIntents.length > 0 ? { intents: intents .filter((intent) => intent.usesDialog()) .map((intent) => { const dialogIntents = { name: intent.props.intentName, confirmationRequired: intent.props.confirmation !== undefined, prompts: intent.props.confirmation ? { confirmation: (0, helpers_1.hashValue)(intent.props.confirmation.prompts) } : undefined, delegationStrategy: intent.props.delegationStrategy, slots: intent.slotsUsingDialog().map((slot) => { const dialogSlot = { name: slot.name, type: slot.type, elicitationRequired: slot.required !== undefined, confirmationRequired: slot.confirmation !== undefined, prompts: { elicitation: slot.required ? (0, helpers_1.hashValue)(slot.required.prompts) : undefined, confirmation: slot.confirmation ? (0, helpers_1.hashValue)(slot.confirmation.prompts) : undefined } }; return dialogSlot; }) }; return dialogIntents; }) } : undefined; const prompts = dialogIntents.length > 0 ? dialogIntents .flatMap((intent) => { const slotPrompts = intent.slotsUsingDialog().flatMap((slot) => { return [...(slot.confirmation ? [slot.confirmation.prompts] : []), ...(slot.required ? [slot.required.prompts] : [])]; }); const intentPrompts = intent.props.confirmation ? [intent.props.confirmation.prompts] : []; return [...slotPrompts, ...intentPrompts]; }) .map((prompt) => { return { id: (0, helpers_1.hashValue)(prompt), variations: prompt }; }) : undefined; return { interactionModel: { languageModel: { intents: intents .map((intent) => intent.model()) .concat([ { name: 'AMAZON.CancelIntent', samples: [] }, { name: 'AMAZON.StopIntent', samples: [] }, { name: 'AMAZON.HelpIntent', samples: [] }, { name: 'AMAZON.FallbackIntent', samples: [] } ]), invocationName: this.props.invocationName }, dialog, prompts } }; } writeToPackageFile(baseSkillPackagePath) { const interactionFolder = (baseSkillPackagePath ? baseSkillPackagePath : './') + '/interactionModels/custom'; if (!fs.existsSync(interactionFolder)) { fs.mkdirSync(interactionFolder, { recursive: true }); } const model = this.model(); // write interaction models fs.writeFileSync(`${interactionFolder}/${this.getString()}.json`, JSON.stringify(model, null, 2)); } getErrorHandler() { return { uniqueId: `ErrorHandler-${this.getString()}`, canHandle: (input) => { const locale = (0, ask_sdk_core_1.getLocale)(input.requestEnvelope); const isLocale = locale === this.getString(); return isLocale; }, handle: this.props.errorHandler }; } getRequestHandlers() { const intentList = Object.keys(this.intents).map((intentName) => this.intents[intentName]); return intentList .flatMap((intent) => intent.getRequestHandlers()) .concat({ uniqueId: `SessionEnded-${this.getString()}`, canHandle: (input) => { const locale = (0, ask_sdk_core_1.getLocale)(input.requestEnvelope); const isLocale = locale === this.getString(); if (!isLocale) { return false; } const requestType = (0, ask_sdk_core_1.getRequestType)(input.requestEnvelope); const canHandle = requestType === 'SessionEndedRequest'; return canHandle; }, handle: (0, helpers_1.wrapHandler)(this.props.sessionEndedHandler) }, { uniqueId: `CancelHandler-${this.getString()}`, canHandle: (input) => { const locale = (0, ask_sdk_core_1.getLocale)(input.requestEnvelope); const isLocale = locale === this.getString(); if (!isLocale) { return false; } const requestType = (0, ask_sdk_core_1.getRequestType)(input.requestEnvelope); const canHandle = requestType === 'IntentRequest' && ((0, ask_sdk_core_1.getIntentName)(input.requestEnvelope) === 'AMAZON.StopIntent' || (0, ask_sdk_core_1.getIntentName)(input.requestEnvelope) === 'AMAZON.CancelIntent'); return canHandle; }, handle: (0, helpers_1.wrapHandler)(this.props.cancelHandler) }, { uniqueId: `FallbackHandler-${this.getString()}`, canHandle: (input) => { const locale = (0, ask_sdk_core_1.getLocale)(input.requestEnvelope); const isLocale = locale === this.getString(); if (!isLocale) { return false; } const requestType = (0, ask_sdk_core_1.getRequestType)(input.requestEnvelope); const canHandle = requestType === 'IntentRequest' && (0, ask_sdk_core_1.getIntentName)(input.requestEnvelope) === 'AMAZON.FallbackIntent'; return canHandle; }, handle: (0, helpers_1.wrapHandler)(this.props.fallbackHandler) }, { uniqueId: `HelpHandler-${this.getString()}`, canHandle: (input) => { const locale = (0, ask_sdk_core_1.getLocale)(input.requestEnvelope); const isLocale = locale === this.getString(); if (!isLocale) { return false; } const requestType = (0, ask_sdk_core_1.getRequestType)(input.requestEnvelope); const canHandle = requestType === 'IntentRequest' && (0, ask_sdk_core_1.getIntentName)(input.requestEnvelope) === 'AMAZON.HelpIntent'; return canHandle; }, handle: (0, helpers_1.wrapHandler)(this.props.helpHandler) }, { uniqueId: `LaunchHandler-${this.getString()}`, canHandle: (input) => { const locale = (0, ask_sdk_core_1.getLocale)(input.requestEnvelope); const isLocale = locale === this.getString(); if (!isLocale) { return false; } const requestType = (0, ask_sdk_core_1.getRequestType)(input.requestEnvelope); return requestType === 'LaunchRequest'; }, handle: (0, helpers_1.wrapHandler)(this.props.launchHandler) }); } } exports.Locale = Locale;