Forum Discussion
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 using the C# Bot Framework SDK Version 4 and .NET Core 3.1 that is installed via Teams. I am trying to implement a requirement where the bot can @mention members of the Team in the messages it sends, following the documentation found https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/channel-and-group-conversations?tabs=dotnet#add-mentions-to-your-messages. This is working fine for a single user, however when I try to @mention more than one user in a single message I am experiencing some weird behavior. The message only mentions the last user added to the Entities object of the MessageActivity, even though all the mention text is appended to the message and is formatted as though it should be a mention. Also the actual mention seems to overwrite the first user name that was added to the Entities list, even though the mention text it should be using still appears at the end of the message, as shown in the screenshot below.
I have tried searching for a solution for this problem but I cannot find anything, is this a current limitation of the Bot Framework SDK or am I implementing this feature incorrectly?
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);
12 Replies
- Meghana-MSFTFormer EmployeeWe are looking into this issue, we will get back to you.
- Meghana-MSFTFormer Employee
amatthews-yakchat - Could you please share a code snippet of how you are mentioning multiple users?
- amatthews-yakchatCopper Contributor
Meghana-MSFT- Here is the code snippet for how we are attempting to implement this feature. This method appears to work for a single user, but not for multiple users.
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--; FormattedMessageWithMention = String.Format(BotResponse.InboundMessage_MentionFormat, FormattedMessage, MentionStringBuilder.ToString(), Environment.NewLine); OutboundMessage.Text = FormattedMessageWithMention;