Slack
In this section, we present how to connect Slack to MindsDB.
Slack is a cloud-based collaboration platform that is designed to facilitate communication and teamwork within organizations and teams. It offers a variety of features that enable users to chat, share files, collaborate on projects, and integrate with other tools and services.
The Slack handler provides interfaces to connect with Slack via APIs and perform various tasks such as sending messages, retrieving conversation history, and more.
Connection
This handler was implemented using slack-sdk
library, which is a Python Library offering a package for each of Slack’s APIs.
The Slack handler is initialized with the following parameters:
token
is a Slack bot token to use for authentication.app_token
is a Slack app token to use for authentication.
Please note that app_token
is an optional parameter. Without providing it, you need to integrate an app to a Slack channel.
Method 1
One way to connect Slack is to use both bot and app tokens. By following the instructions below, you’ll set up the Slack app and be able to message this Slack app directly to chat with the bot.
Here is how to set up a Slack app and generate both Slack bot token and Slack app token:
- Follow this link and sign in with your Slack account.
- Create a new app
From scratch
or select an existing app.- Please note that the following instructions support apps created
From scratch
. - For apps created
From an app manifest
, please follow the Slack docs here.
- Please note that the following instructions support apps created
- Go to Basic Information under Settings.
- Under App-Level Tokens, click on Generate Token and Scopes.
- Name the token
socket
and add theconnections:write
scope. - Copy and save the
xapp-...
token - you’ll need it to publish the chatbot.
- Go to Socket Mode under Settings and toggle the button to Enable Socket Mode.
- Go to OAuth & Permissions under Features.
- Add the following Bot Token Scopes:
- channels:history
- channels:read
- chat:write
- groups:read
- im:history
- im:read
- im:write
- mpim:read
- users.profile:read
- In the OAuth Tokens for Your Workspace section, click on Install to Workspace and then Allow.
- Copy and save the
xoxb-...
token - you’ll need it to publish the chatbot.
- Add the following Bot Token Scopes:
- Go to App Home under Features and click on the checkbox to Allow users to send Slash commands and messages from the messages tab.
- Go to Event Subscriptions under Features.
- Toggle the button to Enable Events.
- Under Subscribe to bot events, click on Add Bot User Event and add
message.im
. - Click on Save Changes.
- Now you can use tokens from points 3 and 5 to initialize the Slack handler in MindsDB.
If you installed MindsDB locally via pip, you need to install all handler dependencies manually. To do so, go to the handler’s folder (mindsdb/integrations/handlers/slack_handler) and run this command: pip install -r requirements.txt
.
Here is how to connect Slack to MindsDB:
CREATE DATABASE mindsdb_slack
WITH
ENGINE = 'slack',
PARAMETERS = {
"token": "xoxb-...",
"app_token": "xapp-..."
};
Method 2
Another way to connect Slack is to use the bot token only. By following the instructions below, you’ll set up the Slack app and integrate it into one of the channels from which you can directly chat with the bot.
Here is how to set up a Slack app and generate a Slack bot token:
- Follow this link and sign in with your Slack account.
- Create a new app
From scratch
or select an existing app.- Please note that the following instructions support apps created
From scratch
. - For apps created
From an app manifest
, please follow the Slack docs here.
- Please note that the following instructions support apps created
- Go to the OAuth & Permissions section.
- Under the Scopes section, add the Bot Token Scopes necessary for your application. You can add more later as well.
- channels:history
- channels:read
- chat:write
- groups:read
- im:read
- mpim:read
- Install the bot to your workspace.
- Under the OAuth Tokens for Your Workspace section, copy the the Bot User OAuth Token value.
- Open your Slack application and add the App/Bot to one of the channels:
- Go to the channel where you want to use the bot.
- Right-click on the channel and select View Channel Details.
- Select Integrations.
- Click on Add an App.
- Now you can use the token from step 6 to initialize the Slack handler in MindsDB, and use the channel name to query and write messages.
If you installed MindsDB locally via pip, you need to install all handler dependencies manually. To do so, go to the handler’s folder (mindsdb/integrations/handlers/slack_handler) and run this command: pip install -r requirements.txt
.
Here is how to connect Slack to MindsDB:
CREATE DATABASE mindsdb_slack
WITH
ENGINE = 'slack',
PARAMETERS = {
"token": "xoxb-..."
};
It comes with the channels
table.
Usage
The following usage applies when Connection Method 2 was used to connect Slack.
The usage for Connection Method 1 is through the LLM UI. More details here.
You can select all messages from the channel using the below query.
SELECT *
FROM mindsdb_slack.channels
WHERE channel="<channel-name>";
Also, you can post messages to the channel like this:
INSERT INTO mindsdb_slack.channels (channel, text)
VALUES("<channel-name>", "Hey MindsDB, Thanks to you! Now I can respond to my Slack messages through SQL Queries.");
And you can delete messages, but only the ones posted by the bot.
DELETE FROM mindsdb_slack.channels
WHERE channel = "<channel-name>"
AND ts = "1688863707.197229";
Let’s select 10 messages created after the specified timestamp:
SELECT *
FROM mindsdb_slack.channels
WHERE channel="<channel-name>"
AND message_created_at > '2023-07-25 00:13:07'
LIMIT 10;
We can also order the selected messages:
SELECT *
FROM mindsdb_slack.channels
WHERE channel="<channel-name>"
ORDER BY messages ASC
LIMIT 5;
You can find more examples here.