Events management requires quick responses, which can be challenging since there is typically an incredibly large volume of events that take place every day. 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 important 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 Go, 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
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. In order to begin receiving GitHub events for you to react to (such as someone creating a pull request), you need to create a GitHub App.
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 by using Serverless Framework, but you could use anything you want as long as you get a public URL. If you are interested in using a Lambda function as well, your “main.go” can be as simple as just calling “lambda.Start” and passing in the function that will hold the business logic.
Because you are using a public URL, security is important. Once you have created the GitHub App, you will be given a secret token that you can use to help validate incoming requests. The google/go-github package makes this easy; all you have to do is call github.ValidatePayload and pass it your secret token and incoming HTTP request to ensure the payload is valid. If successfully validated, the function will return the JSON payload that defines the event you just received. You can parse the payload by using the function github.ParseWebhook, which will return to you an empty interface and can hold any value. Therefore, you will need to use type assertion to access the underlying type to see what event you have received.
Depending on the event type, you might want to take different actions. Perhaps if someone made a new issue you would receive the type github.IssuesEvent, which would be good to perform initial triage such as adding the appropriate labels. It is also possible to receive the event type github.CommitcommentEvent, which would indicate someone had just made a comment that you could check for special commands such as automatically restarting the build system. You could, for example, also create a welcome comment whenever someone creates a new issue, as seen in the code snippet below.
Creating a Slack Bot
These examples have focused on interacting with GitHub, but can be replicated to create a Slack bot similar to the Google Calendar bot. The package slack-go/slack allows you to interact with the Slack API with which you can post messages or create “modals” that allow users to interact with custom buttons and lists.
Creating a Slack bot is a similar process to creating a GitHub App. Slack provides a user interface to register a bot, and similar to GitHub, the main thing it needs from you is a public URL so it can send your bot events. You can then also verify incoming events by using the function slack.NewSecretsVerifier using the secret token provided by Slack. As an example, once these steps are completed, your bot can then begin sending messages to channels to help you answer questions.
Bringing it to Life
An example of a bot can be found in the Telegraf project, which is InfluxData’s open source data collection agent. We have a bot written in Go helping us maintain the repository named “telegraf-tiger.” One of the first interactions users have with the bot is when they submit a new pull request. When the request is made, the bot will chime in with a friendly reminder to sign our contributor’s license agreement (CLA).
Once signed, the bot can be asked to verify if the CLA has been signed by adding a comment “!signed-cla” and give the pull request a shiny green checkmark to let the maintainers know everything is ready.
Another area our Tiger bot supports is sharing the artifacts that our continuous integration (CI) system builds for each pull request with a helpful comment that links to each type. This helps spread awareness of the artifacts so that anyone can easily help test the new feature or bug fix without having to set up a local developer environment themselves.
The Big Takeaway
Bots are an exciting developer tool that can automate mundane assignments and allow engineers to focus on bigger, more engaging tasks. Resources such as GitHub also make it easy to develop bots using Go in various ways. Bots are commonly found across interfaces, with some simply reminding us of upcoming meetings, and others ensuring that important license agreements are signed.
The use cases for bots are endless, and with the right tools, bots can help engineers focus on more significant tasks at hand. Developers can rest easy knowing their bots are automating tasks and responding to the endless alerts they don’t want to deal with. Instead, they’re able to focus on important human activities, like sleep, so they can dream up the next big thing.