[英]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 事件发送一些自定义负载。
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.