UNPKG

@jargon/alexa-skill-sdk

Version:

The Jargon Alexa Skill SDK makes it easy to manage the content of your custom Alexa skill

360 lines 15.1 kB
"use strict"; /* * 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. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); var escape_1 = require("./escape"); var lodash_1 = require("lodash"); exports.DefaultJargonResponseBuilderOptions = { mergeSpeakAndReprompt: false }; var JRB = /** @class */ (function () { function JRB(rb, rm, opts) { if (opts === void 0) { opts = {}; } this._q = []; this._speak = []; this._reprompt = []; this._rb = rb; this._rm = rm; this._opts = Object.assign({}, exports.DefaultJargonResponseBuilderOptions, opts); } JRB.prototype.speak = function (speechOutput, optionsOrMerge) { var _this = this; var p = this._rm.render(speechOutput); var op = function (rb) { return __awaiter(_this, void 0, void 0, function () { var val, _a, op; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = escape_1.escapeSSML; return [4 /*yield*/, p]; case 1: val = _a.apply(void 0, [_b.sent()]); op = { val: val, playBehavior: playBehavior(optionsOrMerge) }; if (this._shouldMerge(optionsOrMerge)) { this._speak.push(op); } else { this._speak = [op]; } return [2 /*return*/, rb]; } }); }); }; this._q.push(op); return this; }; JRB.prototype.reprompt = function (repromptSpeechOutput, optionsOrMerge) { var _this = this; var p = this._rm.render(repromptSpeechOutput); var op = function (rb) { return __awaiter(_this, void 0, void 0, function () { var val, _a, op; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = escape_1.escapeSSML; return [4 /*yield*/, p]; case 1: val = _a.apply(void 0, [_b.sent()]); op = { val: val, playBehavior: playBehavior(optionsOrMerge) }; if (this._shouldMerge(optionsOrMerge)) { this._reprompt.push(op); } else { this._reprompt = [op]; } return [2 /*return*/, rb]; } }); }); }; this._q.push(op); return this; }; JRB.prototype.withSimpleCard = function (cardTitle, cardContent) { var _this = this; var title = this._rm.render(cardTitle); var content = this._rm.render(cardContent); var op = function (rb) { return __awaiter(_this, void 0, void 0, function () { var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _b = (_a = rb).withSimpleCard; return [4 /*yield*/, title]; case 1: _c = [_d.sent()]; return [4 /*yield*/, content]; case 2: return [2 /*return*/, _b.apply(_a, _c.concat([_d.sent()]))]; } }); }); }; this._q.push(op); return this; }; JRB.prototype.withStandardCard = function (cardTitle, cardContent, smallImageUrl, largeImageUrl) { var _this = this; var title = this._rm.render(cardTitle); var content = this._rm.render(cardContent); var op = function (rb) { return __awaiter(_this, void 0, void 0, function () { var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _b = (_a = rb).withStandardCard; return [4 /*yield*/, title]; case 1: _c = [_d.sent()]; return [4 /*yield*/, content]; case 2: return [2 /*return*/, _b.apply(_a, _c.concat([_d.sent(), smallImageUrl, largeImageUrl]))]; } }); }); }; this._q.push(op); return this; }; JRB.prototype.withLinkAccountCard = function () { this._rb.withLinkAccountCard(); return this; }; JRB.prototype.withAskForPermissionsConsentCard = function (permissionArray) { this._rb.withAskForPermissionsConsentCard(permissionArray); return this; }; JRB.prototype.addDelegateDirective = function (updatedIntent) { this._rb.addDelegateDirective(updatedIntent); return this; }; JRB.prototype.addElicitSlotDirective = function (slotToElicit, updatedIntent) { this._rb.addElicitSlotDirective(slotToElicit, updatedIntent); return this; }; JRB.prototype.addConfirmSlotDirective = function (slotToConfirm, updatedIntent) { this._rb.addConfirmSlotDirective(slotToConfirm, updatedIntent); return this; }; JRB.prototype.addConfirmIntentDirective = function (updatedIntent) { this._rb.addConfirmIntentDirective(updatedIntent); return this; }; JRB.prototype.addAudioPlayerPlayDirective = function (playBehavior, url, token, offsetInMilliseconds, expectedPreviousToken, audioItemMetadata) { this._rb.addAudioPlayerPlayDirective(playBehavior, url, token, offsetInMilliseconds, expectedPreviousToken, audioItemMetadata); return this; }; JRB.prototype.addAudioPlayerStopDirective = function () { this._rb.addAudioPlayerStopDirective(); return this; }; JRB.prototype.addAudioPlayerClearQueueDirective = function (clearBehavior) { this._rb.addAudioPlayerClearQueueDirective(clearBehavior); return this; }; JRB.prototype.addRenderTemplateDirective = function (template) { this._rb.addRenderTemplateDirective(template); return this; }; JRB.prototype.addHintDirective = function (text) { var _this = this; var hint = this._rm.render(text); var op = function (rb) { return __awaiter(_this, void 0, void 0, function () { var _a, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: _b = (_a = rb).addHintDirective; return [4 /*yield*/, hint]; case 1: return [2 /*return*/, _b.apply(_a, [_c.sent()])]; } }); }); }; this._q.push(op); return this; }; JRB.prototype.addVideoAppLaunchDirective = function (source, title, subtitle) { var _this = this; if (title || subtitle) { var tp_1; var sp_1; if (title) { tp_1 = this._rm.render(title); } else { tp_1 = Promise.resolve(undefined); } if (subtitle) { sp_1 = this._rm.render(subtitle); } else { sp_1 = Promise.resolve(undefined); } var op = function (rb) { return __awaiter(_this, void 0, void 0, function () { var _a, _b, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _b = (_a = rb).addVideoAppLaunchDirective; _c = [source]; return [4 /*yield*/, tp_1]; case 1: _c = _c.concat([_d.sent()]); return [4 /*yield*/, sp_1]; case 2: return [2 /*return*/, _b.apply(_a, _c.concat([_d.sent()]))]; } }); }); }; this._q.push(op); } else { this._rb.addVideoAppLaunchDirective(source); } return this; }; JRB.prototype.withCanFulfillIntent = function (canFulfillIntent) { this._rb.withCanFulfillIntent(canFulfillIntent); return this; }; JRB.prototype.withShouldEndSession = function (val) { this._rb.withShouldEndSession(val); return this; }; JRB.prototype.addDirective = function (directive) { this._rb.addDirective(directive); return this; }; JRB.prototype.getResponse = function () { return __awaiter(this, void 0, void 0, function () { var _a, _b, op, e_1_1; var e_1, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: _d.trys.push([0, 5, 6, 7]); _a = __values(this._q), _b = _a.next(); _d.label = 1; case 1: if (!!_b.done) return [3 /*break*/, 4]; op = _b.value; return [4 /*yield*/, op(this._rb)]; case 2: _d.sent(); _d.label = 3; case 3: _b = _a.next(); return [3 /*break*/, 1]; case 4: return [3 /*break*/, 7]; case 5: e_1_1 = _d.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 7]; case 6: try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 7: 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 [2 /*return*/, this._rb.getResponse()]; } }); }); }; JRB.prototype._shouldMerge = function (optionsOrMerge) { if (lodash_1.isBoolean(optionsOrMerge)) { return optionsOrMerge; } if (isResponseGenerationOptions(optionsOrMerge) && lodash_1.isBoolean(optionsOrMerge.merge)) { return optionsOrMerge.merge; } return this._opts.mergeSpeakAndReprompt; }; return JRB; }()); exports.JRB = JRB; function isResponseGenerationOptions(opt) { return (opt && lodash_1.isObject(opt)) || false; } function playBehavior(optionsOrMerge) { if (isResponseGenerationOptions(optionsOrMerge)) { return optionsOrMerge.playBehavior; } return undefined; } function mergedString(ops) { return ops.map(function (v) { return v.val; }).join(' '); } function mergedPlayBehavior(ops) { var val = lodash_1.chain(ops) .map(function (op) { return 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