Track My Slack
Track My Slack is a slack bot that alerts workspace members when they mention a key phrase. Users have the ability to add or delete trackable phrases.
Setup
Before we can run docker-compose up
, we'll need to take care of a few things related to the way Slack grants permissions and routes requests.
Setup ngrok, a tunnelling software
- Navigate to https://ngrok.com/ and download ngrok for your operating system (this is required because Slack uses request URLs)
- Open up a terminal window
- Unzip ngrok:
unzip /path/to/ngrok.zip
- Run the following command:
./ngrok authtoken <AUTH_TOKEN>
-- replace auth token with the auth token listed on step two of this page: https://dashboard.ngrok.com/get-started/setup
- Run
./ngrok http 5000
-- you should see the following:
- Save the second "Forwarding" URL for later. It will look like this:
https://<some-string>.ngrok.io
Configure slash commands
- Login to a Slack workspace of your choosing through the browser, where you'll be creating an app to test the bot
- Navigate to https://api.slack.com/
- Click 'Your Apps' in the top right corner
- Create an app
- Click 'Basic Information' within the 'Settings' menu, and scroll to 'Add Credentials' -- next to 'Signing Secret' click 'Show' -- copy and save for later
- Click "Slash Commands" within the "Features" menu -- this is where we'll configure our commands for adding and deleting phrases to the watch-list
- Click 'Create New Command'
- Fill it out like so:
- For the 'Request URL' field, copy and paste the ngrok url from step (6), and add
/add_phrase
to the end of it
- Save changes!
- Duplicate steps 6 - 8 for 'delete_phrase'
Congrats! You've configured the slash commands to add and remove watch phrases.
Configure permissions
- Navigate to "OAuth & Permissions" in the "Features" menu
- Click 'Install App to Workspace'
- Click 'Allow'
- Copy the 'Bot User OAuth Access Token' for later
- Scroll down to 'Scopes'
- Under 'Bot Token Scopes', click 'Add an OAuth Scope'
- Add the following bot scopes:
- Re-install your app:
Setup Environment Variables
- Open up a terminal window
- Clone the respository into a directory of your choice:
git clone https://github.com/codybreene/slack_bot.git
cd
into the slack_bot
directory
- Create a file in that directory to store your environment variables:
touch .env
- Open up the directory in a coding editor of your chosing, and navigate to the
.env
file
- Add the following:
SLACK_SIGNING_SECRET=<SIGNING_SECRET>
SLACK_BOT_TOKEN=<BOT_TOKEN>
AIRTABLE_BASE_KEY=appvq9i1bO2gEbi6Y
AIRTABLE_API_KEY=key54bic9Lngk71On
- Replace with the signing secret you copied from 'Basic Information' (three sections up, step 5)
- Replace with the Bot User OAuth Access Token you copied (two sections up, step 4)
- Save the file
Launch the app
- Ensure you have Docker installed: https://docs.docker.com/get-docker/
- Ensure you're not running any docker containers:
docker ps
then docker stop <CONTAINER_ID>
- Run:
docker-compose up
- Navigate to
localhost:5000
, and you should see an indication the app is running
Add Event Subscriptions
- Navigate back to your app dashboard on the Slack website
- Click on 'Event Subscriptions' in the 'Features' menu
- Toggle events on
- Copy the ngrok URL from the previous section, paste it into the 'Request URL' field and add
/events
:
- Click 'Subscribe to bot events'
- Click 'Add Bot User Event' and add the
message.channels
event:
- Save changes!
Test it out!
- Open up the slack workspace
- Click on a channel where you'd like to test the bot
- Click 'Add an App' right below the channel description
- You should see your app -- add it!
- Type some test messages
- Type some slash commands
/add_phrase
and /delete_phrase
Technologies
- Flask
- Docker
- Slack Events API
- Airtable API
- ngrok