Skip to content

Python, Flask, Postgres, Slack API, Heroku. Giveth karma and taketh karma away! Setup instructions explain how to make Slack Apps/Bots (including how to send outgoing and incoming API requests), how to set up Slack bots on Heroku, and how to connect bots to Slack channels.

cjaiello/Slack-Karma-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Slack Karma Bot

By Christina

Technologies Used

Python, Flask, PostgreSQL, running on Heroku

Description

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.

Usage:

@christina_aiello++ or @christina_aiello-- (or +++, ++++, etc)

How to Use Karma Bot

Local Setup

(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 the Repository

Make Heroku app

  • 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 then Create 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: Your App Name
  • In the Deployment method section, click GitHub Connect to Github 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 in Heroku Postgres and install the Hobby Dev - Free version Install Postgres
  • Go back to the Settings tab and click Reveal config vars again
  • The new DATABASE_URL config var (environment variable) has been added for you! Now when you access os.environ["DATABASE_URL"] in the Slack Karma Bot app code, the application will pull in the value of that new DATABASE_URL Postgres

Make Slack Workspace and Slack App+Bot

  • 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 Create Slack App
  • 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 Install App to Workspace
  • You now have a bot token! Bot Token
  • On the Heroku Settings tab https://dashboard.heroku.com/apps/christinastest/settings (or whatever your URL is, which will have something other than christinastest and will instead have your app's name in it), go to Reveal config vars again and add the Slackbot token: SLACK_BOT_TOKEN and value is whatever your value is Slackbot Token
  • 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 Event Subscriptions
  • Paste in the URL in the box: https://christinastest.herokuapp.com/karma (but replace christinastest with whatever you named your app)

Event Subscriptions

  • 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'] Event Subscriptions
  • 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 Monitor Build and Deploy
  • 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

Event Subscriptions

  • When it succeeds, you'll see this: Event Subscriptions
  • Once it says your url is verified, go back and comment out (or just remove) that return request.json['challenge'] line again in app.py's karma function
  • Scroll down and click on Subscribe to bot events, and then add app_mention, message.channels, and message.groups. This will let your bot listen for messages. Event Subscriptions
  • 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.

Final Setup Steps

  • 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, where postgresql-cubed-27245 is the name of your database and christinastest is the name of your app Get Database Name
  • 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!

Debugging

  • If you're having issues and need to debug, run heroku logs --tail --app christinastest in a terminal window (in any directory), where christinastest is the name of your app on heroku. To quit, type \q

About

Python, Flask, Postgres, Slack API, Heroku. Giveth karma and taketh karma away! Setup instructions explain how to make Slack Apps/Bots (including how to send outgoing and incoming API requests), how to set up Slack bots on Heroku, and how to connect bots to Slack channels.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages