Microsoft Bot中的相同线程正在复制欢迎消息
我有一条欢迎消息配置为在我的机器人第一次启动时出现在MessagesController中.
I have a welcoming message configured to appear in MessagesController the first time my bot is started.
private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.ConversationUpdate)
{
// returning a msg here to the Post method in MessagesController.
}
}
当我调试时,似乎在启动时,有两个线程正在运行bot,并且两个线程都在Post方法中执行,因此,两个线程都在调用 HandleSystemMessage .这对我来说是个问题,因为有两个线程执行该方法,我的欢迎消息在屏幕上打印了两次.
When I debug it would seem that at start time, TWO threads are working the bot and both are executing in the Post method, and consequently both are calling HandleSystemMessage. This is a problem for me, because with two threads executing the method, my welcoming message is being printed twice on screen.
我尝试锁定打印消息,并让其中一个线程进入睡眠状态,但是没有一个起作用.我不知道为什么要执行两个线程.
I tried locking the print msg and putting one of the threads to sleep, but none have worked. I don't know why there are two threads executing to begin with.
有必要吗?他们都运行相同的执行.我可以杀死其中一个吗?还是有其他方法为机器人打印欢迎消息?
Are they necessary? they are both running an identical execution. Could I kill one of them? Or is there a different way to print a welcome message for the bot?
您可能会返回一条消息,告知机器人加入聊天以及用户.很难在根对话框的if-else语句中看不到对话更新中的代码就知道了.您可以使用以下代码仅发布一条消息
it's possible that you are returning a message for the bot joining chat and the user as well. It's hard to tell without seeing the code in your conversation update part of the if-else statement in root dialog. You can use the following code to post just a single message
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
if (iConversationUpdated != null)
{
ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));
foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
{
// if the bot is added, then
if (member.Id == iConversationUpdated.Recipient.Id)
{
var reply = ((Activity) iConversationUpdated).CreateReply(
$"Hi! I'm Botty McBotface and this is a welcome message");
await connector.Conversations.ReplyToActivityAsync(reply);
}
}
}
}