WebChat编程式帖子活动不起作用-单击按钮后未发送任何消息

时间:2019-05-29 15:31:23

标签: botframework

我正在跟踪这两个示例:

  1. Webchat with react
  2. Programmatic post activity

我的机器人运行正常。我可以通过直线发送和处理活动。我的测试helpButton记录为ok,但是当我单击示例中的按钮时,没有发送任何实际的“帮助”消息。

var mainBotConnection;

const { createStore, ReactWebChat } = window.WebChat;
const { createProvider } = window.ReactRedux;
const Provider = createProvider('webchat');
const Store = createStore();

// get a token
const RequestToken = async (user) => {
    ...
};

(async function () {
   RequestToken(agent)
        .then(token => {

            //init main chat bot
            mainBotConnection = window.WebChat.createDirectLine({token: token});

            ...

            //grab mainbot placeholder and put it on screen
            window.ReactDOM.render(
                <Provider store={Store}>
                    <ReactWebChat
                        directLine={mainBotConnection}
                        storeKey='webchat'
                        userID={user.id}
                        username={user.name}
                        styleOptions={mainBotStyleOptions}
                    />
                </Provider>,
                document.getElementById('webchat'));

            // this message does not appear
            Store.dispatch({
                type: 'WEB_CHAT/SEND_MESSAGE',
                payload: { text: 'StartUp hello!' }
            });
        });
   
    // test button
    document.querySelector('#helpButton').addEventListener('click', () => {
        // this is successfully logged 
        console.log(`help button clicked`);
        
        // 'help' text does not appear in bot
        Store.dispatch({
            type: 'WEB_CHAT/SEND_MESSAGE',
            payload: { text: 'help' }
        });
        
        // this is also successfully logged 
        console.log(Store);
    });

    document.querySelector('#webchat').focus();
})().catch(err => console.error(err));

1 个答案:

答案 0 :(得分:0)

您需要将store={Store}添加到您的ReactWebChat组件中:

[...]
<Provider store={Store}>
    <ReactWebChat
        directLine={mainBotConnection}
        storeKey='webchat'
        userID={user.id}
        username={user.name}
        styleOptions={mainBotStyleOptions}
        store={Store} // ADD THIS PART
    />
</Provider>,
[...]

话虽这么说,但是如果没有其余的代码,我将无法进行准确的测试。相反,我启动了React with Redux Sample。如果我删除了store={Store},它将无法正常工作,但是如果我将其保留在其中,它将正常工作并发送欢迎消息和帮助消息。您可能还需要:<Provider store={ store } key='webchat'>,但是就像我说的那样,我无法测试您的确切代码。