BotFramework-WebChat icon indicating copy to clipboard operation
BotFramework-WebChat copied to clipboard

Trying to add Speech to text in react minimizable chat example , no success

Open jchen706 opened this issue 7 years ago • 3 comments

//minimizableWebChat.js
import React from 'react';
import { createStore, createStyleSet } from 'botframework-webchat';

import WebChat from './WebChat';

import './fabric-icons-inline.css';
import './MinimizableWebChat.css';

export default class extends React.Component {
  constructor(props) {
    super(props);

    this.handleFetchToken = this.handleFetchToken.bind(this);
    this.handleMaximizeButtonClick = this.handleMaximizeButtonClick.bind(this);
    this.handleMinimizeButtonClick = this.handleMinimizeButtonClick.bind(this);
      this.handleSwitchButtonClick = this.handleSwitchButtonClick.bind(this);
      this.createCognitiveServicesBingSpeechPonyfillFactory = this.createCognitiveServicesBingSpeechPonyfillFactory.bind(this);


    const store = createStore({}, ({ dispatch }) => next => action => {
      if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
          setTimeout(() => {  
          dispatch({
            type: 'WEB_CHAT/SEND_EVENT',
            payload: {
              name: 'webchat/join',
              value: {
                language: window.navigator.language
              }
            }
          });
        }, 1000);
      } else if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
        if (action.payload.activity.from.role === 'bot') {
          this.setState(() => ({ newMessage: true }));
        }
      }

      return next(action);
    });

    this.state = {
      minimized: true,
      newMessage: false,
      side: 'right',
      store,
      styleSet: createStyleSet({
        backgroundColor: 'Transparent'
      }),
        token: null,
        webSpeechPonyfillFactory: null
    };
  }

  async handleFetchToken() {
    if (!this.state.token) {
      const res = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', { method: 'POST' });
      const { token } = await res.json();

      this.setState(() => ({ token }));
    }
  }

 async createCognitiveServicesBingSpeechPonyfillFactory() {
        const webSpeechPonyfillFactory = await createCognitiveServicesSpeechServicesPonyfillFactory({
            region: 'westus', subscriptionKey: '',
        });
        this.setState({ webSpeechPonyfillFactory: webSpeechPonyfillFactory });
 }

  handleMaximizeButtonClick() {
    this.setState(() => ({
      minimized: false,
      newMessage: false
    }));
  }

  handleMinimizeButtonClick() {
    this.setState(() => ({
      minimized: true,
      newMessage: false
    }));
  }

  handleSwitchButtonClick() {
    this.setState(({ side }) => ({
      side: side === 'left' ? 'right' : 'left'
    }));
  }

  render() {
    const { state: {
      minimized,
      newMessage,
      side,
      store,
      styleSet,
      token,
      webSpeechPonyfillFactory
    } } = this;

    return (
      <div className="minimizable-web-chat">
        {
          minimized ?
            <button
              className="maximize"
              onClick={ this.handleMaximizeButtonClick }
            >
              <span className={ token ? 'ms-Icon ms-Icon--MessageFill' : 'ms-Icon ms-Icon--Message' } />
              {
                newMessage &&
                  <span className="ms-Icon ms-Icon--CircleShapeSolid red-dot" />
              }
            </button>
          :
            <div
              className={ side === 'left' ? 'chat-box left' : 'chat-box right' }
            >
              <header>
                <div className="filler" />
                <button
                  className="switch"
                  onClick={ this.handleSwitchButtonClick }
                >
                  <span className="ms-Icon ms-Icon--Switch" />
                </button>
                <button
                  className="minimize"
                  onClick={ this.handleMinimizeButtonClick }
                >
                  <span className="ms-Icon ms-Icon--ChromeMinimize" />
                </button>
              </header>
              <WebChat
                            className="react-web-chat"
                            onFetchToken={this.handleFetchToken}
                            store={store}
                            styleSet={styleSet}
                            token={token}
                            onFetchSpeech={this.createCognitiveServicesBingSpeechPonyfillFactory}

                            webSpeechPonyfillFactory={webSpeechPonyfillFactory}
              />
            </div>
        }
      </div>
    );
  }
}

jchen706 avatar Jul 14 '19 19:07 jchen706

//webchat.js`//library for saving result after calculation and import React
import memoize from 'memoize-one';
import React from 'react';
import ReactWebChat, { createDirectLine, createStyleSet, Components } from 'botframework-webchat';

import './WebChat.css';

export default class extends React.Component {
    //constructor for the  component
    constructor(props) {
        super(props);

        this.createDirectLine = memoize(token => createDirectLine({
            token:""
        }));

        this.state = {
            styleSet: createStyleSet({
                backgroundColor: 'Transparent'
            }),

          
        };
    }

    componentDidMount() {
        !this.props.token && this.props.onFetchToken();
    }


    render() {
        const {
            props: { className, store, token },
            state: { styleSet, webSpeechPonyfillFactory }
        } = this;

        return (
            token ?
                <ReactWebChat
                    className={`${className || className} web-chat`}
                    directLine={this.createDirectLine(token)}
                    store={store}
                    styleSet={styleSet}
                    webSpeechPonyfillFactory={webSpeechPonyfillFactory}

                />
                :
                <div className={`${className || className} connect-spinner`}>
                    <div className="content">
                        <div className="icon">
                            <span className="ms-Icon ms-Icon--Robot" />
                        </div>
                        <p>Please wait while we are connecting.</p>
                    </div>
                </div>
        );
    }
}

jchen706 avatar Jul 14 '19 19:07 jchen706

Thanks, I figure it out.

jchen706 avatar Jul 15 '19 14:07 jchen706

Hi @jchen706, Can you direct me to the sample or the reference to getting the Speech integrated in this sample I am working on this speech integration not able to figure it out. Any help will be much appreciated.

Thanks in advance.

Edwardfelix08 avatar Jan 04 '21 12:01 Edwardfelix08