【问题标题】:BotFramework V4: how to send an event from the bot and catch it in react WebChat?BotFramework V4:如何从机器人发送事件并在反应 WebChat 中捕获它?
【发布时间】:2020-01-30 13:05:24
【问题描述】:

我从我的机器人 (.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-native botframework web-chat


    【解决方案1】:

    你走在正确的轨道上。基本上,您可以监视“DIRECT_LINE/INCOMING_ACTIVITY”事件,然后检查传入活动是否具有适当的名称。有关详细信息,请查看下面的代码 sn-p 和 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);
    });
    

    【讨论】:

    • 非常感谢它的工作!在我的用例中,上下文位于带有 WaterfallSteps 的 ComponentDialog 中: - 机器人正在发送一个 ChoicePrompt “你希望机器人获取你的位置吗?是/否” - 如果是,机器人发送一个“locationRequest”事件 - WebChat接收事件并显示 GetLocation 按钮 - 当用户单击该按钮时,它将地理位置发送给机器人并继续对话框我们如何暂停 ComponentDialog 以让用户单击网络聊天中的按钮?
    • 很高兴我能帮上忙。如果您可以将我的答案标记为已接受,我将不胜感激。关于您的第二个问题,我建议您在 StackOverflow 上提出一个新问题。它需要太多的代码才能在 cmets 中涵盖。
    • 我的第二个问题就在这里:stackoverflow.com/questions/59996351/…
    【解决方案2】:

    您正在从您的机器人发送一个活动,这意味着您可以尝试捕获该活动,然后您可以检查该活动的名称是否为“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

    【讨论】:

    • 尝试添加描述所使用的技术或框架或编程语言的标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多