
Supabase is a platform-as-a-service built on top of PostgreSQL and many other amazing open-source tools. It’s a fantastic option to create data-intensive apps and tooling. Taking advantage of the community-made Python SDK and Slack's Python SDK, you can automate tasks and build apps for several use cases.
Prerequisites
Before we dive in, let’s look at some prerequisites you'll need:
- Supabase Client
- The SDK only supports Python > 3.7. You can download a supported Python version from here.
pip install supabase
- The SDK only supports Python > 3.7. You can download a supported Python version from here.
- Python dotenv to handle API keys without exposing them in the code
- This is optional, but it will avoid issues of package dependencies and version conflicts.
pip install python-dotenv
- This is optional, but it will avoid issues of package dependencies and version conflicts.
- Slack SDK for Python
- This is needed to create the Slack bot
pip install slack-sdk
- This is needed to create the Slack bot
Creating the App in Slack
Now, it is time to create the bot in Slack and get the API Keys (after granting the required scopes)
Green arrow pointing to the ‘Create New App’ button.
Then, select to create an app from a manifest (this will already set the required permissions) for the app:
Green arrow pointing to the option ‘From an app manifest’ inside the “Create an app” menu.
Select the Slack workspace to deploy:
The picture shows a dropdown UI with Supabase selected as the workspace.
Manifest.yaml
Enter the manifest above when asked:
The picture shows the place where you have to paste the manifest file included in the repo.
Then, confirm to create the bot:
You can see a summary screen to confirm the creation of the bot and the scopes assigned.
Granting access to the bot:
Install the app on the workspace:
The picture shows the button to Install the App to the Slack Workspace.
Inviting the bot to the channels, it will post messages on:
The command to invite the bot is below:
/invite @SlackConsolidate
The picture shows the command /invite @Slackbot in the channel #team-support.
Now, we are done with Slack for now. Let's create some buffer tables in Supabase.
Creating Tables in Supabase
Create an account here (if you don't have one yet).
We will be using Supabase's database and the Python client Libraries. First, we will create one table to store the channels that are being watched and where they are going to send the message which works analogously to a multiplexer circuit. Since you may want to watch several channels but split them into a smaller buffer e.g VIP / Enterprise, etc.
Go to the SQL Editor and run this:
Adding channels to the watch list and setting the destination channel:
You may want to call conversations.list to dump all the channels and channel IDs into a CSV file, then use it to populate the table slack_channels
. You can also manually get the data, but copying the links to messages in the channels:
After right-clicking a message in Slack, you can see the option to select the link.
Slack links have the following format:
https://ORGANIZATION.slack.com/archives/ channel_id/pmessage_id
Organization: subdomain used in Slack
Channel ID: It is the string that you'll need to enter in slack channels as the channel id e.g C0000ABC02DE
The name of the channel is not needed. But it is recommended to set, it so you can filter and find this information later on if needed. You can ignore everything else when setting the table for slack_channels
Examples of adding data to the channel's list:
Method 1:
Using Supabase UI (easier):
The green arrow points to insert row button inside Supabase.
Then, enter the information as needed:
The picture shows the UI in Supabase to insert a new row to the database table.
Method 2:
Go to SQL Editor and run insert commands:
Inserting a public channel named #support-channel to be monitored:
Inserting a private channel named #support-enterprise to be monitored:
Notes:
p_level
is an optional message that will be included in with the message e.g VIP customer.
private
is an integer field that should be set to 1 if the channel has a 🔒padlock before the name (not a public channel).
channel_id
is the source channel ID and dest_channel_id
is the ID of the channel where the bot will post the message.
Setting up the environment File:
You need to get the supabase URL and API keys from here:
https://supabase.com/dashboard/project/_/settings/api
The picture illustrates where to get the URL and the service key to setup the environment variables.
Slack environment variables:
Then, copy the bot and person API Tokens for the bot:
The picture has arrows pointing to the tokens that will be used as environment variables from Slack.
Now, you have everything needed to set up the environment file. Please note that Slack ORG is the subdomain of your slack organization i.e supabase for supabase.slack.com
The environment file:
Now, we can run the bot:
It works in a similar fashion to Arduino / PIC processors of an infinity loop looking for new data and performing tasks.
Of course, we aren't doing a proper Python example if we don't make a test notebook available:
Conclusion
Using Supabase and Slack SDK, it is very easy to create a bot that consolidates data according to the rules set. It just takes some steps to get started with the Python SDK and you can even run a demo directly in Google Colab.
If you have any questions please reach out via Twitter or join our Discord.