我需要自动在团队频道上发布消息并提及该频道。不幸的是,通过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}",
"[email protected]": "#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}\",\"[email protected]\":\"#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}\",\"[email protected]\":\"#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:[email protected]"].Messages
.Request()
.AddAsync(chatMessage);
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
会的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句