Skip to content

Commit

Permalink
fix(kb.gbapp): #332 fix STS.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Feb 26, 2023
1 parent c94228c commit 01cf280
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 54 deletions.
6 changes: 4 additions & 2 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ export class GBMinService {
* Gets a Speech to Text / Text to Speech token from the provider.
*/
private async getSTSToken(instance: any) {
return null; // TODO: https://github.com/GeneralBots/BotServer/issues/332

const options = {
method: 'POST',
headers: {
Expand All @@ -628,7 +628,9 @@ export class GBMinService {
};

try {
return await fetch(instance.speechEndpoint, options);

const res = await fetch(instance.speechEndpoint, options)
return res.text();
} catch (error) {
const msg = `Error calling Speech to Text client. Error is: ${error}.`;

Expand Down
2 changes: 1 addition & 1 deletion packages/default.gbui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "default.gbui",
"version": "1.0.0",
"version": "3.0.0",
"private": false,
"repository": "https://github.com/GeneralBots/BotServer",
"description": "Default web interface for General Bots open-core",
Expand Down
95 changes: 44 additions & 51 deletions packages/default.gbui/src/GBUIApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ import GBBulletPlayer from './players/GBBulletPlayer.js';
import SidebarMenu from './components/SidebarMenu.js';
import SEO from './components/SEO.js';
import GBCss from './components/GBCss.js';
import { DirectLine } from 'botframework-directlinejs';
import { DirectLine, createCognitiveServicesSpeechServicesPonyfillFactory } from 'botframework-directlinejs';
import { ConnectionStatus } from 'botframework-directlinejs';
import ReactWebChat from 'botframework-webchat';
import { UserAgentApplication } from 'msal';
import StaticContent from '@midudev/react-static-content'
import StaticContent from '@midudev/react-static-content';

class GBUIApp extends React.Component {
constructor() {
Expand Down Expand Up @@ -77,21 +77,17 @@ class GBUIApp extends React.Component {
}

send(command) {

window.line
.postActivity({
type: 'event',
name: command,
locale: 'en-us',
textFormat: 'plain',
timestamp: new Date().toISOString(),
from: this.getUser()
});

window.line.postActivity({
type: 'event',
name: command,
locale: 'en-us',
textFormat: 'plain',
timestamp: new Date().toISOString(),
from: this.getUser()
});
}

getUser() {

return { id: 'web@gb', name: 'You' };
}

Expand Down Expand Up @@ -129,7 +125,6 @@ class GBUIApp extends React.Component {
}

authenticate() {

if (this.state.instanceClient.authenticatorClientId === null) {
return;
}
Expand All @@ -150,8 +145,12 @@ class GBUIApp extends React.Component {
);
window.userAgentApplication = userAgentApplication;

if (!userAgentApplication.isCallback(window.location.hash) && window.parent === window
&& !window.opener && userAgentApplication.getUser) {
if (
!userAgentApplication.isCallback(window.location.hash) &&
window.parent === window &&
!window.opener &&
userAgentApplication.getUser
) {
var user = userAgentApplication.getUser();
if (user) {
userAgentApplication.acquireTokenSilent(graphScopes).then(
Expand All @@ -177,6 +176,7 @@ class GBUIApp extends React.Component {

line.connectionStatus$.subscribe(connectionStatus => {
if (connectionStatus === ConnectionStatus.Online) {
line.setUserId = null;
_this_.setState({ instanceClient: instanceClient });
window['botConnection'] = line;
}
Expand Down Expand Up @@ -211,6 +211,7 @@ class GBUIApp extends React.Component {
this.configureChat();
}

webSpeechPonyfillFactory = 0;
render() {
let playerComponent = '';

Expand Down Expand Up @@ -302,43 +303,37 @@ class GBUIApp extends React.Component {
</div>
);

async function fetchCredentials() {
const res = await fetch('/api/authorizationtoken');

if (this.state.line) {
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.instanceClient) {

gbCss = <GBCss 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) {
Expand All @@ -352,9 +347,7 @@ class GBUIApp extends React.Component {
{gbCss}
{sideBar}
<div className="player">{playerComponent}</div>
<div className="webchat">
{chat}
</div>
<div className="webchat">{chat}</div>
</div>
</StaticContent>
);
Expand Down

0 comments on commit 01cf280

Please sign in to comment.