【问题标题】:Identical Threads in Microsoft Bot are Duplicating Welcoming MessageMicrosoft Bot 中的相同线程正在复制欢迎消息
【发布时间】:2020-06-12 22:47:10
【问题描述】:

我有一条欢迎消息配置为在我的机器人第一次启动时出现在 MessagesController 中。

    private Activity HandleSystemMessage(Activity message)
    {
        if (message.Type == ActivityTypes.ConversationUpdate)
        {
            // returning a msg here to the Post method in MessagesController.
        }
    }

当我调试时,似乎在启动时有两个线程在运行机器人,并且都在 Post 方法中执行,因此都在调用 HandleSystemMessage。这对我来说是个问题,因为有两个线程执行该方法,我的欢迎信息会在屏幕上打印两次。

我尝试锁定打印消息并使其中一个线程进入睡眠状态,但没有一个起作用。我不知道为什么要开始执行两个线程。

它们有必要吗?他们都在运行相同的执行。我可以杀死其中一个吗?或者是否有其他方法可以为机器人打印欢迎消息?

【问题讨论】:

标签: bots botframework


【解决方案1】:

当网络频道和机器人之间建立第一次对话时,ConversationUpdate 活动会引发两次。一个来自用户,另一个来自频道,因此我们会收到两次欢迎消息。 我们需要确保为用户提出的活动发送欢迎消息。

这段代码帮助我避免了这个问题。

 private async Task GreetUserAsync(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate && 
            turnContext.Activity.MembersAdded[0].Id.Contains(turnContext.Activity.Recipient.Id))
            await turnContext.SendActivityAsync(MessageFactory.Text("Hi, how can I help you."));
    }

【讨论】:

    【解决方案2】:

    您可能会返回一条消息,让聊天机器人和用户也加入聊天。如果没有在根对话框中看到 if-else 语句的对话更新部分中的代码,很难判断。您可以使用以下代码仅发布一条消息

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

    【讨论】:

    • 解决了这个问题。非常感谢@Jason。我不太确定我理解如何。你能解释一下吗? foreach 循环到底在做什么?
    • 如果添加了多个成员,它会确保消息发送给正确的人。您不必为每个都使用 a,这就是它的编写方式。你可以通过多种方式完成同样的事情,祝你好运。
    【解决方案3】:

    或者你可以试试这个代码

     else if (message.Type == ActivityTypes.ConversationUpdate)
                    {
    
                        ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
                        var reply = message.CreateReply(BotMessages.WelcomeMessage);
                        connector.Conversations.SendToConversation(reply);
                        reply = message.CreateReply();
                        reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                        reply.Attachments = HelpTextDialog.GetMessageCard();
                        connector.Conversations.SendToConversation(reply);
    
    
    
                    }
    

    【讨论】:

      【解决方案4】:

      我认为在

      中定义的简单方法(使用 Linq)

      ActivityTypes.ConversationUpdate 是(即使它在 Azure Bot 服务中也可以使用),

      ,

                  var client = new ConnectorClient(new Uri(message.ServiceUrl));
                  **if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))**
                  {
                      var reply = message.CreateReply();
                      reply.Text = $"Welcome User! May I know your First and Last name?";
                      reply.Text += $"{Environment.NewLine}How can i help you?";
                      client.Conversations.ReplyToActivityAsync(reply);
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-20
        相关资源
        最近更新 更多