UNPKG

4.07 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 botbuilder_core_1 = require("botbuilder-core");
16const htmlparser2_1 = require("htmlparser2");
17const botbuilder_stdlib_1 = require("botbuilder-stdlib");
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 (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 * Support the DirectLine speech and telephony channels to ensure the appropriate SSML tags are set on the
36 * Activity Speak property.
37 */
38class SetSpeakMiddleware {
39 /**
40 * Initializes a new instance of the SetSpeakMiddleware class.
41 *
42 * @param voiceName The SSML voice name attribute value.
43 * @param fallbackToTextForSpeak true if an empty Activity.Speak is populated with Activity.Text.
44 */
45 constructor(voiceName, fallbackToTextForSpeak) {
46 this.voiceName = voiceName;
47 this.fallbackToTextForSpeak = fallbackToTextForSpeak;
48 }
49 /**
50 * Processes an incoming activity.
51 *
52 * @param turnContext The context object for this turn.
53 * @param next The delegate to call to continue the bot middleware pipeline.
54 * @returns A promise representing the async operation.
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}
82exports.SetSpeakMiddleware = SetSpeakMiddleware;
83//# sourceMappingURL=setSpeakMiddleware.js.map
\No newline at end of file