-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port: add speach middleware to runtime (#3435)
* port: add speak middleware to runtime Fixes #3432 * fix: add telephony to channels, remove console * fix: coerce to strings
- Loading branch information
1 parent
c18d387
commit a9003e5
Showing
7 changed files
with
240 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import { ActivityTypes, Channels, Middleware, TurnContext } from 'botbuilder-core'; | ||
import { parseDocument } from 'htmlparser2'; | ||
import { tests } from 'botbuilder-stdlib'; | ||
|
||
const supportedChannels = new Set<string>([Channels.DirectlineSpeech, Channels.Emulator, Channels.Telephony]); | ||
|
||
// Iterate through `obj` and all children in an attempt to locale a key `tag` | ||
function hasTag(tag: string, nodes: unknown[]): boolean { | ||
while (nodes.length) { | ||
const item = nodes.shift(); | ||
|
||
if (tests.isDictionary(item)) { | ||
if (item.tagName === tag) { | ||
return true; | ||
} | ||
|
||
if (tests.isArray(item.children)) { | ||
nodes.push(...item.children); | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Support the DirectLine speech and telephony channels to ensure the appropriate SSML tags are set on the | ||
* Activity Speak property. | ||
*/ | ||
export class SetSpeakMiddleware implements Middleware { | ||
/** | ||
* Initializes a new instance of the SetSpeakMiddleware class. | ||
* | ||
* @param voiceName The SSML voice name attribute value. | ||
* @param lang The xml:lang value. | ||
* @param fallbackToTextForSpeak true if an empty Activity.Speak is populated with Activity.Text. | ||
*/ | ||
constructor( | ||
private readonly voiceName: string | null, | ||
private readonly lang: string, | ||
private readonly fallbackToTextForSpeak: boolean | ||
) { | ||
if (!lang) throw new TypeError('`lang` must be a non-empty string'); | ||
} | ||
|
||
/** | ||
* 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: TurnContext, next: () => Promise<void>): Promise<void> { | ||
turnContext.onSendActivities(async (_ctx, activities, next) => { | ||
await Promise.all( | ||
activities.map(async (activity) => { | ||
if (activity.type !== ActivityTypes.Message) { | ||
return; | ||
} | ||
|
||
if (this.fallbackToTextForSpeak && !activity.speak) { | ||
activity.speak = activity.text; | ||
} | ||
|
||
const channelId = turnContext.activity.channelId?.trim().toLowerCase(); | ||
|
||
if (activity.speak && this.voiceName !== null && supportedChannels.has(channelId)) { | ||
const nodes = 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='${this.lang}'>${activity.speak}</speak>`; | ||
} | ||
} | ||
}) | ||
); | ||
|
||
return next(); | ||
}); | ||
|
||
return next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
const assert = require('assert'); | ||
const { SetSpeakMiddleware } = require('..'); | ||
const { MessageFactory, TestAdapter } = require('botbuilder-core'); | ||
|
||
describe('SetSpeakMiddleware', function () { | ||
describe('constructor', function () { | ||
it('works', function () { | ||
new SetSpeakMiddleware('voiceName', 'lang', false); | ||
}); | ||
|
||
it('throws for falsy lang param', function () { | ||
assert.throws(() => new SetSpeakMiddleware('voiceName', '', false)); | ||
}); | ||
}); | ||
|
||
describe('onTurn', function () { | ||
function makeAdapter({ | ||
channelId = 'emulator', | ||
fallback = true, | ||
logic = async (context) => { | ||
await context.sendActivity(MessageFactory.text('OK')); | ||
}, | ||
voice = 'male', | ||
} = {}) { | ||
return new TestAdapter(logic, { channelId }).use(new SetSpeakMiddleware(voice, 'en-us', fallback)); | ||
} | ||
|
||
it('no fallback does nothing to speak', async function () { | ||
const adapter = makeAdapter({ fallback: false }); | ||
|
||
await adapter | ||
.send('foo') | ||
.assertReply((activity) => assert(activity.speak == null)) | ||
.startTest(); | ||
}); | ||
|
||
it('unsupported channel and empty speak yields speak === text', async function () { | ||
const adapter = makeAdapter({ channelId: 'doesnotsupportspeach' }); | ||
|
||
await adapter | ||
.send('foo') | ||
.assertReply((activity) => assert.strictEqual(activity.speak, activity.text)) | ||
.startTest(); | ||
}); | ||
|
||
it('unsupported channel and non-empty value yields untouched speak', async function () { | ||
const adapter = makeAdapter({ | ||
channelId: 'doesnotsupportspeach', | ||
logic: async (context) => { | ||
const activity = MessageFactory.text('OK'); | ||
activity.speak = 'custom speak'; | ||
|
||
await context.sendActivity(activity); | ||
}, | ||
}); | ||
|
||
await adapter | ||
.send('foo') | ||
.assertReply((activity) => assert.strictEqual(activity.speak, 'custom speak')) | ||
.startTest(); | ||
}); | ||
|
||
it('supported channel yields speak with voice added', async function () { | ||
const adapter = makeAdapter(); | ||
|
||
await adapter | ||
.send('foo') | ||
.assertReply((activity) => assert(activity.speak.includes('<voice '))) | ||
.startTest(); | ||
}); | ||
|
||
it('null voice param yields untouched speak', async function () { | ||
const adapter = makeAdapter({ voice: null }); | ||
|
||
await adapter | ||
.send('foo') | ||
.assertReply((activity) => assert.strictEqual(activity.speak, 'OK')) | ||
.startTest(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters