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 use MindsDB Cloud.
  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.

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 channels and channel_lists table.

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 channels and channel_lists table.

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 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;

List all channels by selecting from the channel_lists table.

SELECT *
FROM mindsdb_slack.channel_lists;

Next Steps

Follow this tutorial to see more use case examples.