UNPKG

botbuilder

Version:

Bot Builder is a framework for building rich bots on virtually any platform.

87 lines 4.11 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SetSpeakMiddleware = void 0; const z = require("zod"); const botbuilder_core_1 = require("botbuilder-core"); const htmlparser2_1 = require("htmlparser2"); const supportedChannels = new Set([botbuilder_core_1.Channels.DirectlineSpeech, botbuilder_core_1.Channels.Emulator, botbuilder_core_1.Channels.Telephony]); // Iterate through `obj` and all children in an attempt to locale a key `tag` function hasTag(tag, nodes) { while (nodes.length) { const item = nodes.shift(); if (z .object({ tagName: z.string(), children: z.array(z.unknown()) }) .partial() .nonstrict() .check(item)) { if (item.tagName === tag) { return true; } if (item.children) { nodes.push(...item.children); } } } return false; } /** * Support the DirectLine speech and telephony channels to ensure the appropriate SSML tags are set on the * Activity Speak property. */ class SetSpeakMiddleware { /** * Initializes a new instance of the SetSpeakMiddleware class. * * @param voiceName The SSML voice name attribute value. * @param fallbackToTextForSpeak true if an empty Activity.Speak is populated with Activity.Text. */ constructor(voiceName, fallbackToTextForSpeak) { this.voiceName = voiceName; this.fallbackToTextForSpeak = fallbackToTextForSpeak; } /** * Processes an incoming activity. * * @param turnContext The context object for this turn. * @param next The delegate to call to continue the bot middleware pipeline. * @returns A promise representing the async operation. */ onTurn(turnContext, next) { turnContext.onSendActivities((_ctx, activities, next) => __awaiter(this, void 0, void 0, function* () { yield Promise.all(activities.map((activity) => __awaiter(this, void 0, void 0, function* () { var _a, _b; if (activity.type !== botbuilder_core_1.ActivityTypes.Message) { return; } if (this.fallbackToTextForSpeak && !activity.speak) { activity.speak = activity.text; } const channelId = (_a = turnContext.activity.channelId) === null || _a === void 0 ? void 0 : _a.trim().toLowerCase(); if (activity.speak && this.voiceName !== null && supportedChannels.has(channelId)) { const nodes = htmlparser2_1.parseDocument(activity.speak).childNodes; if (!hasTag('speak', nodes.slice())) { if (!hasTag('voice', nodes.slice())) { activity.speak = `<voice name='${this.voiceName}'>${activity.speak}</voice>`; } activity.speak = `<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='${(_b = activity.locale) !== null && _b !== void 0 ? _b : 'en-US'}'>${activity.speak}</speak>`; } } }))); return next(); })); return next(); } } exports.SetSpeakMiddleware = SetSpeakMiddleware; //# sourceMappingURL=setSpeakMiddleware.js.map