首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >中的加载程序图标

中的加载程序图标
EN

Stack Overflow用户
提问于 2019-05-28 05:31:58
回答 1查看 707关注 0票数 0

我正在使用网络聊天。很少有与用户相关的数据,我是通过store选项使用反向通道post活动来向用户发送的。

代码语言:javascript
复制
<ReactWebChat
  activityMiddleware={ activityMiddleware }
  directLine={ window.WebChat.createDirectLine( this.state.token ) }
  store = {this.handleGetStore()}
  styleOptions={styleOptions}
/>

handleGetStore返回存储数据:

代码语言:javascript
复制
handleGetStore(){
    const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
        if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
            dispatch({
                type: 'WEB_CHAT/SEND_EVENT',
                payload: {
                    name: 'userDetail',
                    value: this.state.userDetail
                }
            });
        }
        return next(action);
    });
    return store;
}

当连接启动时,将出现加载程序。

在此之后,在欢迎消息出现之前有大约3-5秒的延迟,同时,网络聊天似乎已经为用户做好了准备。

稍微延迟3秒是可以接受的,但通常延迟最多10秒或更长。我知道,通过使用App的Always On特性和扩展计划,可以稍微改进这一点。有没有一种方法,我可以等待后通道欢迎消息出现并显示加载程序,直到那时?

参考资料:https://github.com/microsoft/BotFramework-WebChat/pull/1866

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-29 17:48:40

不幸的是,连接状态显示依赖于从DirectLineJs接收到的事件,而Web Chat目前不支持自定义其行为。尽管如此,还是有一种通过发送伪DirectLine事件来完成您想要完成的任务的方法。

以下是以下步骤:

  • 创建一个标志,该标志将指示机器人是否发送了欢迎消息- received_welcome_message
  • 当welcome发送连接已完成的事件时,请检查标志以确保已收到欢迎消息。如果bot尚未发送欢迎消息,则将欢迎事件发送给bot,并将连接状态重置为满足。
  • 当welcome接收到机器人的活动时,请检查它是否是欢迎消息。我建议在bot端的消息中添加一个name属性来检查- await context.sendActivity({ text: 'Welcome', name: 'welcome'})。如果该活动是一条欢迎消息,则分派一个连接完成事件并将标志设置为true。

要了解更多细节,请看下面的代码片段。

代码语言:javascript
复制
let received_welcome_message = false;
const store = createStore(
  {},
  ({ dispatch}) => next => action => {

    if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {

      if (!received_welcome_message) {
        dispatch({
          type: 'DIRECT_LINE/CONNECT_FULFILLING'
        });
        dispatch({
          type: 'WEB_CHAT/SEND_EVENT',
          payload: { name: 'webchat/join' }
        });

        return
      }

    } else if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY' && action.payload.activity.name === 'welcome') {
      received_welcome_message = true;
      dispatch({
        type: 'DIRECT_LINE/CONNECT_FULFILLED',
      });
    }
    return next(action);
  }
);

编辑

一种不那么麻烦的方法是,在完成与bot的连接时,发送一个post活动挂起事件,以模拟机器人发送欢迎消息。注意,机器人不知道模拟的活动。请参阅下面的代码片段。

代码语言:javascript
复制
const store = createStore(
  {},
  ({ dispatch}) => next => action => {
    console.log(action)
    if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
      dispatch({
        type: 'DIRECT_LINE/POST_ACTIVITY_PENDING',
        meta: { method: 'keyboard' },
        payload: {
          activity: {
            from: { role: "bot" },
            text: "Welcome Message",
            textFormat: "plain",
            timestamp: new Date().toString(),
            type: "message"
          }
        }
      })
    }
    return next(action);
  }

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56335551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档