How to create an automated GitHub Bot in Golang

Events management requires quick responses, which can be challenging since an enormous volume of events occurs daily. Therefore, relying on humans to respond to these events quickly is error-prone, messy, and often frustrating. These events have easily reproducible steps that can be codified and are perfect reasons for a bot to exist.

Creating a bot reduces the daily noise of events so that important humans can focus on critical human activities—such as programming, innovating, or, most importantly, sleeping.  There are already many bots that exist and that can solve numerous problems. For example, Dependabot helps keep your GitHub dependencies updated with automatic pull requests. Google Calendar has a Slack bot that reminds you when a meeting is about to start. Of course, there are also situations where there isn’t a pre-made bot yet, which provides the perfect opportunity for developers to create their own!

My preferred programming language is Golang, and I’ve used it to create multiple bots. A favorite bot I created helps to manage a large open source project called Telegraf, which I discuss in more detail at the end of this article. Bots are not limited to open source projects; another bot I created proved valuable when I worked for a larger enterprise company. It was a Slack bot that could run commands on Amazon EC2 Windows instances and help teams quickly perform actions for customers, such as resetting passwords in seconds as opposed to days.

Creating Your Own Bot for GitHub with Golang

Let‘s look at how you can get started creating a bot written in Go that can interact with GitHub. The package google/go-github lets you interact with the GitHub API and makes creating a bot for GitHub easy. Tools like these are your first step in developing your own bot. Using this package, you could create something similar to what Dependabot does. To begin receiving GitHub events for you to react to (such as someone creating a pull request), you need to create a GitHub App.

GitHub App 1

The GitHub documentation does a great job describing how you can do this, but the important piece to be aware of is a callback URL. The callback URL will be a publicly available address associated with your Go program so that GitHub can route events to it. I use AWS Lambda functions to host the bots I’ve created and managed using Serverless Framework, but you could use anything you want if you get a public URL. If you are also interested in using a Lambda function, your “main.go” can be as simple as just calling “lambda.Start” and passing in the function that will hold the business logic.

GitHub App 2
Golang AWS Lambda