<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=2233467260228916&amp;ev=PageView&amp;noscript=1">

GitHub integration with Workplace by Facebook via Azure Functions

Cegal Tech Advisors Fighting fuzz by striving to make your coding life more straight forward.
02/12/2020 |

At my company, we are using Workplace by Facebook for all of its possibilities and functionalities, and it’s really useful for sharing information with the whole organization. We have a lot of different Workplace groups with different types of content. One of those groups is related to technology and coding where we decided to share and create a post in Workplace on some action or change in one of our GitHub repositories.

Something new I wanted to test is if I could get notifications on merges to the master branch, releases in GitHub, and then create a Workplace post with all the necessary information. Let me show you how I did this with the help of Azure Functions.

In this post, I will try to show the easiest way to integrate these two systems and share my code and findings in one place. So, don’t expect anything fancy because this is still a work in progress and it will grow in functionality. As an easy example, I decided to use the GitHub trigger on the creation of a new issue in the Repo. That being said, the first thing I had to do is to create a function which I need to expose and use the Azure function URL to create a webhook in the desired GitHub repository. I used C# for the development of the integration.

If you are just starting with Azure Functions with Java and you came upon this blog post, this is what I have followed and installed and eventually used and tested successfully. Take a look: The quickstart of how to use Java and Maven to create and publish a function to Azure.

If you want to use C#, then you need the latest version of Visual Studio 2019 from which you have all the support needed to develop and deploy in Azure Functions and much more.

Ok let's start with the code of the Azure function written in C# then a setup of the GitHub webhook and setup of the Workplace Bot

Azure Function code

  
  

namespace GithubWorkplaceAzure

{

    public static class GithubWebhookWorkplaceFunction

    {

        [FunctionName("GithubWebhookWorkplaceFunction")]

        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)

        {          

            // Get request body

            dynamic data = await req.Content.ReadAsAsync<object>();

 

            // Extract github data from request

            string repoName = data?.repository?.name;

            string repoDescription = data?.repository?.description;

            string gitHubIssue = data?.issue?.title;

            string issueDescription = data?.issue?.body;

            string gitSender = data?.sender?.login;            

            string issueURL = data?.issue?.html_url;

 

 

            //Creating a new issue post content for Workplace

            string message = "New issue in repo: " + repoName + "\n" +

                             "Repo description: " + repoDescription + "\n" +

                             "\n" +

                             "Issue: " + gitHubIssue + "\n" +

                             "Comment: " + issueDescription + "\n" + "\n" +

                             "\n" +

                             "Sender: " + gitSender + "\n" +

                             "Issue url: " + issueURL;

 

//Posting created message to the Workplace

            using (var httpClient = new HttpClient())

            {

 

                httpClient.BaseAddress = new Uri("https://graph.facebook.com/");

 

                var parametters = new Dictionary<string, string>

                {

                    { "access_token", "[YOUR WORKPALCE TOKEN VALUE]" },

                    { "message", message }

                };                

 

                var encodedContent = new FormUrlEncodedContent(parametters);

                string groupId = "[YOUR WORKPLACE GROUPID]";

 

                var result = await httpClient.PostAsync($"{groupId}/feed", encodedContent);

                var responseStr = await result.Content.ReadAsStringAsync();                

                var msg = result.EnsureSuccessStatusCode();

                return req.CreateResponse(HttpStatusCode.OK, "Response Workplace: " + msg);

            }

            

        }

    }

}


GitHub webhook

It’s fairly simple to create a GitHub webhook, choose your desired repository and create a trigger on which actions you want the Azure function to be called. In my case, I created a GitHub webhook when a new issue is created. This is how it looks in my repo:

I haven’t created a secret key in the Azure functions yet, but that is one of my next steps. This was a quick PoC where I wanted to test the possibility of the integration between Github and Workplace by Facebook.

Workplace setup

I will not go into too much detail here only to explain which info we need from the Workplace bot so we could post the content on the desired Workplace group. This is also related if you are using Facebook groups, fun pages or just your Facebook wall.

  1. The first thing you will need is your Group ID. This you can find directly from the link of the group itself. In the code replace the text: “[YOUR WORKPLACE GROUPID]” with your actual GrouId value.
  2. Next is to retrieve the Token value of the Workplace/Facebook bot. In the code replace the text “[YOUR WORKPLACE TOKEN VALUE]” with the actual token value.
  3. And don’t forget that Workplace and Facebook are using the Graph API for all the third-party communication and integration.

Take a look at:  Read more about the Facebook bots

Testing the integration

As a simple test here let’s create an issue in our repo:

As soon as we post the issue we can check the workplace group wall for the new GitHub issue post.

Related articles

Software >Energy solutions >Cegal Central Integration Platform
arrow
How to be a better developer
Cegal Tech Advisors Fighting fuzz by striving to make your...
arrow
Microsoft Cloud
Reduce costs and improve operations with Microsoft Azure Cloud
Editorial staff Cegal want to build a stellar nextgen...
arrow