Use Case
In most of the Organization, Microsoft Teams has become an Integrated communication channel and GitHub is used for managing the repositories. The idea is to keep the respective team posted about the issues, and keep everyone in sync so that timely action can be taken to resolve/mitigate the issue. Whenever any issue is created in the GitHub, the same will be posted in the Teams Channel.
Services Used
· Microsoft Teams
· Azure Functions
· GitHub
Add incoming Webhook Connector to the Teams Channel
· Click on Apps at the bottom left panel
· Search for Webhook
· Click on Incoming Webhook in the right panel
· Click on Add to a team
· Select the Teams Channel
· Click on Set up a connector
· Enter the name of the connector
· Click on Create
Create Azure function via portal
We will create Azure function with Http Trigger which will invoke the Team Webhook url to post the Git Issue details to the Teams channel. Azure function will be invoked by the GitHub action whenever Issue is created.
To create Azure Function,
· Click on Create a resource link
· Click on Compute and then Click Function App
· Enter the Required details
· Select Runtime Stack as .Net
· Click Review + Create
· Click Create
· We can see that the Function app is now up and running
· Click Functions in the left panel and then click Add button at the top panel
· Select Http Trigger template from the right panel
· Click Code + Test
· Update the code snippet as below (Replace the webhook url):
#r “Newtonsoft.Json”
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Text;
public static async Task<IActionResult> Run(HttpRequest req, Ilogger log)
{
log.LogInformation(“C# HTTP trigger function processed a request.”);
string gitIssue = req.Query[“issue”];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
gitIssue = gitIssue ?? data?.issue;
string responseMessage = string.IsNullOrEmpty(gitIssue)
? “This HTTP triggered function executed successfully. Pass a Git Issue in the query string or in the request body.”
: $”Git Issue posted to the Teams Channel successfully”;
if(gitIssue != null)
{
var result = await SendTeamsChannelMessage(gitIssue);
return new OkObjectResult(responseMessage);
}
else
{
return new BadRequestObjectResult(responseMessage);
}
}
public static async Task<string> SendTeamsChannelMessage(string gitIssue)
{
using (var client = new HttpClient())
{
Idictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add(“text”, gitIssue);
string json = JsonConvert.SerializeObject(dictionary);
var requestData = new StringContent(json, Encoding.UTF8, “application/json”);
var response = await client.PostAsync(String.Format(“https://tvcbabugmailcom.webhook.office.com/webhookb2/f2d956b3-31c5-4f30-a1f5-44e2add22d72@c90da82d-705a-4327-8208-3318bda5f9c6/IncomingWebhook/592e04c9fc7b4c8d90d290b7e829941a/0dae7508-a619-4c03-8553-91427b816b64”), requestData);
var result = await response.Content.ReadAsStringAsync();
return result;
}
}
· Enter the values in the Input tab
· In the output tab, we can see the success response.
· The message has been posted in the Teams channel as below:
Click on Get Function URL and copy it. This will be used to configure in the GitHub settings
Add Webhook in GitHub
· Login to the Github
· Select one of the existing repositories
· Go to Settings tab
· Click on Webhooks from the left panel
· Click Add webhook from the top panel
· Paste the Function URL which we had already copied, in the Payload URL
· Select the option Let me select individual events
· Select Issues from the options
· Click Add webhook
For Testing, We will send only the Issue Title.
Modify the function code as below:
gitIssue = gitIssue ?? data?.issue.title;
Check the entire flow by creating an issue in the GitHub
· Click on the Issues tab
· Enter the details for the issue
· Click on Submit new Issue
Now, We can see the Issue title posted in the Teams channel
The functionality can be extended to include other elements of the issue and customized as needed.
Happy Coding 😊