fix(kb.gbapp): #332 fix STS.

This commit is contained in:
rodrigorodriguez 2023-02-26 18:15:22 -03:00
parent c94228cd8d
commit 01cf2803a0
3 changed files with 49 additions and 54 deletions

View file

@ -619,7 +619,7 @@ export class GBMinService {
* Gets a Speech to Text / Text to Speech token from the provider. * Gets a Speech to Text / Text to Speech token from the provider.
*/ */
private async getSTSToken(instance: any) { private async getSTSToken(instance: any) {
return null; // TODO: https://github.com/GeneralBots/BotServer/issues/332
const options = { const options = {
method: 'POST', method: 'POST',
headers: { headers: {
@ -628,7 +628,9 @@ export class GBMinService {
}; };
try { try {
return await fetch(instance.speechEndpoint, options);
const res = await fetch(instance.speechEndpoint, options)
return res.text();
} catch (error) { } catch (error) {
const msg = `Error calling Speech to Text client. Error is: ${error}.`; const msg = `Error calling Speech to Text client. Error is: ${error}.`;

View file

@ -1,6 +1,6 @@
{ {
"name": "default.gbui", "name": "default.gbui",
"version": "1.0.0", "version": "3.0.0",
"private": false, "private": false,
"repository": "https://github.com/GeneralBots/BotServer", "repository": "https://github.com/GeneralBots/BotServer",
"description": "Default web interface for General Bots open-core", "description": "Default web interface for General Bots open-core",

View file

@ -40,11 +40,11 @@ import GBBulletPlayer from './players/GBBulletPlayer.js';
import SidebarMenu from './components/SidebarMenu.js'; import SidebarMenu from './components/SidebarMenu.js';
import SEO from './components/SEO.js'; import SEO from './components/SEO.js';
import GBCss from './components/GBCss.js'; import GBCss from './components/GBCss.js';
import { DirectLine } from 'botframework-directlinejs'; import { DirectLine, createCognitiveServicesSpeechServicesPonyfillFactory } from 'botframework-directlinejs';
import { ConnectionStatus } from 'botframework-directlinejs'; import { ConnectionStatus } from 'botframework-directlinejs';
import ReactWebChat from 'botframework-webchat'; import ReactWebChat from 'botframework-webchat';
import { UserAgentApplication } from 'msal'; import { UserAgentApplication } from 'msal';
import StaticContent from '@midudev/react-static-content' import StaticContent from '@midudev/react-static-content';
class GBUIApp extends React.Component { class GBUIApp extends React.Component {
constructor() { constructor() {
@ -77,21 +77,17 @@ class GBUIApp extends React.Component {
} }
send(command) { send(command) {
window.line.postActivity({
window.line type: 'event',
.postActivity({ name: command,
type: 'event', locale: 'en-us',
name: command, textFormat: 'plain',
locale: 'en-us', timestamp: new Date().toISOString(),
textFormat: 'plain', from: this.getUser()
timestamp: new Date().toISOString(), });
from: this.getUser()
});
} }
getUser() { getUser() {
return { id: 'web@gb', name: 'You' }; return { id: 'web@gb', name: 'You' };
} }
@ -129,7 +125,6 @@ class GBUIApp extends React.Component {
} }
authenticate() { authenticate() {
if (this.state.instanceClient.authenticatorClientId === null) { if (this.state.instanceClient.authenticatorClientId === null) {
return; return;
} }
@ -150,8 +145,12 @@ class GBUIApp extends React.Component {
); );
window.userAgentApplication = userAgentApplication; window.userAgentApplication = userAgentApplication;
if (!userAgentApplication.isCallback(window.location.hash) && window.parent === window if (
&& !window.opener && userAgentApplication.getUser) { !userAgentApplication.isCallback(window.location.hash) &&
window.parent === window &&
!window.opener &&
userAgentApplication.getUser
) {
var user = userAgentApplication.getUser(); var user = userAgentApplication.getUser();
if (user) { if (user) {
userAgentApplication.acquireTokenSilent(graphScopes).then( userAgentApplication.acquireTokenSilent(graphScopes).then(
@ -177,6 +176,7 @@ class GBUIApp extends React.Component {
line.connectionStatus$.subscribe(connectionStatus => { line.connectionStatus$.subscribe(connectionStatus => {
if (connectionStatus === ConnectionStatus.Online) { if (connectionStatus === ConnectionStatus.Online) {
line.setUserId = null;
_this_.setState({ instanceClient: instanceClient }); _this_.setState({ instanceClient: instanceClient });
window['botConnection'] = line; window['botConnection'] = line;
} }
@ -211,6 +211,7 @@ class GBUIApp extends React.Component {
this.configureChat(); this.configureChat();
} }
webSpeechPonyfillFactory = 0;
render() { render() {
let playerComponent = ''; let playerComponent = '';
@ -302,43 +303,37 @@ class GBUIApp extends React.Component {
</div> </div>
); );
async function fetchCredentials() {
const res = await fetch('/api/authorizationtoken');
if (res.ok) {
return {
authorizationToken: await res.text(),
region: 'westus2'
};
} else {
throw new Error('Failed to retrieve authorization token for Cognitive Services.');
}
}
if (this.state.line) { if (this.state.line) {
if (this.state.instanceClient) { if (this.state.instanceClient) {
gbCss = <GBCss instance={this.state.instanceClient} />; gbCss = <GBCss instance={this.state.instanceClient} />;
seo = <SEO instance={this.state.instanceClient} />; seo = <SEO instance={this.state.instanceClient} />;
const token = this.state.instanceClient.speechToken;
chat = (
<ReactWebChat
ref={chat => {
this.chat = chat;
}}
locale={'pt-br'}
directLine={this.state.line}
webSpeechPonyfillFactory={window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory({
credentials: { authorizationToken: token, region: 'westus' }
})}
/>
);
} }
// let speechOptions;
// let token = this.state.instanceClient.speechToken;
// speechOptions = {
// speechRecognizer: new SpeechRecognizer({
// locale: "pt-br",
// fetchCallback: (authFetchEventId) => getToken(),
// fetchOnExpiryCallback: (authFetchEventId) => getToken()
// }),
// speechSynthesizer: new SpeechSynthesizer({
// fetchCallback: (authFetchEventId) => getToken(),
// fetchOnExpiryCallback: (authFetchEventId) => getToken(),
// gender: SynthesisGender.Male,
// voiceName: 'Microsoft Server Speech Text to Speech Voice (pt-BR, Daniel, Apollo)'
// })
// };
chat = (
<ReactWebChat
ref={chat => {
this.chat = chat;
}}
locale={'pt-br'}
directLine={this.state.line}
user={this.getUser()}
bot={{ id: 'bot@gb', name: 'Bot' }}
/>
);
} }
if (!this.state.instanceClient) { if (!this.state.instanceClient) {
@ -352,9 +347,7 @@ class GBUIApp extends React.Component {
{gbCss} {gbCss}
{sideBar} {sideBar}
<div className="player">{playerComponent}</div> <div className="player">{playerComponent}</div>
<div className="webchat"> <div className="webchat">{chat}</div>
{chat}
</div>
</div> </div>
</StaticContent> </StaticContent>
); );