简体   繁体   English

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

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

I have a requirement to automatically post a message on a Teams channel and mention the channel.我需要在 Teams 频道上自动发布消息并提及该频道。 Unfortunately via MS Flow, the option to mention an entire channel is not available, however it seems that through the beta version of the Graph API, I can mention an entire channel.不幸的是,通过 MS Flow,提及整个频道的选项不可用,但似乎通过 Graph API 的 beta 版本,我可以提及整个频道。

I have first tried via Graph Explorer, changing the VERB to POST and setting the URL to <https://graph.microsoft.com/beta/teams/{group ID}/channels/{channel id}/messages>我首先尝试通过 Graph Explorer,将 VERB 更改为 POST 并将 URL 设置为<https://graph.microsoft.com/beta/teams/{group ID}/channels/{channel id}/messages>

Moreover added the below request body此外添加了以下请求正文

{
    "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"
                }
            }
        }
    ]
}

When the Run Query is pressed, the message is successfully posted and the channel is mentioned.按下运行查询时,消息已成功发布并提及频道。 I've then retrieved the code snippet from the graph explorer for C# code, which resulted in the below code然后,我从图形资源管理器中检索了 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);

However when executing the code, the error below is displayed:但是在执行代码时,会显示以下错误:

ServiceException: Code: BadRequest Message: Invalid request body was sent. ServiceException:代码:BadRequest 消息:发送了无效的请求正文。

Removing the Mentions or changing the mentions to make use of the User works successfully.删除提及或更改提及以成功使用用户。 Also, kindly note that I've tried using both the Microsoft.Graph and Microsoft.Graph.Beta另外,请注意我已经尝试使用 Microsoft.Graph 和 Microsoft.Graph.Beta

I had done a long research on this and found that its a Deserialization issue happening on the Graph server because of the code written this way.我对此进行了长期研究,发现由于以这种方式编写的代码,它在 Graph 服务器上发生了反序列化问题。 The main problem is with the conversation in the Mentioned property.主要问题在于提及属性中的对话。 The Graph server was unable to understand the serialized content so try deserializing it before sending the request as shown below. 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);
            }
        }

It will work.它会起作用的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft Graph API 查询可在 Explorer 上运行,但不能在 Microsoft Graph .NET 客户端库中运行 - Microsoft Graph API Query works on Explorer but not in Microsoft Graph .NET Client Library 如何通过 Microsoft Graph API/SDK 向 Microsoft 团队发送自适应卡片? - How to send a Adaptive card to Microsoft teams via Microsoft Graph API/SDK? 如何通过 microsoft graph API C# 向 Microsoft Teams 中的 1:1 聊天或群聊发送消息 - How to send message to 1:1 chat or to group chat in Microsoft Teams via microsoft graph API C# 在 C# 中通过 Microsoft Graph API 在 Microsoft 团队通用频道中发布消息时出现禁止访问错误 - Getting Forbidden error while posting the message in Microsoft teams general channel via Microsoft Graph API in C# 阅读 Microsoft Teams 频道消息(Microsoft graph API、C#) - Read Microsoft Teams channel messages (Microsoft graph API, C#) 使用 Graph API 和委派权限将成员添加到 Microsoft Teams - Add a member to Microsoft Teams using Graph API and delegated permissions 通过Microsoft Graph API创建主类别 - Create Master Category Via Microsoft Graph API Microsoft Graph GetPhoto无法在Microsoft Teams bot中运行 - Microsoft Graph GetPhoto not working in Microsoft Teams bot 如何通过 Graph 创建 1:1 团队聊天 | 图形API | C# - How to create a 1:1 Teams-Chat via Graph | Graph API | C# Office 365图获得Microsoft Teams消息 - Office 365 Graph getting Microsoft Teams Messages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM