Reputation: 320
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.
Upvotes: 23
Views: 40894
Reputation: 33397
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.
Upvotes: 49
Reputation: 772
There's another option to consider, You can send emails to your channel. It can be really handy if you already have this infrastructure in the code.
Here's the official Microsoft documentation: https://support.microsoft.com/en-us/office/send-an-email-to-a-channel-in-microsoft-teams-d91db004-d9d7-4a47-82e6-fb1b16dfd51e
Go to your channel and click on "..." -> "Get email address"
Copy the email and you're good to go
Upvotes: 3
Reputation: 4397
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://learn.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.
Upvotes: 3
Reputation: 1015
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.
Upvotes: -2