首页 > 解决方案 > 无法通过 IE11/es5 的 botframework-webchat 中的自定义存储/调度发送消息

问题描述

我的客户要求我们的聊天机器人支持 IE11。我不得不修改网络聊天代码以不使用箭头函数等 es6 功能,因为 IE11/es5 不支持它们。在大多数情况下,我是成功的,但我无法开始工作的一件事是发送我正在发送的消息事件。新代码在 Chrome 中有效,但我在 IE11 中收到预期标识符错误消息。以下是相关代码:

const store = window.WebChat.createStore({}, function({dispatch}) { return function(next) { return function(action) {
    if (action.type === 'WEB_CHAT/SEND_MESSAGE') {
        // Message sent by the user
        PageTitleNotification.Off();
        clearTimeout(interval);
    } else if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY' && action.payload.activity.name !== "inactive") {
        // Message sent by the bot
        clearInterval(interval);
        interval = setTimeout(function() {
            // Change title to flash the page
            PageTitleNotification.On('Are you still there?');

            // Notify bot the user has been inactive
            dispatch({
                type: 'WEB_CHAT/SEND_EVENT',
                payload: {
                    name: 'inactive',
                    value: ''
                }
            });

        }, 3000)
    }
    return next(action);
}}});

以前第一行看起来像

const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {

问题来了function({dispatch})。IE 控制台说需要一个标识符并且整个网络聊天无法加载。它在 Chrome 中运行良好。如果我更改function({dispatch})为 just function(dispatch),机器人会呈现,但发送非活动消息到机器人(如下)的调度语句不再在 IEChrome 中起作用。

// Notify bot the user has been inactive
dispatch({
    type: 'WEB_CHAT/SEND_EVENT',
    payload: {
        name: 'inactive',
        value: ''
    }
});

为什么 IE 无法识别{dispatch}为标识符,我该怎么做才能使其正常工作?

标签: javascriptnode.jsbotframeworkinternet-explorer-11es5-compatiblity

解决方案


IE11 不支持该语法。ES6 允许通过变量名设置对象属性,所以 ES5 等价物是:

window.WebChat.createStore({}, { dispatch: dispatch }) {
  // ...
}

编辑:

经过一番调试,最终的解决方案是直接将dispatch对象传递给createStore函数,并访问其dispatch方法:

dispatch.dispatch({
    type: 'WEB_CHAT/SEND_EVENT',
    payload: {
        name: 'inactive',
        value: ''
    }
});

您可以重命名参数以减少冗余,但这不是必需的。


推荐阅读