简体   繁体   English

如何将自定义负载从 React bf DIRECT_LINE/CONNECT_FULFILLED 发送到 onMembersAdded 事件

[英]How to send custom payload from React bf DIRECT_LINE/CONNECT_FULFILLED to onMembersAdded event

I have been using React webchat to interact with the botframework. I need to send some custom payload to onMembersAdded event.我一直在使用 React 网络聊天与 botframework 进行交互。我需要向 onMembersAdded 事件发送一些自定义负载。

Reference link 参考链接

Below is my code下面是我的代码

if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
  dispatch({
    type: 'WEB_CHAT/SEND_EVENT',
    payload: {
      name: 'webchat/join',
      value: {
        language: window.navigator.language,
        key: "customdata",  // custom value
      }
    }
  });
}

My app service logic我的应用服务逻辑

this.onMembersAdded(async (context, next) => {
    console.log('Running onMembersAdded Activity.');
    console.log(`Processing onMembersAdded Activity :: ${JSON.stringify(context.activity)}`);
    
    await context.sendActivity('Welcome to the QnA Maker sample! Ask me a question and I will try to answer it.');

    // By calling next() you ensure that the next BotHandler is run.
    await next();
});

Log response日志响应

{
  "type": "conversationUpdate",
  "id": "Il5FyDXlgNn",
  "timestamp": "2022-03-23T12:36:21.930Z",
  "serviceUrl": "https://webchat.botframework.com/",
  "channelId": "webchat",
  "from": {
    "id": "9sdfs15-0b2a-43b1-9c7e-7d29ffa79f23"
  },
  "conversation": {
    "id": "KywsdfkRLNGDCxjBNQQsVn-in"
  },
  "recipient": {
    "id": "xxx_xxx@B6i2sd3Yvs4",
    "name": "xxx_xxx"
  },
  "membersAdded": [
    {
      "id": "xxx_xxx@B6i2sd3Yvs4",
      "name": "xxx_xxx"
    },
    {
      "id": "9sdfs15-0b2a-43b1-9c7e-7d29ffa79f23"
    }
  ],
  "locale": "en-US",
  "rawTimestamp": "2022-03-23T12:36:21.9302875Z",
  "callerId": "urn:botframework:azure"
}

Unable to see my custom payload in log response.无法在日志响应中看到我的自定义负载。

I could use onEvent event and check for (context.activity.name), but I'm curious to know how's its done with onMembersAdded event.我可以使用 onEvent 事件并检查 (context.activity.name),但我很想知道它是如何处理 onMembersAdded 事件的。

The issue might be raising because of the React JS version.由于 React JS 版本,可能会出现此问题。 As there are some glitches in handling Webchat with React V17.0.2 and it breaks the operation often.由于使用 React V17.0.2 处理 Webchat 时存在一些故障,并且经常中断操作。 Try to downgrade the version of React to v16.8.6.尝试将 React 版本降级到 v16.8.6。

https://github.com/microsoft/BotFramework-WebChat/issues/4205 https://github.com/microsoft/BotFramework-WebChat/issues/4205

The issue is not tied to the React version or even React, at all.这个问题根本与 React 版本甚至 React 无关。

Instead, it is a bit of a race condition.相反,这是一种竞争条件。 The ConversationUpdate activity occurs when the DirectLine object in your Web Chat client establishes a connection with the Azure Bot Service. ConversationUpdate活动在您的 Web 聊天客户端中的 DirectLine object 与 Azure 机器人服务建立连接时发生。 The connector then issues the ConversationUpdate activity to the bot that, essentially, says 'Hey, I'm seeing some activity here.然后,连接器向机器人发出ConversationUpdate活动,本质上说,'嘿,我在这里看到一些活动。 This "member" just joined.'这个“会员”刚刚加入。 This activity is automatic and cannot be affected by either the bot or the client (ie Web Chat).此活动是自动的,不受机器人或客户端(即 Web 聊天)的影响。

Here's the problem: When you dispatch an event, message, etc., during DIRECT_LINE/CONNECT_FULFILLED , it can collide with the ConversationUpdate that also occurs.问题在于:当您在DIRECT_LINE/CONNECT_FULFILLED期间发送事件、消息等时,它可能会与同时发生的ConversationUpdate发生冲突。 The result is the ConversationUpdate wins and the Web Chat activity is 'lost'.结果是ConversationUpdate获胜,而 Web Chat 活动“丢失”。

Not to fear...there are two possible ways to overcome this.不要害怕......有两种可能的方法来克服这个问题。

One, wrap the Web Chat dispatch action in a setTimeout() with a delay of 100-200 ms.第一,将 Web 聊天调度操作包装在setTimeout()中,延迟 100-200 毫秒。

Two, dispatch the action on DIRECT_LINE/CONNECT instead of DIRECT_LINE/CONNECT_FULFILLED .第二,在DIRECT_LINE/CONNECT而不是DIRECT_LINE/CONNECT_FULFILLED上发送操作。

In my testing, either of these options solves the problem and should solve it for you, as well.在我的测试中,这些选项中的任何一个都可以解决问题,并且应该也能为您解决问题。

Hope of help!希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义 Lambda 授权方事件负载类型 - Custom Lambda Authorizer Event Payload type 如何从 EventBridge 发送事件到 Lambda - How to send event from EventBridge to Lambda AWS Direct Connect 如何运作? - How AWS Direct Connect works? 使用直接连接从 S3 存储桶传输大量数据 - Transferring large quantities of data from S3 bucket with direct connect 每当将某些数据推送到我的数据库时,如何将通知有效负载数据发送到 javascript 的主题? - Ho do I send notification payload data to a topic from javascript whenever some data is pushed to my database? 如何将触摸事件从一个应用程序发送到另一个应用程序 - how to send touch event from one app to another app 如何在没有 Confluent 的情况下使用 Kakfa Connect 从 Kafka 向 AWS S3 发送数据? - How to send data to AWS S3 from Kafka using Kakfa Connect without Confluent? 无法从入站 MMS“messageAdded”事件的负载中获取属性值 - Unable to get attributes value from inbound MMS "messageAdded" event's payload 如何从 WSO2 EI 6.1.1 中的 soap Payload 中检索元素 - How to retrieve elements from soap Payload in WSO2 EI 6.1.1 AmazonSimpleNotificationServiceClient - 如何使用包含没有“默认”值的自定义负载的 TargetArn 发布请求? - AmazonSimpleNotificationServiceClient - How to PublishRequest with TargetArn that contains Custom Payload without "default" value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM