Search code examples
c#botframeworkchatbotmicrosoft-teamsproactive

Bot mentions to meetings groups are blank on Desktop/Web chat view


We're developing a bot that proactively messages people in a group chat. Bot mentions are showing blank on Desktop/Web chat view. Interestingly, on mobile and in the notification bar on the left, the full text does show correctly.

This issue may apply to other chats, but I have not tested.

I'm using similar code to the following Microsoft guide by constructing the mention object: https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/channel-and-group-conversations?tabs=dotnet#add-mentions-to-your-messages

Yes, I have tried using XMLConvert on the name as in the example, however, that does not make a difference, in fact, it puts the XML verbatim into the message sent by the bot.

I've also submitted a bug report here, as I suspect it's a bug in Teams itself (although, I could not find any other mentions of this or other similar example code): https://microsoftteams.uservoice.com/forums/555103-public/suggestions/43922577-bot-mentions-to-meetings-groups-are-blank-on-deskt

Relevant C# code:

...
using (ConnectorClient _client = new ConnectorClient(new Uri(groupChat.ServiceUrl), GetMicrosoftAppCredentials(), new HttpClient()))
{
    var theMessage = Activity.CreateMessageActivity();
    theMessage.Text = messageDto.Message;

    // Process the message text for <at>mentions</at>
    var textMentions = System.Text.RegularExpressions.Regex.Matches(theMessage.Text, "<at>(.*?)</at>");

    var mentionObjects = new List<Entity>(); // For storing the mentions

    foreach (var textMention in textMentions)
    {
        // The mentioned person should be between the tags
        var theName = textMention.ToString().Split(new string[] { "<at>", "</at>" }, StringSplitOptions.RemoveEmptyEntries)[0];
        if (!String.IsNullOrEmpty(theName))
        {
            // Find the teamsUser based on their name
            var teamsUser = _botContext.Users.FirstOrDefault(u => u.Name.Equals(theName));
            if (teamsUser != null)
            {
                var mention = new Mention
                {
                    Mentioned = new ChannelAccount(teamsUser.TeamsUserId),
                    Text = textMention.ToString()
                };

                mentionObjects.Add(mention);
            }
        }
    }

    theMessage.Entities = mentionObjects;

    try
    {
        var response = await _client.Conversations.SendToConversationAsync(groupChat.GroupChatId, (Activity)theMessage);
        return Ok(response.Id);
    }
    catch (Exception e)
    {}
}
...

Desktop Teams Chat:

Desktop Teams Chat

Activity shows name OK:

Activity shows name OK

Mobile app looks OK:

Mobile app looks OK

Images edited for privacy


Solution

  • Try passing "User Name" also in ChannelAccount() like below:

    var mention = new Mention
                   {
                            Mentioned = new ChannelAccount(
                                turnContext.Activity.From.Id,
                                **turnContext.Activity.From.Name,**
                                role: null, 
                                aadObjectId: null),
                            Text = textMention.ToString()
                        };
    

    I tried above code and it's working for me.