user2454923
user2454923

Reputation: 49

I got the same message twice

I used this code to send a different message if a new user start conversation :

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

My problem is that when a user click in facebook : Get started this message comes twice.

Can you please help me ?

Upvotes: 1

Views: 557

Answers (2)

D4RKCIDE
D4RKCIDE

Reputation: 3426

I'm looking into the facebook Name part... You should be able to just plug in this code:

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

Upvotes: 0

stuartd
stuartd

Reputation: 73283

Facebook includes the conversation itself in the list of members:

enter image description here

So you need to change the if statement to this:

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

Upvotes: 1

Related Questions