SOLVED

Mentioning multiple users with Bot Framework

Copper Contributor

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.

 

amatthewsyakchat_0-1650617321773.png

 

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?

12 Replies
We are looking into this issue, we will get back to you.

@amatthews-yakchat - Could you please share a code snippet of how you are mentioning multiple users?

@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;

 

@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.

MeghanaMSFT_0-1651835576668.pngMeghanaMSFT_1-1651836130945.pngMeghanaMSFT_2-1651836211346.png

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?

Yes, 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.
Could you please debug once again and check if you are getting correct values into the mention objects as we do not have a repro for this.

@Meghana-MSFT 

 

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

 

amatthewsyakchat_1-1652272415632.png

best response confirmed by amatthews-yakchat (Copper Contributor)
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);

 

Using 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?

@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. 

 

https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/get-teams-context?tabs=dotnet#f...

 

@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.

If the response is helpful, please click "**Mark as Best Response**" and like it. You can share your feedback via Microsoft Teams Developer Feedback link.
1 best response

Accepted Solutions
best response confirmed by amatthews-yakchat (Copper Contributor)
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);

 

View solution in original post