Microsoft Graph - Teams API 可在 Graph Explorer 上运行,但不能通过代码运行

Microsoft Graph - Teams API works on Graph Explorer but not via code

我需要在 Teams 频道上自动 post 一条消息并提及该频道。不幸的是,通过 MS Flow,提及整个频道的选项不可用,但似乎通过 Graph API 的测试版,我可以提及整个频道。

我首先尝试通过 Graph Explorer,将 VERB 更改为 POST 并将 URL 设置为 <https://graph.microsoft.com/beta/teams/{group ID}/channels/{channel id}/messages>

此外添加了以下请求正文

{
    "subject": "@Mention in Teams channel post!",
    "body": {
        "content": "Hello <at id ='0'>{channel name}</at>, Test message on the channel with at mention.",
        "contentType": "html"
    },
    "mentions": [
        {
            "id": 0,
            "mentionText": "{channel name}",
            "mentioned": {
                "conversation": {
                    "id": "{channel id}",
                    "displayName": "{channel name}",
                    "conversationIdentityType@odata.type": "#Microsoft.Teams.GraphSvc.conversationIdentityType",
                    "conversationIdentityType": "channel"
                }
            }
        }
    ]
}

当按下 运行 查询 时,消息被成功 posted 并且频道被提及。然后,我从图形资源管理器中检索了 C# 代码的代码片段,生成了以下代码

GraphServiceClient graphClient = new GraphServiceClient(authProvider);

var chatMessage = new ChatMessage
{
    Subject = "@Mention in Teams channel post!",
    Body = new ItemBody
    {
        Content = "Hello <at id ='0'>{channel name}</at>, Test message on the channel with at mention.",
        ContentType = BodyType.Html
    },
    Mentions = new List<ChatMessageMention>()
    {
        new ChatMessageMention
        {
            Id = 0,
            MentionText = "{channel name}",
            Mentioned = new IdentitySet
            {
                AdditionalData = new Dictionary<string, object>()
                {
                    {"conversation", "{\"id\":\"{channel id}\",\"displayName\":\"{channel name}\",\"conversationIdentityType@odata.type\":\"#Microsoft.Teams.GraphSvc.conversationIdentityType\",\"conversationIdentityType\":\"channel\"}"}
                }
            }
        }
    }
};

await graphClient.Teams["{group id}"].Channels["{channel id}"].Messages
    .Request()
    .AddAsync(chatMessage);

但是执行代码时,出现如下错误:

ServiceException: Code: BadRequest Message: Invalid request body was sent.

删除 提及 或更改提及以使用用户成功。另外,请注意,我已经尝试同时使用 Microsoft.Graph 和 Microsoft.Graph.Beta

我对此进行了长时间的研究,发现由于以这种方式编写的代码,它在图形服务器上发生了反序列化问题。主要问题在于提到的 属性 中的对话。 Graph 服务器无法理解序列化的内容,因此在发送请求之前尝试对其进行反序列化,如下所示。

Identity A = JsonConvert.DeserializeObject<Identity>("{\"id\":\"{channel id}\",\"displayName\":\"{channel name}\",\"conversationIdentityType@odata.type\":\"#Microsoft.Teams.GraphSvc.conversationIdentityType\",\"conversationIdentityType\":\"channel\"}");
            var chatMessage = new ChatMessage
            {
                Subject = "@Mention in Teams channel post!",
                Body = new ItemBody
                {
                    Content = "Hello <at id ='0'>General</at>, Test message on the channel with at mention.",
                    ContentType = BodyType.Html
                },
                Mentions = new List<ChatMessageMention>()
                {
                    new ChatMessageMention
                    {
                        Id = 0,
                        MentionText = "General",
                        Mentioned = new IdentitySet
                        {
                            AdditionalData = new Dictionary<string, object>()
                            {
                                {"conversation", A}
                            }
                        }
                    }
                }
            };
            
            try
            {
                await graphClient.Teams["d3b31e36-d63d-4bbe-9478-b4cc7cb17a3d"].Channels["19:342b9f379eb340048b16d9859d9e3712@thread.tacv2"].Messages
                .Request()
                .AddAsync(chatMessage);

            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }

它会起作用。