Apr 22 2022 01:56 AM
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 here. 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?
Apr 26 2022 05:51 AM
Apr 29 2022 11:55 AM - edited Apr 29 2022 11:56 AM
@amatthews-yakchat - Could you please share a code snippet of how you are mentioning multiple users?
May 03 2022 01:07 AM
@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;
May 06 2022 04:33 AM
@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?
May 06 2022 06:25 AM
May 10 2022 10:11 PM
May 11 2022 05:36 AM
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
May 12 2022 06:55 AM
Solution@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);
May 13 2022 03:16 AM
May 18 2022 04:47 AM
@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.
May 19 2022 07:59 AM
@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.
May 20 2022 04:17 AM
May 12 2022 06:55 AM
Solution@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);