Forum Discussion
Mentioning multiple users with Bot Framework
- 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);
amatthews-yakchat - Could you please share a code snippet of how you are mentioning multiple users?
- amatthews-yakchatMay 03, 2022Copper 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;- Meghana-MSFTMay 06, 2022Former Employee
amatthews-yakchat - We checked this issue and we were able to get multiple mentions. The mentioned users get notifications in the Feed as well. Attaching the screenshots of the same.
Please note that we added mentions individually by hardcoding the values for now.
var mention2 = new Mention { Mentioned = new ChannelAccount() { AadObjectId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", Id = "AlexW@xxxxxxxx.OnMicrosoft.com", Name = "Alex", Role = "User" }, Text = $"<at>Alex</at>", }; var mention3 = new Mention { Mentioned = new ChannelAccount() { AadObjectId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", Id = "DebraB@xxxxxxxx.OnMicrosoft.com", Name = "Debra", Role = "User" }, Text = $"<at>Debra</at>", }; var replyActivity = MessageFactory.Text($"Hello {mention.Text} and {mention1.Text}, {mention2.Text}, {mention3.Text}."); replyActivity.Entities = new List<Entity>(); replyActivity.Entities.Add(mention); replyActivity.Entities.Add(mention1); replyActivity.Entities.Add(mention2); replyActivity.Entities.Add(mention3); await turnContext.SendActivityAsync(replyActivity, cancellationToken);Could you please confirm if you are mentioning the same issue that is working for us?
- amatthews-yakchatMay 06, 2022Copper ContributorYes, this is the functionality we are trying to implement and cannot get to function correctly. We are saving the ChannelAccount serialized as JSON against the user records in our database, so we can retrieve them to include in the notification messages.