【发布时间】:2020-12-11 09:02:31
【问题描述】:
我需要在 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# 代码的代码 sn-p,这导致了以下代码
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
【问题讨论】:
-
嗨,马克,如果发布的答案解决了您的问题,请点赞或单击复选标记将其标记为答案。这样做可以帮助其他人找到他们问题的答案。见meta.stackexchange.com/questions/5234/…
-
您在应用中使用的是哪个 Microsoft 图形版本 dll?
-
@Trinetra-MSFT 我正在使用 Microsoft.Graph.Beta 版本 0.35.0-preview。但是 Shiva - 下面的 MSFT Identity 答案运行良好
标签: c# microsoft-graph-api microsoft-teams