当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《当用户向我的机器人发送消息时,他会收到欢迎消息.但是当用户响应时,bot再次发送欢迎消息.我怎样才能解决这个问题?》经验,为你挑选了1个好方法。

我正在使用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;
        }

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



1> Ezequiel Jad..:

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

检查公告和相关问题(与您的相似,但在中node.js).

第一个ConversationUpdate是在将bot添加到对话时发送的.之后,ConversationUpdate当新用户加入对话时,会发送每个附加内容.

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

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

推荐阅读
喜生-Da
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有