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 botbuilder_core_1 = require("botbuilder-core");
|
16 | const htmlparser2_1 = require("htmlparser2");
|
17 | const botbuilder_stdlib_1 = require("botbuilder-stdlib");
|
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 (botbuilder_stdlib_1.tests.isDictionary(item)) {
|
24 | if (item.tagName === tag) {
|
25 | return true;
|
26 | }
|
27 | if (botbuilder_stdlib_1.tests.isArray(item.children)) {
|
28 | nodes.push(...item.children);
|
29 | }
|
30 | }
|
31 | }
|
32 | return false;
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | class SetSpeakMiddleware {
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | constructor(voiceName, fallbackToTextForSpeak) {
|
46 | this.voiceName = voiceName;
|
47 | this.fallbackToTextForSpeak = fallbackToTextForSpeak;
|
48 | }
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | onTurn(turnContext, next) {
|
57 | turnContext.onSendActivities((_ctx, activities, next) => __awaiter(this, void 0, void 0, function* () {
|
58 | yield Promise.all(activities.map((activity) => __awaiter(this, void 0, void 0, function* () {
|
59 | var _a, _b;
|
60 | if (activity.type !== botbuilder_core_1.ActivityTypes.Message) {
|
61 | return;
|
62 | }
|
63 | if (this.fallbackToTextForSpeak && !activity.speak) {
|
64 | activity.speak = activity.text;
|
65 | }
|
66 | const channelId = (_a = turnContext.activity.channelId) === null || _a === void 0 ? void 0 : _a.trim().toLowerCase();
|
67 | if (activity.speak && this.voiceName !== null && supportedChannels.has(channelId)) {
|
68 | const nodes = htmlparser2_1.parseDocument(activity.speak).childNodes;
|
69 | if (!hasTag('speak', nodes.slice())) {
|
70 | if (!hasTag('voice', nodes.slice())) {
|
71 | activity.speak = `<voice name='${this.voiceName}'>${activity.speak}</voice>`;
|
72 | }
|
73 | 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>`;
|
74 | }
|
75 | }
|
76 | })));
|
77 | return next();
|
78 | }));
|
79 | return next();
|
80 | }
|
81 | }
|
82 | exports.SetSpeakMiddleware = SetSpeakMiddleware;
|
83 |
|
\ | No newline at end of file |