首页 > 解决方案 > BotFramework V4:如何从机器人发送事件并在反应 WebChat 中捕获它?

问题描述

我从我的机器人(.NET SDK)发送了一个名为“locationRequest”的事件

            Activity activity = new Activity
            {
                Type = ActivityTypes.Event,
                Name = "locationRequest"
            };
            await stepContext.Context.SendActivityAsync(activity, cancellationToken);

我想在 WebChat 客户端应用程序中捕获此事件,基于 React 中编码的 minizable-web-chat,并将布尔 locationRequested 设置为 True:

const store = createStore({}, ({ dispatch }) => next => action => {
  if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
    dispatch({
      type: 'WEB_CHAT/SEND_EVENT',
      payload: {
        name: 'webchat/join',
      }
    });
  }
  else if(action.name == 'locationRequest'){
    this.setState(() => ({
      locationRequested: true
    }));
  }
  return next(action);
});

我无法赶上这个活动,请问有什么想法吗?

标签: c#react-nativebotframeworkweb-chat

解决方案


你走在正确的轨道上。基本上,您可以监视“DIRECT_LINE/INCOMING_ACTIVITY”事件,然后检查传入活动是否具有适当的名称。有关更多详细信息,请查看下面的代码片段和传入事件Web 聊天示例。

const store = createStore({}, ({ dispatch }) => next => action => {
  if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
    dispatch({
      type: 'WEB_CHAT/SEND_EVENT',
      payload: {
        name: 'webchat/join',
      }
    });
  }
  else if(action.type === 'DIRECT_LINE/INCOMING_ACTIVITY'){
    if (action.payload.activity.name === 'locationRequest') {
      this.setState(() => ({
        locationRequested: true
      }));
    }
  }
  return next(action);
});

推荐阅读