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);
I've run another test of this and gotten the same result. Here is the debug data I have collected from the testing, the message should have 5 entities mentioned in the message posted, all 5 entities are added to the entity collection for the message, but I am getting the same odd behavior of only the last entity added to the last being mentioned and the mention overwriting the text for the first entity in the list when displayed in teams.
Entity 1
Mention.Text
<at>Andrew Matthews</at>
Mention.Mentioned
{"id":"29:1_2qC_nUipbzr4xvKNXUJfZ-fPXWbe7FhvjxFSy7eD6joNRLh1rtZyNpp1dYl-dw7t5kRYbKz_1GChCTtB1KMjg","name":"Andrew Matthews","aadObjectId":"ac5c8125-daaa-4764-a0a1-250e3b14faf1","role":null}
Entity 2
Mention.Text
<at>Jack Jenson</at>
Mention.Mentioned
{"id":"29:1znmswEmc8SskPlpBAqpWvv7uaXt4B046WXcDVzeFDZTD64L6bhQ4NZVK6veZJyGcRNOCRiAWSllSiBlo1V_dbw","name":"Jack Jenson","aadObjectId":"3d02d75f-c6e1-4f0d-8a2f-225c564958dd","role":null}
Entity 3
Mention.Text
<at>Dalia Farouk</at>
Mention.Mentioned
{"id":"29:1-Oe0wuCbfYw-rBj20L0ePQxWqF1bmhwT2IVF6PpogH7xafGMxN3YJWaMtHYJFt8Sf_9EJ6rlCdHht2E7RfnV7w","name":"Dalia Farouk","aadObjectId":"fcf3d09e-6f54-4f14-9476-cc11c8727a59","role":null}
Entity 4
Mention.Text
<at>U12 Test</at>
Mention.Mentioned
{"id":"29:15XaomK15dlPgsKUxoZ84DL9MUJcpnR6Z7q70-_7BHosfp78ZSrh6zPrecb9IL2L3rDqm_JRsPwdmsMoVokir1A","name":"U12 Test","aadObjectId":"0b302b93-d0f0-499f-8e3c-6072e3db6535","role":null}
Entity 5
Mention.Text
<at>YakChat Test 3</at>
Mention.Mentioned
{"id":"29:1AdwTVIKCmaV9_jkN9OF9B-kYZaHbDqcnTS7B4QeU6JkdJSUAN2Un-pnj7qhj0lR44M4bUTi3c5RSKZY0lE1MBA","name":"YakChat Test 3","aadObjectId":"b9301a39-8978-4dfa-944f-d63ac583b4d6","role":null}
Message sent out from the Bot
OutboundMessage.Text
__Message From *+19186014007*__ Test 1307 <at>Andrew Matthews</at>, <at>Dalia Farouk</at>, <at>Jack Jenson</at>, <at>U12 Test</at>, <at>YakChat Test 3</at>
Message as it appears in Teams
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);
- Meghana-MSFTMay 20, 2022MicrosoftIf the response is helpful, please click "**Mark as Best Response**" and like it. You can share your feedback via Microsoft Teams Developer Feedback link.
- amatthews-yakchatMay 19, 2022Copper Contributor
Meghana-MSFTUsing the Teams member details has solved this issue for us and we are now able to mention multiple users in a single message from our bot. I noticed that the methods in TeamsInfo return TeamsChannelAccounts instead of ChannelAccount which is returned from the method documented here
I'm not sure if I was referencing the incorrect documentation when trying to implement this, but if this is the correct documentation for this feature, could I suggest that it is updated to include this method of mentioning multiple users as the method in the documentation only appears to work if you are mentioning a single user.
- Meghana-MSFTMay 18, 2022Microsoft
amatthews-yakchat - You can get the Team member details using TeamsInfo.GetPagedMembersAsync. TeamsInfo gives the UPN values as well. Could you please try using these UPN values and check if that solves your issue.
- amatthews-yakchatMay 13, 2022Copper ContributorUsing hard coded values for the ChannelAccount and the user principal names works, so I think the issue is that the ChannelData we are currently saving in our database has something like 29:1AdwTVIKCmaV9_jkN9OF9B-kYZaHbDqcnTS7B4QeU6JkdJSUAN2Un-pnj7qhj0lR44M4bUTi3c5RSKZY0lE1MBA instead of the user principal name for the id field.
We are currently getting the ChannelAccount data from ITurnContext.Activity.From on the user's first incoming message to save it against the user record in our database. Do you have any suggestions on how we can change this so that instead of the weird id string we can instead save the user principal name in the id field before saving the serialized ChannelAccount data in our database? Is the fact multi mention doesn't work with this weird id a bug, or is it a known constraint?