Forum Discussion

amatthews-yakchat's avatar
amatthews-yakchat
Copper Contributor
Apr 22, 2022

Mentioning multiple users with Bot Framework

I am having a problem with mentions using Bot Framework in Teams and cannot find any information about how to fix it or any one who has had similar issues.   We have a bot that has been developed u...
  • Meghana-MSFT's avatar
    Meghana-MSFT
    May 12, 2022

    amatthews-yakchat - We tried this with your code as well and its working, could you also please try by using user principal names once. We hardcoded the values into SubscriptionMentions object. Also please note that we MentionStringBuilder.ToString() directly at the end.

     

    Could you please give a try like this?

                var SubscriptionMentions = new string[]
                {
                    @"{
                        Id: 'admin@M365x654992.onmicrosoft.com',
                        Name: 'Admin'
                    }",
                    @"{
                        Id: 'adelev@M365x654992.onmicrosoft.com',
                        Name: 'Adele'
                    }",
                    @"{
                        Id: 'AlexW@M365x654992.OnMicrosoft.com',
                        Name: 'Alex'
                    }",
                };
                IMessageActivity OutboundMessage = Activity.CreateMessageActivity();
                OutboundMessage.Entities = new List<Entity>();
                StringBuilder MentionStringBuilder = new StringBuilder(2048);
                foreach (String MentionData in SubscriptionMentions)
                {
                    ChannelAccount MentionAccount = JsonConvert.DeserializeObject<ChannelAccount>(MentionData);
                    String MentionName = XmlConvert.EncodeName(MentionAccount.Name);
                    if (MentionName.Contains("_x0020_"))
                    {
                        MentionName = MentionName.Replace("_x0020_", " ");
                    }
                    Mention UserMention = new Mention()
                    {
                        Mentioned = MentionAccount,
                        Text = $"<at>{MentionName}</at>"
                    };
                    OutboundMessage.Entities.Add(UserMention);
                    MentionStringBuilder.Append($" <at>{MentionName}</at>,");
                }
                
                MentionStringBuilder.Length--;
               
                OutboundMessage.Text = MentionStringBuilder.ToString();
                await turnContext.SendActivityAsync(OutboundMessage, cancellationToken);

     

Resources