Microsoft Bot中相同的线程正在复制欢迎消息

时间:2017-06-13 14:48:17

标签: bots botframework

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

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

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

我尝试锁定打印消息并将其中一个线程置于休眠状态,但没有一个工作。我不知道为什么有两个线程在开始执行。

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

4 个答案:

答案 0 :(得分:2)

在网络频道和漫游器之间建立第一个对话时,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."));
    }

答案 1 :(得分:1)

您可能正在为加入聊天的机器人和用户返回消息。如果没有在根对话框中看到对话中的代码更新部分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);
            }
        }
    }
}

答案 2 :(得分:1)

或者您可以尝试此代码

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



                }

答案 3 :(得分:0)

我认为在

中定义的简单方法(使用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);
            }