UNPKG

botbuilder

Version:

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

111 lines 5.24 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. 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; }; 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 = __importStar(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(); const itemParsed = z .object({ tagName: z.string(), children: z.array(z.unknown()) }) .partial() .nonstrict() .safeParse(item); if (itemParsed.success) { if (itemParsed.data.tagName === tag) { return true; } if (itemParsed.data.children) { nodes.push(...itemParsed.data.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 = (0, 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