How to build your first slack bot in 2020 with Python & Flask using the Slack Events-API

Photo by Scott Webb on Unsplash

Step 1: Setting up Initial Requirements!

The requirements are as follows:

  1. A Computer in working condition XD
  2. A slack account and a workspace that you are part of.
  3. Python 3.7 and above Follow this link to download Python for your operating system. You can refer to these tutorials on YouTube for the same — Windows, Mac-OS, Ubuntu (it’ll be somewhat similar for other Linux distributions).
  4. pip pip has been included with the Python installer since versions 3.4 for Python 3 and 2.7.

Step 2: Establishing our Environment

virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary packages for your project. To know more about virtualenv, go here.

pip install virtualenv
python3 -m virtualenv env
source env/bin/activate              (Linux/Mac-OS)env/Scripts/activate                 (Windows)

Step 3: Create a new Slack App

Now it’s time to create the Slack App. Login with your credentials and you should see a screen like this.

export SLACK_BOT_TOKEN=YOUR-BOT-TOKEN-HERE             (LINUX)
set SLACK_BOT_TOKEN=YOUR-BOT-TOKEN-HERE (WINDOWS)
export SLACK_SIGNING_SECRET=your-slack-signing-secret-here

Step 4: Coding the Bot

It’s time to write the actual code that will do the magic! Create a file named bot.py inside the main folder. The project structure now looks like —

|-Slack-tutorial
|-env
|-bot.py

Step 5: Subscribing to events

In order for Slack to contact your local server, you’ll need to run a tunnel. We recommend ngrok or localtunnel. We’re going to use ngrok for this example.

Image Source: https://ngrok.com/
python3 bot.py
ngrok http 3000

Step 6: Go Ahead and interact with the bot

Invite your bot to a public channel, then say hi and your bot will respond!

--

--

We are a bunch of computer-savvy college community kids, here to change the world, bit by bit!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store