UNPKG

@jargon/platform-alexa-skill-kit

Version:

Jargon Platform SDK adapter for the Alexa Skills Kit

264 lines 8.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JRB = exports.DefaultJargonResponseBuilderOptions = void 0; /* * Copyright 2018 Jargon, Inc. or its affiliates. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ const platform_sdk_core_1 = require("@jargon/platform-sdk-core"); const lodash_1 = require("lodash"); exports.DefaultJargonResponseBuilderOptions = { mergeSpeakAndReprompt: false }; class JRB { constructor(rb, rm, opts = {}) { this._q = []; this._speak = []; this._reprompt = []; this._rb = rb; this._rm = rm; this._opts = Object.assign({}, exports.DefaultJargonResponseBuilderOptions, opts); } withJargonResponse(response, options) { let p = this._rm.renderResponse(response); let op = async (rb) => { const obj = await p; if (obj.speech) { const val = platform_sdk_core_1.escapeSSML(obj.speech.content); const op = { val, playBehavior: playBehavior(options) }; if (this._shouldMerge(options)) { this._speak.push(op); } else { this._speak = [op]; } } if (obj.reprompt) { const val = platform_sdk_core_1.escapeSSML(obj.reprompt.content); const op = { val, playBehavior: playBehavior(options) }; if (this._shouldMerge(options)) { this._reprompt.push(op); } else { this._reprompt = [op]; } } if (obj.alexaCard) { const card = obj.alexaCard.content; if (card.type === 'Simple') { rb.withSimpleCard(card.title, card.content); } else if (card.type === 'Standard') { const image = card.image || {}; rb.withStandardCard(card.title, card.text, image.smallImageUrl, image.largeImageUrl); } } if (obj.alexaDirectives) { const alexaDirectives = obj.alexaDirectives; for (const alexaDirective in alexaDirectives) { const directive = alexaDirectives[alexaDirective]; const content = directive.content; rb.addDirective(content); } } return rb; }; this._q.push(op); return this; } speak(speechOutput, options) { let p = this._rm.render(speechOutput); let op = async (rb) => { const val = platform_sdk_core_1.escapeSSML(await p); const op = { val, playBehavior: playBehavior(options) }; if (this._shouldMerge(options)) { this._speak.push(op); } else { this._speak = [op]; } return rb; }; this._q.push(op); return this; } reprompt(repromptSpeechOutput, options) { let p = this._rm.render(repromptSpeechOutput); let op = async (rb) => { const val = platform_sdk_core_1.escapeSSML(await p); const op = { val, playBehavior: playBehavior(options) }; if (this._shouldMerge(options)) { this._reprompt.push(op); } else { this._reprompt = [op]; } return rb; }; this._q.push(op); return this; } withSimpleCard(cardTitle, cardContent) { let title = this._rm.render(cardTitle); let content = this._rm.render(cardContent); let op = async (rb) => { return rb.withSimpleCard(await title, await content); }; this._q.push(op); return this; } withStandardCard(cardTitle, cardContent, smallImageUrl, largeImageUrl) { let title = this._rm.render(cardTitle); let content = this._rm.render(cardContent); let op = async (rb) => { return rb.withStandardCard(await title, await content, smallImageUrl, largeImageUrl); }; this._q.push(op); return this; } withLinkAccountCard() { this._rb.withLinkAccountCard(); return this; } withAskForPermissionsConsentCard(permissionArray) { this._rb.withAskForPermissionsConsentCard(permissionArray); return this; } addDelegateDirective(updatedIntent) { this._rb.addDelegateDirective(updatedIntent); return this; } addElicitSlotDirective(slotToElicit, updatedIntent) { this._rb.addElicitSlotDirective(slotToElicit, updatedIntent); return this; } addConfirmSlotDirective(slotToConfirm, updatedIntent) { this._rb.addConfirmSlotDirective(slotToConfirm, updatedIntent); return this; } addConfirmIntentDirective(updatedIntent) { this._rb.addConfirmIntentDirective(updatedIntent); return this; } addAudioPlayerPlayDirective(playBehavior, url, token, offsetInMilliseconds, expectedPreviousToken, audioItemMetadata) { this._rb.addAudioPlayerPlayDirective(playBehavior, url, token, offsetInMilliseconds, expectedPreviousToken, audioItemMetadata); return this; } addAudioPlayerStopDirective() { this._rb.addAudioPlayerStopDirective(); return this; } addAudioPlayerClearQueueDirective(clearBehavior) { this._rb.addAudioPlayerClearQueueDirective(clearBehavior); return this; } addRenderTemplateDirective(template) { this._rb.addRenderTemplateDirective(template); return this; } addHintDirective(text) { let hint = this._rm.render(text); let op = async (rb) => { return rb.addHintDirective(await hint); }; this._q.push(op); return this; } addVideoAppLaunchDirective(source, title, subtitle) { if (title || subtitle) { let tp; let sp; if (title) { tp = this._rm.render(title); } else { tp = Promise.resolve(undefined); } if (subtitle) { sp = this._rm.render(subtitle); } else { sp = Promise.resolve(undefined); } let op = async (rb) => { return rb.addVideoAppLaunchDirective(source, await tp, await sp); }; this._q.push(op); } else { this._rb.addVideoAppLaunchDirective(source); } return this; } withCanFulfillIntent(canFulfillIntent) { this._rb.withCanFulfillIntent(canFulfillIntent); return this; } withShouldEndSession(val) { this._rb.withShouldEndSession(val); return this; } addDirective(directive) { this._rb.addDirective(directive); return this; } async getResponse() { for (let op of this._q) { await op(this._rb); } if (this._speak.length > 0) { this._rb.speak(mergedString(this._speak), mergedPlayBehavior(this._speak)); } if (this._reprompt.length > 0) { this._rb.reprompt(mergedString(this._reprompt), mergedPlayBehavior(this._reprompt)); } return this._rb.getResponse(); } _shouldMerge(options) { if (options && lodash_1.isBoolean(options.merge)) { return options.merge; } return this._opts.mergeSpeakAndReprompt; } } exports.JRB = JRB; function playBehavior(options) { if (options) { return options.playBehavior; } return undefined; } function mergedString(ops) { return ops.map(v => v.val).join(' '); } function mergedPlayBehavior(ops) { const val = lodash_1.chain(ops) .map(op => op.playBehavior) .compact() .first() .value(); // _.first() gets typed as returning a StringChain instead of a CollectionChain, // so we need to assert the correct type here return val; } //# sourceMappingURL=jrb.js.map