【问题标题】:How do I post a message to Microsoft team from other application如何从其他应用程序向 Microsoft 团队发布消息
【发布时间】:2022-01-28 01:11:51
【问题描述】:

我正在尝试在我的桌面应用程序中创建一个自定义方法(使用 C#),以向 Microsoft 团队发布消息。 但我仍然不知道用什么样的工具或服务来完成它。 有可能实现吗?如果是,怎么做?

我在 Visual Studio 中找到了有关 MS-Teams 的块。但它不会工作。 就像在 Visual Studio 市场一样。我发现的是 https://marketplace.visualstudio.com/items?itemName=ms-vsts.vss-services-teams

但是好像不符合我的要求。

【问题讨论】:

    标签: c# microsoft-teams


    【解决方案1】:

    您可以按照 4 个步骤向您的频道发送消息通知:

    1. 在您的团队中,右键单击您的频道。并搜索Incoming Webhook
    2. 安装/添加Incoming Webhook(如果尚未添加)。
    3. 通过提供 webhook 名称来配置 Incoming Webhook。点击创建
    • 它将为您生成一个带有唯一指南的链接,复制该链接
    1. 最后一步,在 Power shell 中使用此命令行
    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 号, 但是您需要将其替换为从 webhook 获得的那个。

    您可以在 power shell 中调用此行,也可以将其合并到 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: 并删除

    【讨论】:

      【解决方案2】:

      我们在图形 API 的帮助下实现了同样的目标

      注意:向频道发送消息目前处于测试阶段,但很快将移至图 V1 端点。

      使用 HTTP:

      POST https://graph.microsoft.com/beta/teams/{id}/channels/{id}/messages
      Content-type: application/json
      
      {
        "body": {
          "content": "Hello World"
        }
      }
      

      使用 C#:

      GraphServiceClient graphClient = new GraphServiceClient( authProvider );
      
      var chatMessage = new ChatMessage
      {
          Subject = null,
          Body = new ItemBody
          {
              ContentType = BodyType.Html,
              Content = "<attachment id=\"74d20c7f34aa4a7fb74e2b30004247c5\"></attachment>"
          },
          Attachments = new List<ChatMessageAttachment>()
          {
              new ChatMessageAttachment
              {
                  Id = "74d20c7f34aa4a7fb74e2b30004247c5",
                  ContentType = "application/vnd.microsoft.card.thumbnail",
                  ContentUrl = null,
                  Content = "{\r\n  \"title\": \"This is an example of posting a card\",\r\n  \"subtitle\": \"<h3>This is the subtitle</h3>\",\r\n  \"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  \"buttons\": [\r\n    {\r\n      \"type\": \"messageBack\",\r\n      \"title\": \"Login to FakeBot\",\r\n      \"text\": \"login\",\r\n      \"displayText\": \"login\",\r\n      \"value\": \"login\"\r\n    }\r\n  ]\r\n}",
                  Name = null,
                  ThumbnailUrl = null
              }
          }
      };
      
      await graphClient.Teams["{id}"].Channels["{id}"].Messages
          .Request()
          .AddAsync(chatMessage);
      

      您可能需要查看官方文档以获得更清晰的信息。这是下面的链接

      https://docs.microsoft.com/en-us/graph/api/channel-post-messages?view=graph-rest-beta&tabs=csharp

      就我而言,我使用的是 Angular 并调用端点。

      希望它能提供一些想法。

      【讨论】:

      • 如何为 GraphServiceClient 创建authProvider
      • 每次我尝试发起 .CreateClientApplication() 时都会出现红色的摆动线。我不知道怎么了。
      • 您是否使用 oAuthV2 连接到 Graph 服务?不知道你在哪里打电话给createClientApplication()
      • var endpoint = "https://login.microsoftonline.com/organizations/oauth2/v2.0/token"; 创建graphservice.cs文件
      • 也许这绝对有帮助。 https://github.com/microsoftgraph/csharp-teams-sample-graph 尝试克隆它并调整您的逻辑。谢谢
      【解决方案3】:

      Connectors 的帮助下可以在团队中发布消息。 按照文档创建传入 webhook 并使用消息卡发布消息。

      【讨论】:

      • 你能告诉我逻辑如何吗?我的意思是,当我单击桌面应用程序上的按钮时,它会向我的 MS.Team 组发送一条消息
      • 我已经尝试在这篇文章中提问。但它似乎只适用于 azure devOps。
      • 我已经尝试了 SQL SERVER 触发器在 MS.Flow 上的另一种方法。所以当我在我的应用程序中插入数据时,它会向团队发布消息。但问题是触发器在premise data 上不起作用。
      猜你喜欢
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多