Skip to content

Commit b9612d5

Browse files
compulimcorinagum
andcommitted
Update speech options to use credentials (#2759)
* Update to latest fetch credentials signature * Prettier * Refactor * Apply suggestions from code review Co-Authored-By: Corina <[email protected]> Co-authored-by: Corina <[email protected]>
1 parent 6addf73 commit b9612d5

File tree

10 files changed

+105
-176
lines changed

10 files changed

+105
-176
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
4646
- Fixes [#2510](https://github.com/microsoft/BotFramework-WebChat/issues/2510). Host hybrid-react and clear-after-idle samples, by [@corinagum](https://github.com/corinagum) in PR [#2798](https://github.com/microsoft/BotFramework-WebChat/pull/2798)
4747
- Fixes [#2772](https://github.com/microsoft/BotFramework-WebChat/issues/2772). Updated Adaptive Cards HostConfig to include container styles, by [@tdurnford](https://github.com/tdurnford) in PR [#28XX](https://github.com/microsoft/BotFramework-WebChat/pull/2810)
4848
- Fixes [#2145](https://github.com/microsoft/BotFramework-WebChat/issues/2145). Updated Adaptive Cards styles to include action styles, by [@tdurnford](https://github.com/tdurnford) in PR [#2810](https://github.com/microsoft/BotFramework-WebChat/pull/2810)
49+
- Fixes [#2459](https://github.com/microsoft/BotFramework-WebChat/issues/2459). Updated Cognitive Services Speech Services to use latest fetch credentials signature, by [@compulim](https://github.com/compulim) in PR [#2740](https://github.com/microsoft/BotFramework-WebChat/pull/2759)
4950

5051
### Changed
5152

packages/bundle/src/createCognitiveServicesSpeechServicesPonyfillFactory.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { AudioConfig } from 'microsoft-cognitiveservices-speech-sdk/distrib/lib/src/sdk/Audio/AudioConfig';
22
import createPonyfill from 'web-speech-cognitive-services/lib/SpeechServices';
33

4+
async function resolveFunction(fnOrValue) {
5+
return await (typeof fnOrValue === 'function' ? fnOrValue() : fnOrValue);
6+
}
7+
48
export default function createCognitiveServicesSpeechServicesPonyfillFactory({
59
audioConfig,
610
authorizationToken,
11+
credentials,
712
enableTelemetry,
813
region,
914
speechRecognitionEndpointId,
@@ -12,6 +17,26 @@ export default function createCognitiveServicesSpeechServicesPonyfillFactory({
1217
subscriptionKey,
1318
textNormalization
1419
}) {
20+
if (!credentials && (authorizationToken || region || subscriptionKey)) {
21+
console.warn(
22+
'botframework-webchat: "authorizationToken", "region", and "subscriptionKey" are being deprecated and will be removed on or after 2020-12-17. Please use "credentials" instead.'
23+
);
24+
25+
credentials = async () => {
26+
if (authorizationToken) {
27+
return {
28+
authorizationToken: resolveFunction(authorizationToken),
29+
region
30+
};
31+
} else {
32+
return {
33+
region,
34+
subscriptionKey: resolveFunction(subscriptionKey)
35+
};
36+
}
37+
};
38+
}
39+
1540
// HACK: We should prevent AudioContext object from being recreated because they may be blessed and UX-wise expensive to recreate.
1641
// In Cognitive Services SDK, if they detect the "end" function is falsy, they will not call "end" but "suspend" instead.
1742
// And on next recognition, they will re-use the AudioContext object.
@@ -35,14 +60,12 @@ export default function createCognitiveServicesSpeechServicesPonyfillFactory({
3560
return ({ referenceGrammarID }) => {
3661
const ponyfill = createPonyfill({
3762
audioConfig,
38-
authorizationToken,
63+
credentials,
3964
enableTelemetry,
4065
referenceGrammars: [`luis/${referenceGrammarID}-PRODUCTION`],
41-
region,
4266
speechRecognitionEndpointId,
4367
speechSynthesisDeploymentId,
4468
speechSynthesisOutputFormat,
45-
subscriptionKey,
4669
textNormalization
4770
});
4871

samples/03.speech/b.cognitive-speech-services-js/README.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ Cognitive Services Speech Services has published a new API to provide speech rec
6060
+ let lastPromise;
6161
+
6262
+ return () => {
63-
+ if (Date.now() > expireAfter) {
64-
+ const speechServicesTokenResPromise = fetch(
63+
+ const now = Date.now();
64+
+
65+
+ if (now > expireAfter) {
66+
+ expireAfter = now + 300000;
67+
+ lastPromise = fetch(
6568
+ 'https://webchat-mockbot.azurewebsites.net/speechservices/token',
6669
+ { method: 'POST' }
67-
+ );
68-
+
69-
+ expireAfter = Date.now() + 300000;
70-
+ lastPromise = speechServicesTokenResPromise.then(
70+
+ ).then(
7171
+ res => res.json(),
7272
+ err => {
73-
+ lastPromise = null;
73+
+ expireAfter = 0;
7474
+
7575
+ return Promise.reject(err);
7676
+ }
@@ -82,22 +82,13 @@ Cognitive Services Speech Services has published a new API to provide speech rec
8282
+ }
8383
+
8484
+ const fetchSpeechServicesCredentials = createFetchSpeechServicesCredentials();
85-
+
86-
+ async function fetchSpeechServicesRegion() {
87-
+ return (await fetchSpeechServicesCredentials()).region;
88-
+ }
89-
+
90-
+ async function fetchSpeechServicesToken() {
91-
+ return (await fetchSpeechServicesCredentials()).token;
92-
+ }
9385

9486
(async function () {
9587
const res = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', { method: 'POST' });
9688
const { token } = await res.json();
9789

9890
+ const webSpeechPonyfillFactory = await window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory({
99-
+ authorizationToken: fetchSpeechServicesToken,
100-
+ region: await fetchSpeechServicesRegion()
91+
+ credentials: fetchSpeechServicesCredentials
10192
+ });
10293

10394
window.WebChat.renderWebChat({

samples/03.speech/b.cognitive-speech-services-js/index.html

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@
3535
let lastPromise;
3636

3737
return () => {
38+
const now = Date.now();
39+
3840
// Fetch a new token if the existing one is expiring.
3941
// The following article mentioned the token is only valid for 10 minutes.
4042
// We will invalidate the token after 5 minutes.
4143
// https://docs.microsoft.com/en-us/azure/cognitive-services/authentication#authenticate-with-an-authentication-token
42-
if (Date.now() > expireAfter) {
43-
const speechServicesTokenResPromise = fetch(
44-
'https://webchat-mockbot.azurewebsites.net/speechservices/token',
45-
{ method: 'POST' }
46-
);
47-
48-
expireAfter = Date.now() + 300000;
49-
lastPromise = speechServicesTokenResPromise.then(
44+
if (now > expireAfter) {
45+
expireAfter = now + 300000;
46+
lastPromise = fetch('https://webchat-mockbot.azurewebsites.net/speechservices/token', {
47+
method: 'POST'
48+
}).then(
5049
res => res.json(),
5150
err => {
52-
lastPromise = null;
51+
expireAfter = 0;
5352

5453
return Promise.reject(err);
5554
}
@@ -62,14 +61,6 @@
6261

6362
const fetchSpeechServicesCredentials = createFetchSpeechServicesCredentials();
6463

65-
async function fetchSpeechServicesRegion() {
66-
return (await fetchSpeechServicesCredentials()).region;
67-
}
68-
69-
async function fetchSpeechServicesToken() {
70-
return (await fetchSpeechServicesCredentials()).token;
71-
}
72-
7364
(async function() {
7465
// In this demo, we are using Direct Line token from MockBot.
7566
// Your client code must provide either a secret or a token to talk to your bot.
@@ -81,12 +72,9 @@
8172

8273
// Create the ponyfill factory function, which can be called to create a concrete implementation of the ponyfill.
8374
const webSpeechPonyfillFactory = await window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory({
84-
// We are passing the Promise function to the authorizationToken field.
75+
// We are passing the Promise function to the "credentials" field.
8576
// This function will be called every time the token is being used.
86-
authorizationToken: fetchSpeechServicesToken,
87-
88-
// In contrast, we are only fetching the region once.
89-
region: await fetchSpeechServicesRegion()
77+
credentials: fetchSpeechServicesCredentials
9078
});
9179

9280
// Pass a Web Speech ponyfill factory to renderWebChat.

samples/03.speech/c.cognitive-speech-services-with-lexical-result/index.html

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@
3535
let lastPromise;
3636

3737
return () => {
38+
const now = Date.now();
39+
3840
// Fetch a new token if the existing one is expiring.
3941
// The following article mentioned the token is only valid for 10 minutes.
4042
// We will invalidate the token after 5 minutes.
4143
// https://docs.microsoft.com/en-us/azure/cognitive-services/authentication#authenticate-with-an-authentication-token
42-
if (Date.now() > expireAfter) {
43-
const speechServicesTokenResPromise = fetch(
44-
'https://webchat-mockbot.azurewebsites.net/speechservices/token',
45-
{ method: 'POST' }
46-
);
47-
48-
expireAfter = Date.now() + 300000;
49-
lastPromise = speechServicesTokenResPromise.then(
44+
if (now > expireAfter) {
45+
expireAfter = now + 300000;
46+
lastPromise = fetch('https://webchat-mockbot.azurewebsites.net/speechservices/token', {
47+
method: 'POST'
48+
}).then(
5049
res => res.json(),
5150
err => {
52-
lastPromise = null;
51+
expireAfter = 0;
5352

5453
return Promise.reject(err);
5554
}
@@ -62,14 +61,6 @@
6261

6362
const fetchSpeechServicesCredentials = createFetchSpeechServicesCredentials();
6463

65-
async function fetchSpeechServicesRegion() {
66-
return (await fetchSpeechServicesCredentials()).region;
67-
}
68-
69-
async function fetchSpeechServicesToken() {
70-
return (await fetchSpeechServicesCredentials()).token;
71-
}
72-
7364
(async function() {
7465
// In this demo, we are using Direct Line token from MockBot.
7566
// Your client code must provide either a secret or a token to talk to your bot.
@@ -81,12 +72,9 @@
8172

8273
// Create the ponyfill factory function, which can be called to create a concrete implementation of the ponyfill.
8374
const webSpeechPonyfillFactory = await window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory({
84-
// We are passing the Promise function to the authorizationToken field.
75+
// We are passing the Promise function to the "credentials" field.
8576
// This function will be called every time the token is being used.
86-
authorizationToken: fetchSpeechServicesToken,
87-
88-
// In contrast, we are only fetching the region once.
89-
region: await fetchSpeechServicesRegion(),
77+
credentials: fetchSpeechServicesCredentials,
9078
textNormalization: 'lexical'
9179
});
9280

samples/03.speech/d.cognitive-speech-services-speech-recognition-only/README.md

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,17 @@ Here is the finished `index.html`:
112112
let lastPromise;
113113

114114
return () => {
115-
if (Date.now() > expireAfter) {
116-
const speechServicesTokenResPromise = fetch(
115+
const now = Date.now();
116+
117+
if (now > expireAfter) {
118+
expireAfter = now + 300000;
119+
lastPromise = fetch(
117120
'https://webchat-mockbot.azurewebsites.net/speechservices/token',
118121
{ method: 'POST' }
119-
);
120-
121-
expireAfter = Date.now() + 300000;
122-
lastPromise = speechServicesTokenResPromise.then(
122+
).then(
123123
res => res.json(),
124124
err => {
125-
lastPromise = null;
125+
expireAfter = 0;
126126

127127
return Promise.reject(err);
128128
}
@@ -135,14 +135,6 @@ Here is the finished `index.html`:
135135

136136
const fetchSpeechServicesCredentials = createFetchSpeechServicesCredentials();
137137

138-
async function fetchSpeechServicesRegion() {
139-
return (await fetchSpeechServicesCredentials()).region;
140-
}
141-
142-
async function fetchSpeechServicesToken() {
143-
return (await fetchSpeechServicesCredentials()).token;
144-
}
145-
146138
(async function() {
147139
const directLineTokenRes = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', {
148140
method: 'POST'
@@ -151,15 +143,13 @@ Here is the finished `index.html`:
151143
const { token } = await directLineTokenRes.json();
152144

153145
- const webSpeechPonyfillFactory = await window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory({
154-
- authorizationToken: fetchSpeechServicesToken,
155-
- region: await fetchSpeechServicesRegion()
146+
- credentials: fetchSpeechServicesCredentials
156147
- });
157148

158149
+ async function createSpeechRecognitionOnlyPonyfillFactory() {
159150
+ const speechServicesPonyfillFactory = await window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory(
160151
+ {
161-
+ authorizationToken: fetchSpeechServicesToken,
162-
+ region: await fetchSpeechServicesRegion()
152+
+ credentials: fetchSpeechServicesCredentials
163153
+ }
164154
+ );
165155
+

samples/03.speech/d.cognitive-speech-services-speech-recognition-only/index.html

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@
3535
let lastPromise;
3636

3737
return () => {
38+
const now = Date.now();
39+
3840
// Fetch a new token if the existing one is expiring.
3941
// The following article mentioned the token is only valid for 10 minutes.
4042
// We will invalidate the token after 5 minutes.
4143
// https://docs.microsoft.com/en-us/azure/cognitive-services/authentication#authenticate-with-an-authentication-token
42-
if (Date.now() > expireAfter) {
43-
const speechServicesTokenResPromise = fetch(
44-
'https://webchat-mockbot.azurewebsites.net/speechservices/token',
45-
{ method: 'POST' }
46-
);
47-
48-
expireAfter = Date.now() + 300000;
49-
lastPromise = speechServicesTokenResPromise.then(
44+
if (now > expireAfter) {
45+
expireAfter = now + 300000;
46+
lastPromise = fetch('https://webchat-mockbot.azurewebsites.net/speechservices/token', {
47+
method: 'POST'
48+
}).then(
5049
res => res.json(),
5150
err => {
52-
lastPromise = null;
51+
expireAfter = 0;
5352

5453
return Promise.reject(err);
5554
}
@@ -62,14 +61,6 @@
6261

6362
const fetchSpeechServicesCredentials = createFetchSpeechServicesCredentials();
6463

65-
async function fetchSpeechServicesRegion() {
66-
return (await fetchSpeechServicesCredentials()).region;
67-
}
68-
69-
async function fetchSpeechServicesToken() {
70-
return (await fetchSpeechServicesCredentials()).token;
71-
}
72-
7364
(async function() {
7465
// In this demo, we are using Direct Line token from MockBot.
7566
// Your client code must provide either a secret or a token to talk to your bot.
@@ -87,12 +78,9 @@
8778
// Create the ponyfill factory function, which can be called to create a concrete implementation of the ponyfill.
8879
const speechServicesPonyfillFactory = await window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory(
8980
{
90-
// We are passing the Promise function to the authorizationToken field.
81+
// We are passing the Promise function to the "credentials" field.
9182
// This function will be called every time the token is used.
92-
authorizationToken: fetchSpeechServicesToken,
93-
94-
// In contrast, we are only fetching the region once.
95-
region: await fetchSpeechServicesRegion()
83+
credentials: fetchSpeechServicesCredentials
9684
}
9785
);
9886

0 commit comments

Comments
 (0)