Why Your Startup Needs Serverless Slack Bots
In this post, I will explain how we utilize serverless Slack bots in Epsagon, demonstrate their value, and provide an example — including corresponding open source code, using Python and Serverless Framework, to implement your custom serverless Slack bot.
Slack is without a doubt the most famous messaging software for startups. Startups have this unique need for speed and agility, and Slack is just perfect for that. No overhead — just pure efficiency.
One of my favorite features in Slack is its vast list of Apps and Integrations, as well as the easy way to develop custom apps, which are commonly called “Slack Bots.” The point behind these apps is simple — you want all the information in a single location, and if Slack is the primary way of communication and knowledge sharing, why not stream information from other sources into it as well?
You can find many lists of recommended apps, such as this one. I highly recommend looking for existing apps that fit your needs and add them. Built-in apps are great — but what if you need a specific, tailor-made app, which precisely meets your requirements? Luckily, the people at Slack thought of this and created an easy way to develop custom apps and integrations. Slack has excellent documentation which you can find here, and it will guide you step-by-step to build your app.
Here are just some of the bots that we’re using at Epsagon. Usually, every Slack bot reports to a dedicated Slack channel.
- GitHub (built-in): the easiest way to follow changes in code and issues.
- TravisCI (built-in): this continuous integration tool has a smooth Slack integration.
- Mention (built-in): a useful tool for tracking things around the internet, which has a ready-to-use Slack bot.
- RSS feeds (built-in): the AWS feed, Google Alerts, and more. These can be easily added using the “/feed …” statement.
- Custom RSS feed (in-house): we implemented our own Google Alerts Slack bot since we wanted to improve the formatting of the built-in integration.
- Stack Overflow (in-house): watch and comment on Stack Overflow questions.
- Reddit (in-house): our way of monitoring the front page of the internet for discussions that are relevant to us.
- Meetup (in-house): notifications about new meetups around the world that we might be interested in.
- Birthday bot (built-in): because it’s fun!
As you can see, we love Slack bots — both built-in and custom ones. What is the easiest way to develop a custom Slack bot?
To run a Slack bot, you need to run code (surprising, isn’t it?). This code sends events via the Slack API, which then publishes them to a corresponding Slack channel. What is the trigger for the bot to run? Well, it can be triggered by a user action (as happens with interactive Slack bots), but usually — it’s going to be time-based. Every 1 minute, every 1 hour, etc.
This code also needs to have some sort of a compute unite. It can run on a server, or as a serverless function.
Serverless fits the use case of Slack bots perfectly. Using a serverless compute service, such as AWS Lambda, you can deploy Slack bots in no-time, and never worry about them again!
I will demonstrate the Reddit Slack bot that we’ve implemented in Epsagon. It tracks submissions in subreddits that we are following.
This is an open source project which can be found in our GitHub.I’m not going to go through the entire process of configuring Slack — you can find excellent documentation online. Instead, I’m going to focus on workarounds that we did to make things more convenient for us. Note: although this is a Reddit bot, the general ideas are the same for any scheduled serverless Slack bot. The Reddit bot can be used as a template for other bots.
Slack webhook: Webhooks for Slack easily be created using this guide.
The configuration file (config.json): The configuration contains the Slack URL, the Reddit client parameters, and the subreddits to follow. The subreddits are written in Reddit’s format. In this case, we will get notified about submissions which include the keyword “lambda” in the subreddits “programming” and “Python.”
Lambda code (main.py): The code includes three functions.
- Handler — the Lambda handler, called every 60 minutes in this case.
- get_submissions — retrieves new submissions using the Reddit API.
- update_slack — formats the message and posts it to the Slack channel.
Notice that the handler function contains an update of the function’s configuration — specifically, the LAST_SUBMISSION environment variable. It is used to remember the time of the last submission, and to publish only new ones to Slack. This is a way to save a state in the function without setting up a database. We’ll get to it later.
serverless.yml: We are big fonds of the Serverless Framework. Deploying new Lambda functions is straightforward using the framework.
The serverless.yml file includes special permission for the function — to edit its configuration (lambda:UpdateFunctionConfiguration). This permission is needed because the handler function is updating the environment variable LAST_SUBMISSION. The Resource field can be calculated using the AWS region, the account ID (which can be found using this guide), and the function’s name, taken from the service field at the top of the file.
This approach is indeed “hacky,” but it enables us to forget about setting up additional AWS resources, such as a DynamoDB. Since there is no way to save a state in a Lambda function today, this is an easy solution. While letting a Lambda function change its configuration (including its code) is a potential security risk — this function is triggered only be a scheduled timer and does not expose an external API, which minimizes the risk. However — setting one DynamoDB for all the Slack bots that you have is also a rather easy solution.
As mentioned, the project is available in our repository. Also, it can be used as a template for any time-based serverless Slack bot. We have used the same template at Epsagon for creating Google Alerts bot, Stack Overflow bot, and others.
What is your experience with Slack bots? Do you have ideas for improving our Slack bot? Share your stories with us, and contribute to the project!
Originally published at blog.epsagon.com.