Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to make a custom method in my desktop application (using C#), to post a message to a Microsoft team.

But I still don't know what kind of tool or services to get done.

Is it possible to achieve it? if yes, how?

I found a NuGet package regarding MS Teams in Visual Studio but was without luck.

As in the Visual studio marketplace. What I found is https://marketplace.visualstudio.com/items?itemName=ms-vsts.vss-services-teams

But it seems like doesn't meet my requirement.

like image 755
orlandeu man Avatar asked Sep 13 '25 15:09

orlandeu man


1 Answers

Since I answered this question a lot of things happened. It looks like connectors are going to be retired as this link mentions, https://devblogs.microsoft.com/microsoft365dev/retirement-of-office-365-connectors-within-microsoft-teams/

One way is using the new feature for get email address (each channel has email address) that can be used to recevied messages via email.

enter image description here

enter image description here

and use any email client to send email message.

enter image description here

The other options is using Team Developer portal to create a API key, SSO or OAuth (https://dev.teams.microsoft.com/tools) as this link describe, https://learn.microsoft.com/en-us/microsoftteams/platform/messaging-extensions/build-api-based-message-extension, then you can get bearer token to authorize your post message using Graph API as this link mension, https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=http.

Original answer:

Yes, it is possible to send notifications from your software/desktop application to MS teams. You can either use Microsoft Graph API for MS teams or the MS Teams Incoming hooks feature.

I found it much easier to use incoming hooks.

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

  1. In your teams, right-click on your channel. And search for Incoming Webhook.
    Right click channel
  2. Installing/Adding Incoming Webhook if not added yet. enter image description here
  3. Configure Incoming Webhook, by providing a webhook name. Click on Create Configure Incoming webhook
  • It will generate a link with unique GUIDs, copy the link enter image description here
  1. Last step, use this command line in the 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

Note: the URL in the command line contains some faked GUID unique id reference, but you need to replace it with the one you get from webhooks.

You can either call this line in the command line, PowerShell, or any other programming language that can make a post request and incorporated it in your code. In this case for answering the question, I implemented the post requirest in 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);
    }
}

Now when I run the command or C# code I get a message in that channel:

Message Demo


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

Disclaimer: I wrote an article on my personal blog that covers this topic.

like image 105
Maytham Avatar answered Sep 15 '25 05:09

Maytham