Python, Flask, PostgreSQL, running on Heroku
Simple slack karma bot running on Heroku. You can give karma to users and take karma away from users. Karma giving and receiving is capped at 5 units of karma per Slack message.
@christina_aiello++ or @christina_aiello-- (or +++, ++++, etc)
(This is long, but ~50% of it is what you'd do to set up any Slack bot, regardless of whether or not you use Heroku to host the app server.)
- Fork this repository so it's in your account
- Clone it to your machine using
git clone
(More help available at https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository) cd Slack-Karma-Bot
to get into the directory of the code you just cloned
- Install the Heroku CLI (command line interface) https://devcenter.heroku.com/articles/heroku-cli
- Go to https://dashboard.heroku.com/apps (register if you haven't)
- Click
New
and thenCreate New App
- Give it a name and click
Create
- You will be dropped onto the
Deploy
tab. Note your app name and how to access your app via web browser: - In the
Deployment method
section, clickGitHub
Connect to Github
- Choose your repository
- Click
Enable Automatic Deploys
. Now, anytime you push to Github, your code will be automatically deployed to Heroku! - Go to the Resources tab
- In the
Add-Ons
section, type inHeroku Postgres
and install theHobby Dev - Free
version - Go back to the
Settings
tab and clickReveal config vars
again - The new
DATABASE_URL
config var (environment variable) has been added for you! Now when you accessos.environ["DATABASE_URL"]
in the Slack Karma Bot app code, the application will pull in the value of that newDATABASE_URL
- Make a Slack workspace (They're free, don't worry!) at https://slack.com/create or I can invite you to a test Slack workspace I set up for this Slack bot and my standup Slack bot
- Go to
https://api.slack.com/apps
- Make your app and attach it to your workspace you just made
- On the Basic Information page, go to
Permissions
- Scroll down to
Bot Token Scopes
- Add:
app_mentions:read
,channels:history
,channels:join
,channels:read
,chat:write
,chat:write.customize
,chat:write.public
,groups:history
,incoming-webhook
,users.profile:read
,users:read
- Now scroll up and click
Install App to Workspace
- You now have a bot token!
- On the Heroku Settings tab
https://dashboard.heroku.com/apps/christinastest/settings
(or whatever your URL is, which will have something other thanchristinastest
and will instead have your app's name in it), go toReveal config vars
again and add the Slackbot token:SLACK_BOT_TOKEN
and value is whatever your value is - Back on the Basic Information page in Slack when managing your new Slack app, go to
Event Subscriptions
- Click the switch to turn event subscriptions on
- Paste in the URL in the box:
https://christinastest.herokuapp.com/karma
(but replacechristinastest
with whatever you named your app)
- Go back to the code you cloned onto your computer.
- Open
app.py
- Go to
def karma():
and uncomment this line:return request.json['challenge']
- Save and push to git
- Wait for it to deploy (takes... ~1min, sometimes 30 seconds?). You can monitor the build and deploy by going onto the Heroku website, going to your app, and clicking on the "Activity" tab
- Go back to the Slack
Event Subscriptions
page for your app (URL example: https://api.slack.com/apps/A01BFFWKQ4T/event-subscriptions?) - Click the
Retry
button to the right of the URL you pasted in to verify
- When it succeeds, you'll see this:
- Once it says your url is verified, go back and comment out (or just remove) that
return request.json['challenge']
line again inapp.py
'skarma
function - Scroll down and click on
Subscribe to bot events
, and then addapp_mention
,message.channels
, andmessage.groups
. This will let your bot listen for messages. - Click the green
Save Changes
button at the bottom right of the page - Now you'll see a message in a yellow box at the top of the screen that says, "You’ve changed the permission scopes your app uses. Please reinstall your app for these changes to take effect (and if your app is listed in the Slack App Directory, you’ll need to resubmit it as well)." Click on
Reinstall your app
and reinstall it.
- Go to the #general channel in Slack and tag the bot, example:
your-username 9:56 PM
@Name Your App
- After tagging the bot, you'll be asked to invite it to the channel. Invite it.
- Make a channel called
karma_bot_log
in Slack - Go back to the Heroku
Resources
tab (Ex: https://dashboard.heroku.com/apps/christinastest/resources) - Click on your database
- At the top of the page you're brought to, copy the name (Example:
postgresql-cubed-27245
) - Open up a new terminal and run
heroku pg:psql postgresql-cubed-27245 --app christinastest
in any directory, wherepostgresql-cubed-27245
is the name of your database andchristinastest
is the name of your app - Now that you're connected to your database, run
CREATE TABLE users (id SERIAL PRIMARY KEY, username varchar(128), karma int);
to create your table - Finally, go back to Slack, give yourself karma via
@username ++
, and watch your bot respond!
- If you're having issues and need to debug, run
heroku logs --tail --app christinastest
in a terminal window (in any directory), wherechristinastest
is the name of your app on heroku. To quit, type\q