Today more and more companies use Slack to ensure communication within their teams. Slack includes different features, including direct messages, private and public channels, voice and video calls, file searching, and bot integrations.
This article is a tutorial on how to make a Slack bot. Slack bot is a program that can perform different tasks, like sending messages, files, pictures, opening modals, sending emoji reactions, alerting on various events, etc.
Why Create a Slack Bot?
Though Slack offers a lot of applications for any purpose in the Slack App Directory, there can still be a need for custom applications, as every business has its own needs and requirements. Sometimes it is extremely hard to find a suitable option from the existing ones. In this case, it can be a great idea to create a Slack bot of your own.
There are many ways how to use Slack bots to your advantage. They may simplify work processes or to increase revenue, so let’s recall some of them:
- Automating business processes;
- Integrating with the tools used for marketing, sales or project management;
- Giving the users an option to perform actions without leaving Slack;
- Providing support services;
- Assistance in sales, allowing to place orders using a Slack bot;
- Creating a fun tool that can be a part of your company's culture.
If you want to create your own bot, let’s take a look at an example of how to make a simple Slack bot.
Step 1: Initial Slack Bot Setup
For study purposes we’ll create a simple Slack bot that adds a new command to your workspace that mentions all admins in your workspace.
All code listed below can be found in our repository.
In general, a Slack bot is simply an HTTP server that provides webhook endpoints and handles events sent by Slack. How might Slack reach your local machine HTTP server? Well, in this exact case it doesn’t as we use so called Socket Mode. It makes our application connect to Slack servers and listen to events from there instead of Slack connecting to our server. If there were no Socket Mode, we would have to use something like ngrok to expose our local port to the Internet. Read more about Socket Mode in official documentation.
Step 2: Creating a Slack Bot
Go to https://api.slack.com/apps?new_app=1. There you should see a modal window:
Choose “From an app manifest”, select your workspace in the dropdown and then paste the following YAML to the text area, and then press “Create”:
You will be redirected to the app page. Press the “Install to Workspace” button, and then allow access to your workspace. When you click “Allow”, Slack grants the permissions you asked in the manifest. If you should add a new permission, you should reinstall the application as well.
Next we need an app-level token to use Socket Mode. When you’re redirected back to the app page, scroll down to “App-Level Tokens” sections and click “Generate Token and Scopes” there.
Step 3: Running and Testing the Application
Go to any channel and start typing “/admins”. You should see a suggestion with your app command.
Choose it and then send the message. An ephemeral message will be sent to the channel (hence only you can see the message).
Slack applications can be a powerful tool to automate your business' workflows and make them more effective. When you think about how to create a Slack bot efficiently, it is very important to consider its infrastructure, the events it receives, and the user experience.
Hope that this guide helped you better understand how to make a Slack bot that can perform some useful tasks. It is also possible to add some extra features to your bot, or modify it so that it will suit your requirements. If you still have questions, please feel free to contact our team, and they will be happy to help. Our experts have extensive experience in Slack bot development and are glad to share their knowledge.