如何从其他应用程序向 Microsoft 团队发布消息

我正在尝试在我的桌面应用程序中创建一个自定义方法(使用 C#),以向 Microsoft 团队发布消息。

但我还是不知道用什么样的工具或者服务来完成。

有可能实现吗?如果是,怎么办?

我在 Visual Studio 中找到了一个有关 MS Teams 的 NuGet 包,但没有成功。

就像在 Visual Studio 市场中一样。

但好像不符合我的要求。


慕村225694
浏览 122回答 2
2回答

翻阅古今

是的,可以从您的软件/桌面应用程序向 MS 团队发送通知。您可以使用适用于 MS 团队的 Microsoft Graph API 或 MS 团队传入挂钩功能。我发现使用传入钩子要容易得多。您可以按照 4 个步骤向您的频道发送消息通知:在您的团队中,右键单击您的频道。并搜索 Incoming Webhook.安装/添加Incoming Webhook(如果尚未添加)。Incoming Webhook通过提供 webhook 名称来配置。单击创建它将生成一个具有唯一 GUID 的链接,复制该链接 最后一步,在 PowerShell 中使用此命令行curl.exe -H "Content-Type:application/json" -d "{'text':'Servers x is started.'}" https://example.webhook.office.com/webhookb2/4dee1c26-036c-4bd2-af75-eb1abd901d18@3c69a296-d747-4ef3-9cc5-e94ee78db030/IncomingWebhook/87557542b42d8d3b04453c4a606f2b92/b852b3d0-84b6-4d98-a547-ae5f53452235注意:命令行中的 URL 包含一些伪造的 GUID 唯一 ID 引用,但您需要将其替换为从 Webhooks 获取的引用。您可以在命令行、PowerShell 或任何其他可以发出发布请求并将其合并到代码中的编程语言中调用此行。在这种情况下,为了回答这个问题,我在 C# 中实现了帖子要求:using (var httpClient = new HttpClient()){    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://example.webhook.office.com/webhookb2/4dee1c26-036c-4bd2-af75-eb1abd901d18@3c69a296-d747-4ef3-9cc5-e94ee78db030/IncomingWebhook/87557542b42d8d3b04453c4a606f2b92/b852b3d0-84b6-4d98-a547-ae5f53452235"))    {        request.Content = new StringContent("{'text':'Servers x is started.'}");        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");         var response = await httpClient.SendAsync(request);    }}现在,当我运行命令或 C# 代码时,我会在该频道中收到一条消息:如果您需要删除已添加的挂钩,请单击“已配置”,然后单击“配置”。并管理 webhook:并删除免责声明:我在我的个人博客上写了一篇涵盖该主题的文章。

largeQ

我们在图形 API 的帮助下实现了同样的目标注意:向通道发送消息目前处于测试阶段,但很快就会转移到图 V1 端点。使用 HTTP:POST https://graph.microsoft.com/beta/teams/{id}/channels/{id}/messagesContent-type: application/json{&nbsp; "body": {&nbsp; &nbsp; "content": "Hello World"&nbsp; }}使用 C#:GraphServiceClient graphClient = new GraphServiceClient( authProvider );var chatMessage = new ChatMessage{&nbsp; &nbsp; Subject = null,&nbsp; &nbsp; Body = new ItemBody&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ContentType = BodyType.Html,&nbsp; &nbsp; &nbsp; &nbsp; Content = "<attachment id=\"74d20c7f34aa4a7fb74e2b30004247c5\"></attachment>"&nbsp; &nbsp; },&nbsp; &nbsp; Attachments = new List<ChatMessageAttachment>()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; new ChatMessageAttachment&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Id = "74d20c7f34aa4a7fb74e2b30004247c5",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContentType = "application/vnd.microsoft.card.thumbnail",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContentUrl = null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content = "{\r\n&nbsp; \"title\": \"This is an example of posting a card\",\r\n&nbsp; \"subtitle\": \"<h3>This is the subtitle</h3>\",\r\n&nbsp; \"text\": \"Here is some body text. <br>\\r\\nAnd a <a href=\\\"http://microsoft.com/\\\">hyperlink</a>. <br>\\r\\nAnd below that is some buttons:\",\r\n&nbsp; \"buttons\": [\r\n&nbsp; &nbsp; {\r\n&nbsp; &nbsp; &nbsp; \"type\": \"messageBack\",\r\n&nbsp; &nbsp; &nbsp; \"title\": \"Login to FakeBot\",\r\n&nbsp; &nbsp; &nbsp; \"text\": \"login\",\r\n&nbsp; &nbsp; &nbsp; \"displayText\": \"login\",\r\n&nbsp; &nbsp; &nbsp; \"value\": \"login\"\r\n&nbsp; &nbsp; }\r\n&nbsp; ]\r\n}",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = null,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ThumbnailUrl = null&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }};await graphClient.Teams["{id}"].Channels["{id}"].Messages&nbsp; &nbsp; .Request()&nbsp; &nbsp; .AddAsync(chatMessage);您可能需要查看官方文档以获得更清晰的信息。这是下面的链接https://learn.microsoft.com/en-us/graph/api/channel-post-messages?view=graph-rest-beta&tabs=csharp就我而言,我使用 Angular 并调用端点。希望它能提供一些想法。

叮当猫咪

在连接器的帮助下可以在团队中发布消息。按照文档创建传入 Webhook 并使用消息卡发布消息。
打开App,查看更多内容
随时随地看视频慕课网APP