1 | "use strict";
|
2 |
|
3 |
|
4 | var __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 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.SetSpeakMiddleware = void 0;
|
15 | const z = require("zod");
|
16 | const botbuilder_core_1 = require("botbuilder-core");
|
17 | const htmlparser2_1 = require("htmlparser2");
|
18 | const supportedChannels = new Set([botbuilder_core_1.Channels.DirectlineSpeech, botbuilder_core_1.Channels.Emulator, botbuilder_core_1.Channels.Telephony]);
|
19 |
|
20 | function 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 |
|
40 |
|
41 |
|
42 | class SetSpeakMiddleware {
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | constructor(voiceName, fallbackToTextForSpeak) {
|
50 | this.voiceName = voiceName;
|
51 | this.fallbackToTextForSpeak = fallbackToTextForSpeak;
|
52 | }
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
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 | }
|
86 | exports.SetSpeakMiddleware = SetSpeakMiddleware;
|
87 |
|
\ | No newline at end of file |