0

我们正在开发一个机器人,它可以在群聊中主动向人们发送消息。机器人提及在桌面/网络聊天视图上显示为空白。有趣的是,在移动设备和左侧的通知栏中,全文确实显示正确。

这个问题可能适用于其他聊天,但我没有测试过。

我通过构建提及对象使用与以下 Microsoft 指南类似的代码: https ://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/channel-and-group-conversations ?tabs=dotnet#add-mentions-to-your-messages

是的,我已经尝试在示例中的名称上使用 XMLConvert,但是,这并没有什么不同,事实上,它将 XML 逐字放入机器人发送的消息中。

我还在这里提交了错误报告,因为我怀疑这是 Teams 本身的错误(尽管我找不到任何其他提及此代码或其他类似示例代码): https ://microsoftteams.uservoice.com/forums/ 555103-public/suggestions/43922577-bot-mentions-to-meetings-groups-are-blank-on-deskt

相关C#代码:

...
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)
    {}
}
...

桌面团队聊天:

桌面团队聊天

活动显示名称OK:

活动显示名称OK

移动应用看起来不错:

移动应用看起来不错

为保护隐私而编辑的图像

4

1 回答 1

1

尝试在 ChannelAccount() 中也传递“用户名”,如下所示:

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

我尝试了上面的代码,它对我有用。

于 2021-08-11T09:42:30.280 回答