[英]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.