Graph API - 使用@mention 发送频道消息的 BadRequest

问题描述

我们有一些代码在过去 10 个月内一直在工作(自开发以来),今天下午刚刚停止工作。这是一个 WebAPI 代码,用于发送提及机器人和用户的通道消息,现在返回“错误的请求。发送了无效的请求正文。”

如果未提供“提及”属性,则调用会起作用,并且消息将在没有@提及的情况下发送。所以,我想知道这个 API 是否有重大变化,现在期待“提及”属性采用不同的格式。

按照 the Microsoft Graph documentation 中的示例代码进行重现非常简单。

在这里发帖是希望一些开发人员发现一些明显的东西,或者知道使用 API 的替代方法,它可能会停止抱怨,因为 Microsoft 需要很长时间才能回复

这是我们可以引导我发现问题的代码

private async Task SendMentionToTheBotAsync(GraphServiceClient onBehalfOfClient,string userName,string teamId,string channelId)
{
        var supportAgentUser = await onBehalfOfClient.Me.Request().GetAsync();

        var chatMessage = new ChatMessage
        {
            Body = new ItemBody
            {
                ContentType = BodyType.Html,Content = $"<at id=\"0\">{Configuration["BotName"]}</at>: This is the start of the conversation between {userName} and <at id=\"1\">{supportAgentUser.displayName}</at>."
            },Mentions = new List<ChatMessageMention>
              {
                new ChatMessageMention
                {
                    Id = 0,MentionText = Configuration["BotName"],Mentioned = new IdentitySet
                    {
                        Application = new Identity
                        {
                            displayName = Configuration["BotName"],Id = Configuration["BotAppId"],AdditionalData = new Dictionary<string,object>
                            {
                                {
                                    "applicationIdentityType","bot"
                                }
                            }
                        }
                    }
                },new ChatMessageMention
                {
                    Id = 1,MentionText = supportAgentUser.displayName,Mentioned = new IdentitySet
                    {
                        User = new Identity
                        {
                            displayName = supportAgentUser.displayName,Id = supportAgentUser.Id,object>
                            {
                                {
                                    "userIdentityType","aadUser"
                                }
                            }
                        }
                    }
                }
            }
        };

        await onBehalfOfClient.Teams[teamId].Channels[channelId].Messages
                              .Request()
                              .AddAsync(chatMessage);
    }

解决方法

微软支持回复:

“感谢您联系 Microsoft 支持。

我了解此问题与向 Teams 发布消息有关。根据屏幕截图,您似乎在使用提及频道。您可能在请求中使用了密钥“conversationIdentityType@odata.type”。

您能否尝试从请求正文中删除“conversationIdentityType@odata.type”键,然后重试。它应该工作。这是因为正在亚洲地区进行部署。一旦 100% 推出,此密钥将不会出现在请求中。”

删除了密钥,它对我有用。

,

保罗,

不幸的是,我不是程序员。我在 Microsoft 365 Power Automate 工作流中使用 Graph 调用。我有一个应用程序,用于获取授权承载令牌,然后使用图形 HTTP 操作发布到团队消息。

这里是 HTTP 的语法(如果你不熟悉 Flow,紫色项目是变量)

click to view image of Power Automate workflow HTTP action