This documentation describes the integration of MindsDB with Slack, a cloud-based collaboration platform. The integration allows MindsDB to access data from Slack and enhance Slack with AI capabilities.

Prerequisites

Before proceeding, ensure the following prerequisites are met:

  1. Install MindsDB locally via Docker or Docker Desktop.
  2. To connect Slack to MindsDB, install the required dependencies following this instruction.
  3. Install or ensure access to Slack.

Connection

Establish a connection to Slack from MindsDB by executing the following SQL command and providing its handler name as an engine.

CREATE DATABASE slack_conn
WITH ENGINE = 'slack', 
PARAMETERS = {
   "token": "values",     -- required parameter
   "app_token": "values"  -- optional parameter
};

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 into a Slack channel.

Method 1: Chatbot responds in direct messages to a Slack app

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.

If you want to use Slack in the CREATE CHATBOT syntax, use this method of connecting Slack to MindsDB.

This connection method enables you to chat directly with an app via Slack.

Alternatively, you can connect an app to the Slack channel:

  • 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.

Here is how to connect Slack to MindsDB:

CREATE DATABASE mindsdb_slack
WITH
  ENGINE = 'slack',
  PARAMETERS = {
      "token": "xoxb-...",
      "app_token": "xapp-..."
    };

It comes with the conversations and messages tables.

Method 2: Chatbot responds on a defined Slack channel

Another way to connect to 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 connect Slack to MindsDB:

CREATE DATABASE mindsdb_slack
WITH
  ENGINE = 'slack',
  PARAMETERS = {
      "token": "xoxb-..."
    };

It comes with the conversations and messages tables.

Usage

The following usage applies when Connection Method 2 was used to connect Slack.

See the usage for Connection Method 1 via the CREATE CHATBOT syntax.

You can select all messages from a conversation using the below query.

SELECT *
FROM mindsdb_slack.messages
WHERE channel_id="<channel-id>";

To find the channel ID of a conversation, you can use the conversations table:

SELECT *
FROM mindsdb_slack.conversations
WHERE name = "<channel-name>";

Please note that if your workspace has more than 1000 conversations, you may need to use the LIMIT clause to retrieve all conversations. More information on this can be found below.

You can also find the channel ID by right-clicking on the conversation in Slack, selecting ‘View conversation details’ or ‘View channel details,’ and copying the channel ID from the bottom of the ‘About’ tab.

Also, you can post messages to the conversation like this:

INSERT INTO mindsdb_slack.messages (channel_id, text)
VALUES("<channel-id>", "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.messages
WHERE channel_id = "<channel-id>"
AND ts = "1688863707.197229";

Let’s select 10 messages created after the specified timestamp:

SELECT *
FROM mindsdb_slack.messages
WHERE channel_id = "<channel-id>"
AND message_created_at > '2023-07-25 00:13:07'
LIMIT 10;

We can also order the selected messages:

SELECT *
FROM mindsdb_slack.messages
WHERE channel_id = "<channel-id>"
ORDER BY created_at ASC;
LIMIT 5;

List all conversations by selecting from the conversations table.

SELECT *
FROM mindsdb_slack.conversations;

The conversations table will return the first 1000 conversations by default. To retrieve more, use the LIMIT clause.

When using the LIMIT clause to query additional conversations, you may encounter Slack API rate limits.

You can also filter conversations by providing the channel ID:

SELECT *
FROM mindsdb_slack.conversations
WHERE channel_id = "<channel-id>";

To select multiple conversations, provide a list of channel IDs in an IN clause:

SELECT *
FROM mindsdb_slack.conversations
WHERE channel_id IN ("<channel-id-1>", "<channel-id-2>");

Next Steps

Follow this tutorial to build an AI agent with MindsDB.