Menu

Posting messages to Microsoft Teams from your C#/ ASP NET Core application

Sending emails is a bit old and most check emails a few times a day. One of the interesting ways of integration is Microsoft Teams or Slack. These tools are popular among startups as well as enterprise clients.

Both Microsoft Teams & Slack has RESTfull service. I will use Microsoft Teams for this example.

Select your MS teams channel, create a connector and note down the long URL it provides. It looks something like below,

https://outlook.office.com/webhook/<MICRSOFT_TEAMS_GROUP_ID>@<TENANT_ID>/IncomingWebhook/<MICRSOFT_TEAMS_CONNECTOR_ID>/<MICRSOFT_TEAMS_WEBHOOK_ID>
public class MicrosoftTeamsIntegrationService : IMicrosoftTeamsIntegrationService
{
	private readonly HttpClient _client;

	public MicrosoftTeamsIntegrationService(HttpClient client)
	{
		_client = client;
	}

	public async Task<bool> Notify<T>(MessageEnvelope<T> message)
	{
		using (var request = new HttpRequestMessage())
		{
			request.Method = HttpMethod.Post;
			request.Content = new StringContent(JsonConvert.SerializeObject(message));
			request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

			var response = await _client.SendAsync(request);

			var responseContent = await response.Content.ReadAsStringAsync();

			// MS teams always 200 and responce message with value 1, and its not a Json object. not sure for all messages, but atlease for the card type I tired
			return int.TryParse(responseContent, out int numericValue) && numericValue == 1; 
		}
	}
}

You can read about supported card types here

https://docs.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/universal-actions-for-adaptive-cards/overview?tabs=mobile

As part of your Startup.cs we need to register our awesome service under “ConfigureServices“, like below

public void ConfigureServices(IServiceCollection services)
{
	services.AddMediatR(typeof(.....).Assembly);
	
	string TENANT_ID = Environment.GetEnvironmentVariable(EnvironmentVariables.TENANT_ID);
	string MICRSOFT_TEAMS_GROUP_ID = Environment.GetEnvironmentVariable(EnvironmentVariables.MICRSOFT_TEAMS_GROUP_ID);
	string MICRSOFT_TEAMS_CONNECTOR_ID = Environment.GetEnvironmentVariable(EnvironmentVariables.MICRSOFT_TEAMS_CONNECTOR_ID);
	string MICRSOFT_TEAMS_WEBHOOK_ID = Environment.GetEnvironmentVariable(EnvironmentVariables.MICRSOFT_TEAMS_WEBHOOK_ID);

	services.AddHttpClient<IMicrosoftTeamsIntegrationService, MicrosoftTeamsIntegrationService>("MicrosoftTeamsIntegration", client =>
	{
		client.BaseAddress = new Uri($"https://outlook.office.com/webhook/{MICRSOFT_TEAMS_GROUP_ID}@{TENANT_ID}/IncomingWebhook/{MICRSOFT_TEAMS_CONNECTOR_ID}/{MICRSOFT_TEAMS_WEBHOOK_ID}");
	});
		
	
	services.AddControllers()
                .AddNewtonsoftJson(x =>
                {
                    x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                });
	.....
	.....				
}

If you enable both inbound and outbound connections on the channel, you could pretty much write your own application in MS Teams, but that’s up to you.

Happy coding

Leave a comment