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:
- Receive command:
/anonbot here's a message for the channel sent from an anonymous team member
- Save the command data in the database (uses Hasura data APIs)
- Ask the slack user to confirm if they want to post their message
- 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:
- This is a small but perfect example of how to implement a bot that accepts commands
- The bot asks users for their confirmation and is a good starting point for implementing interactive commands/messages/buttons in slackbots
- 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:
- https://api.slack.com/custom-integrations/slash-commands
- https://api.slack.com/interactive-messages
- https://api.slack.com/methods/chat.postMessage
All of the code is in one file that you can read at: microservices/bot/app/src/server.py
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.
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
- Quickstart the project from Hasura hub.
$ hasura quickstart rishi/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.
- 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.
- Go to
interactive components
in the panel on the left and enable interactive component. Add the URL as https://bot.cluster-name.hasura-app.io/confirm . Leave the options load URL empty.
- Go to
Basic Information
in the left panel and click onAdd features and functionality -> Bots
. Add a bot user and name it whatever you like.
- Go to
OAuth and Permissions
in the panel on the left. Scroll down to scopes and add the following permission 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
$ 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
If you happen to get stuck anywhere, please feel free to mail me at [email protected]. Also, if you find an error or a bug, you can raise an issue here.