rishi/python-slack-bot
Published 2 days ago
13 Pulls
A slack bot demo on top of the hello-python-flask project


Clone and deploy this project in 3 steps

If you're a new user, install the hasura CLI tool before you continue!

$ # 1) Login if you haven't already
$ hasura login

$ # 2) Run the quickstart command
$ hasura quickstart rishi/python-slack-bot

$ # 3) Git add, commit & push to deploy to your cluster
$ cd python-slack-bot
$ git add .
$ git commit -m 'First commit'
$ git push hasura master

NOTE: You might need to follow more configuration steps (eg: adding API tokens) before you run the git push command. Please refer to the README below for further instructions.

Check out the instant Hasura APIs, manage data & users

$ # Use the API console to explore APIs, modify your schema, manage users
$ hasura api-console

Check out the microservices deployed

$ # View the URLs of the microservices deployed on this cluster
$ hasura microservices list

README.md

python-slack-bot

This is slackbot you will have:

This is a simple slackbot that responds to slash commands and uses message buttons. It allows users on a channel to post messages to the channel.

Features:

  1. Receive command: /anonbot here's a message for the channel sent from an anonymous team member
  2. Save the command data in the database (uses Hasura data APIs)
  3. Ask the slack user to confirm if they want to post their message
  4. If the user clicks on the confirm button, post the message to the channel as the bot so that everyone can see

Why this is the best way to get started with a slackbot:

  1. This is a small but perfect example of how to implement a bot that accepts commands
  2. The bot asks users for their confirmation and is a good starting point for implementing interactive commands/messages/buttons in slackbots
  3. Slack requires all bots to have API callbacks on HTTPS. This project can be deployed on a free Hasura cluster in one command which auto-generates certified free SSL certificates (LetsEncrypt FTW) :)

You can make trivial modifications to the code to make the bot work as you wish.

This slack bot builds on top the following slack APIs:

  1. https://api.slack.com/custom-integrations/slash-commands
  2. https://api.slack.com/interactive-messages
  3. https://api.slack.com/methods/chat.postMessage

Codebase structure

All of the code is in one file that you can read at: microservices/bot/app/src/server.py

1. slash command callback request URL

POST /echo
Content-Type application/x-www-form-urlencoded

command=/anonbot
text=here's a message for the channel sent from an anonymous team member
...

As taken from: https://api.slack.com/custom-integrations/slash-commands

This callback will take the message text and save it in the database. It will also reply to the user asking the user to confirm if the user wants to paste the to everyone in the current channel.

2. Interactive message callback request URL

POST /confirm
Content-Type application/x-www-form-urlencoded

payload={..JSON payload describing whether the user confirmed to post the message...}

This is based on: https://api.slack.com/docs/message-buttons

Deployment guide

  • Quickstart the project from Hasura hub.
$ hasura quickstart coco98/python-slack-bot && cd python-slack-bot
  • Create a Slack app. Name it whatever you want. Choose the workspace you want to run it in.

CreateApp

  • On creation, you will be taken to the app management page. Click on slash commands and create a command. Add the URL to be https://bot.cluster-name.hasura-app.io/echo . You can find your cluster name by running $ hasura cluster status from the project directory.

AddCommand

AddInteractive

  • Go to Basic Information in the left panel and click on Add features and functionality -> Bots. Add a bot user and name it whatever you like.

AddBot

  • Go to OAuth and Permissions in the panel on the left. Scroll down to scopes and add the following permission scope.

Scope

  • Scroll up and install the app to your workspace. Once it is installed, you will see a bot access token. Copy this token and add it to your secrets (since you do not want to expose the token explicitly in your code).
$ hasura secret update bot.access.token <bot_access_token>
  • Go to Basic Information in the panel on the left. Scroll down to App credentials. Copy the verification token. Add this to your secrets as well

app_creds

$ hasura secret update slack.token <verification_token>
  • Push these changes to your Hasura cluster.
$ git add .
$ git commit -m "First commit"
$ git push hasura master

You can test your app in your channels. :D


GIT REPOSITORY
Not Available

PUBLISHED BY
rishi on Mon Dec 11 2017
© 2017 Hasura Technologies Pvt. Ltd. All rights reserved