So, a few months back I created a Slack workspace for people from various walks to come together to share interesting articles, videos and podcasts — think of it as a place where you find something interesting to read from various domains such as business, education, technology and so on. People started joining and like all groups, it reached a stage where I felt the # active people were < # people in the group. I wanted to send a message to everyone asking if they’d like to continue or leave the group.
Note: If you’re interested in joining the workspace, comment below.
So, I opened Google and typed “How to send direct message to everyone in a Slack workspace”. To my surprise, there was no direct way of doing it. The only way was to create a Slack bot yourself and add a few lines of code to enable this feature — and I found no article that had the detailed procedure. I had some free time, so I thought I’d try it myself. It would also serve as a good refresher for my upcoming interviews.
Warning: I am an amateur when it comes to programming, so it took me 3 days and lots and lots and lots and lots…. of errors after which it finally worked. Hopefully, this step by step guide should make it easier for you.
Minute 1: Download Python 2.7.13 and Add To PATH
You are going to need to set up the environment before beginning to code. You need the following to create the bot and make it work:
- Python 2.7.13
- Pip and virtualenv
- A slack account and a workspace that you are part of
Note: I did it on my Windows laptop. The commands are slightly different if it’s on a Mac or Linux machine, so kindly check online for the appropriate changes.
That’s it. Now, follow this link to download the Python 2.7.13 version. Do not download Python 2.7 as it is not compatible with downloading the latest Pip versions.
Have you downloaded and installed? Great! Now, if you open up your Command Prompt and type Python, it will show the following:
This is because the Python path has not been added to your PATH environment variable. Go to My Computer -> Properties -> Advanced System Settings -> Environment Variables. You should see the following.
Click on Path and ‘Edit’. Add a new path there: C:\Python27 (assuming that’s where your Python directory is or add whatever the path is).
Now open Command Prompt and type Python. It should work!
Minute 2: Download and Install pip
Time to download pip. pip is a package management system used to install and manage software packages written in Python. Follow this link and download the get-pip.py file and save it on a new folder. This is the folder where all your files should be saved from now.
Now, open Command Prompt, navigate to the folder where the pip file is and type:
It should work and tell you that it’s successful. Now, you have to add the path to the pip file as you did before. Follow the same instructions as before and add this to your PATH environment variables: C:/Python27/Scripts (this is where the pip file is).
Now type pip in Command Prompt and it should work!
Minute 3: Install and Activate virtualenv
virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.
Simply go to the folder where you stored your get-pip.py file and type the following:
pip install virtualenv
Once it’s installed, you need to create a new virtualenv and activate it.
The above commands should give you the following screen:
Minute 4: Create A New Slack App and Slack Bot
Now it’s time to create a bot that will do the actual task of sending messages to all members. Follow this link and go to the Slack API page. Login there with your Slack account credentials and you should see a screen like this.
Once you click on Create an App, fill in the App name and choose your workspace:
Now, move over to the Bot Users tab on the left and create a new bot:
Once you’ve added this, you need to go to OAuth & Permissions and authorize the bot to post in your workspace:
All good so far? Now, go back to OAuth & Permissions. You should see the following two tokens. These are very important and hence keep it to yourself.
Minute 5: Install Slackclient and Check If API Call Works
Open up the good old Command Prompt again (hopefully it’s still kept open), go to the folder with the pip file and type the following:
pip install slackclient
We need the SlackClient to connect to the Slack Bot. Let’s see if it works now. Copy the Bot User OAuth Access Token and keep it. Now, type python in the Command Prompt and enter the python environment.
Minute 6: Time To Test The Environment
Till now, all we did was set up the environment. Now it’s time to write the actual code that will do the magic. I want you to first test if your environment works by writing a code that gets the list of all channel names in your workspace and posts that in the Prompt. Use the following code:
(Comment below to get this code + my code which will come later on)
Now, in the Command Prompt, type:
It should display below all the channel names in your workspace. Works? Awesome!
Minute 7: Method I: Use Dan Goldin’s Code
First, we will use Dan’s code. Next, I will show my code.
So, hopefully you have downloaded Dan’s code and put it in the folder you’ve been using up till now.
First, we need to install requirements.txt. In your Command Prompt, exit the python environment by typing exit(). Now, type:
pip install -r requirements.txt
It should install all the packages needed!
Now, open spam_channel_members.py in IDLE (or whichever python IDE you have) and paste your token in the line
sh = SlackHelper(‘paste your token here’)
Minute 8: Method 2: Use My Code
Now, this is the final part. Type the following in the prompt:
python spam_channel_members.py your_test_channel_name your_test_message
This should hopefully work!
So, I wrote a code to do the same which is shorter but probably not optimal. Here is the code:
I am basically getting the list of users in the workspace using the users.list API call, extracting the members dictionary from it, and using a for loop to send messages to the member using their ID. This worked fine for me, and hope it does for you. If you need the above code, commend below and I’ll email it to you!
Once you have the above code ready in a Python file, simply go to Prompt and type:
That is all! It should hopefully show you a ‘Success!’ message once it’s executed and you should have sent a message to everyone in your workspace.
So hopefully that worked out for you. This was just a small project I worked on last week and thought it’d be useful if someone else wanted to do the same. If you encountered an error at any step, Stackoverflow is your best friend. I had errors at every step and it took me the longest to set up the environment. Initially, I tried to do this using Stdlib and Node.js, however, that did not work. Python to the rescue!
This is my first article where I’ve talked about a technical concept and I plan on doing this more from now. I would love to hear your comments on whether the article was articulate enough for you to understand. Thank you for reading till now. Cheers 🙂
If you found this enjoyable, do Follow me for more articles. I’m a sufficiently frequent writer. Here is my personal blog.