我两次得到同样的消息

时间:2017-06-08 10:33:01

标签: c# facebook bots botframework

如果新用户开始对话,我使用此代码发送不同的消息:

IConversationUpdateActivity update = message;
            var client = new ConnectorClient(new Uri(message.ServiceUrl), new MicrosoftAppCredentials());
            if (update.MembersAdded != null && update.MembersAdded.Any())
            {
                foreach (var newMember in update.MembersAdded)
                {
                    if (newMember.Id != message.Recipient.Id)
                    {
                        var reply = message.CreateReply();
                        reply.Text = $"Welcome {newMember.Name}! You are a new member! If you want to see help menu , type : help";
                        client.Conversations.ReplyToActivityAsync(reply);
                    }
                }
            }

我的问题是当用户点击Facebook时:开始使用此消息两次。

你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

Facebook将对话本身包含在成员列表中:

enter image description here

因此您需要将if语句更改为:

if (newMember.Id != message.Recipient.Id && newMember.Id != message.Conversation.Id)
{ 
   // send welcome message

答案 1 :(得分:0)

我正在查看Facebook名称部分... 你应该能够插入这段代码:

            else if (message.Type == ActivityTypes.ConversationUpdate)
        {
            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 Friend I'm Botty McBotface");
                        connector.Conversations.ReplyToActivityAsync(reply);
                    }
                }
            }
        }