如何使用 React.js 在 Microsoft botframework 网络聊天中添加 AutoComplete/AutoSuggestion

IT技术 reactjs .net-core autocomplete botframework
2021-04-05 05:45:33

我正在尝试使用 react js 在我的机器人框架网络聊天(v-4)中添加自动建议/自动完成功能。我想从 azure 表中获取输入数据。听说不建议在 React js 中使用 j-query。寻找解决方案来添加它。

我正在寻找如下图所示的解决方案,PFA。我用于 React 的代码附在下面,

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>
            );
        }
    }

}

我正在寻找这样的东西。 请在下面找到附件

1个回答

网络聊天使用Redux,它有一个Redux 商店,可以使用Redux 中间件网络聊天有一个名为的操作WEB_CHAT/SET_SEND_BOX,可用于响应用户在文本输入框中键入的内容,如下所示:

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);
    }
);

当用户单击建议或按右键时,您可以使用相同的操作来更改文本输入框中的内容,如下所示:

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

网络聊天存储库中的示例可能有助于在网络聊天中使用 Redux 操作

@SuneetPatil - 我不确定你在问什么,但最好在一个新问题中问它
2021-06-07 05:45:33
凯尔,谢谢你的片段。但是,您能否解释一下我们如何列出发送框的建议(字符串数组)?
2021-06-12 05:45:33