簡體   English   中英

Microsoft Graph - Teams API 在 Graph Explorer 上工作,但不能通過代碼

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

我需要在 Teams 頻道上自動發布消息並提及該頻道。 不幸的是,通過 MS Flow,提及整個頻道的選項不可用,但似乎通過 Graph API 的 beta 版本,我可以提及整個頻道。

我首先嘗試通過 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"
                }
            }
        }
    ]
}

按下運行查詢時,消息已成功發布並提及頻道。 然后,我從圖形資源管理器中檢索了 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:代碼:BadRequest 消息:發送了無效的請求正文。

刪除提及或更改提及以成功使用用戶。 另外,請注意我已經嘗試使用 Microsoft.Graph 和 Microsoft.Graph.Beta

我對此進行了長期研究,發現由於以這種方式編寫的代碼,它在 Graph 服務器上發生了反序列化問題。 主要問題在於提及屬性中的對話。 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);
            }
        }

它會起作用的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM