UNPKG

4.11 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft Corporation.
3// Licensed under the MIT License.
4var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6 return new (P || (P = Promise))(function (resolve, reject) {
7 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10 step((generator = generator.apply(thisArg, _arguments || [])).next());
11 });
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14exports.SetSpeakMiddleware = void 0;
15const z = require("zod");
16const botbuilder_core_1 = require("botbuilder-core");
17const htmlparser2_1 = require("htmlparser2");
18const supportedChannels = new Set([botbuilder_core_1.Channels.DirectlineSpeech, botbuilder_core_1.Channels.Emulator, botbuilder_core_1.Channels.Telephony]);
19// Iterate through `obj` and all children in an attempt to locale a key `tag`
20function hasTag(tag, nodes) {
21 while (nodes.length) {
22 const item = nodes.shift();
23 if (z
24 .object({ tagName: z.string(), children: z.array(z.unknown()) })
25 .partial()
26 .nonstrict()
27 .check(item)) {
28 if (item.tagName === tag) {
29 return true;
30 }
31 if (item.children) {
32 nodes.push(...item.children);
33 }
34 }
35 }
36 return false;
37}
38/**
39 * Support the DirectLine speech and telephony channels to ensure the appropriate SSML tags are set on the
40 * Activity Speak property.
41 */
42class SetSpeakMiddleware {
43 /**
44 * Initializes a new instance of the SetSpeakMiddleware class.
45 *
46 * @param voiceName The SSML voice name attribute value.
47 * @param fallbackToTextForSpeak true if an empty Activity.Speak is populated with Activity.Text.
48 */
49 constructor(voiceName, fallbackToTextForSpeak) {
50 this.voiceName = voiceName;
51 this.fallbackToTextForSpeak = fallbackToTextForSpeak;
52 }
53 /**
54 * Processes an incoming activity.
55 *
56 * @param turnContext The context object for this turn.
57 * @param next The delegate to call to continue the bot middleware pipeline.
58 * @returns A promise representing the async operation.
59 */
60 onTurn(turnContext, next) {
61 turnContext.onSendActivities((_ctx, activities, next) => __awaiter(this, void 0, void 0, function* () {
62 yield Promise.all(activities.map((activity) => __awaiter(this, void 0, void 0, function* () {
63 var _a, _b;
64 if (activity.type !== botbuilder_core_1.ActivityTypes.Message) {
65 return;
66 }
67 if (this.fallbackToTextForSpeak && !activity.speak) {
68 activity.speak = activity.text;
69 }
70 const channelId = (_a = turnContext.activity.channelId) === null || _a === void 0 ? void 0 : _a.trim().toLowerCase();
71 if (activity.speak && this.voiceName !== null && supportedChannels.has(channelId)) {
72 const nodes = htmlparser2_1.parseDocument(activity.speak).childNodes;
73 if (!hasTag('speak', nodes.slice())) {
74 if (!hasTag('voice', nodes.slice())) {
75 activity.speak = `<voice name='${this.voiceName}'>${activity.speak}</voice>`;
76 }
77 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>`;
78 }
79 }
80 })));
81 return next();
82 }));
83 return next();
84 }
85}
86exports.SetSpeakMiddleware = SetSpeakMiddleware;
87//# sourceMappingURL=setSpeakMiddleware.js.map
\No newline at end of file