How do I post a message to Microsoft team from other application

£可爱£侵袭症+ 提交于 2021-02-08 06:53:47

问题


I am trying to make a custom method in my desktop application (using C#), to post a message into a Microsoft team. but I still don't know what kind of tool or services to get it done. is it possible to achieve it? if yes, how?

I found a nugget regarding MS-Teams in Visual Studio. but it won't work. as in Visual studio market place. what I found is https://marketplace.visualstudio.com/items?itemName=ms-vsts.vss-services-teams

But it seems like doesn't meet my requirement.


回答1:


You can follow 3 steps to send message notifications to your channels:

  1. In your teams, right click on your channel. And send for Incoming webhook.
  2. Install/configure webhook, by provide a webhook name. Click on Create
  • It will create you a link, copy the link
  1. Last step, use this command line in 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

Note: the URL in the command line contains some faked guid numbers, but you need to replace it with the one you get from webhooks.

You can either call this line in power shell or incorporated in c#.

Now when I run the command I get a message in that channel:


In case you need to remove hook that you have added, click on Configured then Configure. And Manage the the webhook: And remove




回答2:


We have achieved the same with the help of graph API

NB: Sending message to channel is currently beta but will soon move to graph V1 endpoint.

using HTTP:

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

{
  "body": {
    "content": "Hello World"
  }
}

using 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);

You may need to look at the official documentation for more clarity. Here is the link below

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

In my case I was using Angular and calling the endpoints.

Hope it gives some idea.




回答3:


Posting messages in teams can be acheived with the help of Connectors. Follow the doc to create incoming webhook and post the message using message card.



来源:https://stackoverflow.com/questions/57050321/how-do-i-post-a-message-to-microsoft-team-from-other-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!