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.
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.
and use any email client to send email message.
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:
Incoming Webhook
.Incoming Webhook
if not added yet.
Incoming Webhook
, by providing a webhook name. Click on Create
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:
In case you need to remove the hook that you have added, click on Configured then Configure. And Manage the webhook:
And remove
Disclaimer: I wrote an article on my personal blog that covers this topic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With