当用户向我的机器人发送消息时,他会收到欢迎消息。但是当用户响应时,bot再次发送欢迎消息。我怎样才能解决这个问题?

时间:2017-01-24 08:33:59

标签: c# botframework

我正在使用Microsoft Bot Framework开发一个聊天机器人,我最近将框架3.0升级到3.5。升级之前它工作正常,但现在

当用户向我的机器人发送消息时,他会收到欢迎消息。但是当用户响应时,bot再次发送欢迎消息。 我怎样才能解决这个问题?这是代码。

private Activity HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // 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               
            }
            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
                logger.Debug("Activity Type " + message.Type);
                logger.Debug("Inside conversation update and activity Id is :-"+ message.Id);
                ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));
                Activity reply = message.CreateReply(ConstantsTable.WelcomeMessage);                                
                connector.Conversations.ReplyToActivityAsync(reply);
                message.Type = ActivityTypes.Message;                    
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // Handle add/remove from contact lists
                // Activity.From + Activity.Action represent what happened
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing tha the user is typing
                ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));
                Activity reply = message.CreateReply("You are typing");
                connector.Conversations.ReplyToActivityAsync(reply);
            }
            else if (message.Type == ActivityTypes.Ping)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
                Activity reply = message.CreateReply("Hello PING. Please reply");
                connector.Conversations.ReplyToActivityAsync(reply);
            }

            return message;
        }

但是在本地模拟器中它的工作正常,而发布时只会发生这种情况。 请帮忙。

2 个答案:

答案 0 :(得分:7)

我认为这可能与几天前推出的变化有关; Direct Line将发送比以往更多ConversationUpdate条消息。

检查announcementrelated issue(与您的类似,但在node.js中)。

将机器人添加到对话中时会发送第一个ConversationUpdate。 之后,当新用户加入对话时,会发送每个额外的ConversationUpdate

所以,我认为这里的解决方案是检查添加的成员(activity.MembersAdded

    else if (message.Type == ActivityTypes.ConversationUpdate)
    {
        if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))
        {
            // logic
        }
    }

答案 1 :(得分:0)

您需要在主对话框中使用基于意图的开关操作来处理该问题,还需要为相关意图添加对话框。

介绍和问候之间有区别。