Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

reactjs - How to add AutoComplete/AutoSuggestion in Microsoft botframework webchat using React.js

I'm trying to add autosuggestion/autocomplete function in my bot-framework web chat(v-4) using react js. In which i want to fetch input data from azure table. Heard that its not recommended to use j-query inside React js. Looking for a solution to add this.

I'm looking for a solution like in the below image, PFA. And the code which i used for React is attached below,

React.js file

import React from 'react';
import { DirectLine, ConnectionStatus } from 'botframework-directlinejs';
import ReactWebChat from 'botframework-webchat';
import './ChatComponent.css';


export default class extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            token: '',
            conversationId:'',
            directLine: {},
            view: false,
            feedBack: null,
            value: '',
            popupContent: '',
            storeValue:''
        };
        this.handleTokenGeneration = this.handleTokenGeneration.bind(this);
        this.handleChange = this.handleChange.bind(this);

    }

    handleTokenGeneration = async () => {
        console.log("handleTokenGeneration");
        const response = await fetch(`api/TokenGenerationService/GetToken`);
        const data = await response.json();
        this.setState({ token: data.categoryObject.token, conversationId: data.categoryObject.conversationId });
        console.log("conversationId");
    };

    async componentDidMount() {
        try {
            await this.handleTokenGeneration();          

        } catch (error) {
            console.log("error in fetchung token");
            console.log(error);
        }

        this.state.directLine = new DirectLine({ token: this.state.token });
        this.setState({ view: true });

    }


    handleChange = (event) => {
        this.setState({ value: event.target.value });
    }

    render() {

        if (!this.state.view) {
            return <div />
        } else {        
            return (             

                <div className="react-container webchat" >
                    <ReactWebChat directLine={this.state.directLine} webSocket={true}  userID='2656' username='res' store={this.state.storeValue} />

                    <footer className="chat-footer" >                     

                        <div className="foot-footer">
                            Was I helpful ?
                            <span className="feedback"  >Yes</span><span>|</span><span className="feedback" >No</span>
                        </div>
                    </footer>
                </div>
            );
        }
    }

}

I'm looking for something like this. Please find the attached file below

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Web Chat uses Redux which has a Redux store that can use Redux middleware. Web chat has an action called WEB_CHAT/SET_SEND_BOX that can be used to respond to what the user types in the text input box like this:

const store = window.WebChat.createStore(
    {},
    store => next => action => {
        if (action.type === 'WEB_CHAT/SET_SEND_BOX') {
            const user_entered_text = action.payload.text;

            // Use the text to query the Azure database and display suggestions
        }
        return next(action);
    }
);

When the user clicks on a suggestion or presses the right key, you can use that same action to change what's in the text input box like this:

store.dispatch({
    type: 'WEB_CHAT/SET_SEND_BOX',
    payload: {
        text: user_selected_suggestion,
    }
});

There are samples in the Web Chat repo that may help with using Redux actions in Web Chat


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...