BotFramework V4:如何从机器人发送事件并在 React WebChat 中捕获它?

BotFramework V4: how to send an event from the bot and catch it in react 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);
});

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

您的方向是正确的。基本上,您可以监视 'DIRECT_LINE/INCOMING_ACTIVITY' 事件,然后检查传入的 activity 是否具有适当的名称。有关详细信息,请查看下面的代码片段和 Incoming Event 网络聊天示例。

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

您正在从您的机器人发送一个 activity,这意味着您可以尝试捕获 activity 然后您可以检查 activity 的名称是否为 "locationRequest",然后更改 locationRequested 变量的值。

看起来像这样:

// We are adding a new middleware to customize the behavior of DIRECT_LINE/INCOMING_ACTIVITY.
    const store = window.WebChat.createStore(
      {},
      ({ dispatch }) => next => action => {
       if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
         if (action.payload.activity.name == "locationRequest") {
           this.setState(() => ({ locationRequested: true }));
         }
       }

        return next(action);
      }
    );

希望对您有所帮助!顺便提一句。我的回答基于 this sample