To make your Chainlit app available on Slack, you will need to create a Slack app and set up the necessary environment variables.

How it Works

The Slack bot will listen to messages mentioning it in channels and direct messages. It will send replies to a dedicated thread or DM depending on the context.

Preview

Supported Features

MessageStreamingElementsAudioAsk UserChat HistoryChat ProfilesFeedback

Install the Slack Bolt Library

The Slack Bolt library is not included in the Chainlit dependencies. You will have to install it manually.

pip install slack_bolt

Create a Slack App

To start, navigate to the Slack apps dashboard for the Slack API. Here, you should find a green button that says Create New App. When you click this button, select the option to create your app from scratch.

Create a name for your bot, such as “ChainlitDemo”. Select the workspace you would like your bot to exist in.

Create a Slack App

Working Locally

If you are working locally, you will have to expose your local Chainlit app to the internet to receive incoming messages to Slack. You can use ngrok for this.

ngrok http 8000

This will give you a public URL that you can use to set up the app manifest. Do not forget to replace it once you deploy Chainlit to a public host.

Set the App Manifest

Go to App Manifest and paste the following Yaml.

Replace the {placeholders} with your own values.
display_information:
  name: { APP_NAME }
features:
  bot_user:
    display_name: { APP_NAME }
    always_online: false
oauth_config:
  scopes:
    user:
      - im:history
      - channels:history
    bot:
      - app_mentions:read
      - channels:read
      - chat:write
      - files:read
      - files:write
      - im:history
      - im:read
      - im:write
      - users:read
      - users:read.email
      - channels:history
      - groups:history
settings:
  event_subscriptions:
    request_url: https://{ CHAINLIT_APP_HOST }/slack/events
    bot_events:
      - app_home_opened
      - app_mention
      - message.im
  interactivity:
    is_enabled: true
    request_url: https://{ CHAINLIT_APP_HOST }/slack/events
  org_deploy_enabled: false
  socket_mode_enabled: false
  token_rotation_enabled: false

Click on Save Changes.

Set the App Manifest

You will see a warning stating that the URL is not verified. You can ignore this for now.

[Optional] Allow users to send DMs to Chainlit

By default the app will only listen to mentions in channels.

If you want to allow users to send direct messages to the app, go to App Home and enable “Allow users to send Slash commands and messages from the messages tab”.

Allow DMs

Install the Slack App to Your Workspace

Navigate to the Install App tab and click on Install to Workspace.

Set the Environment Variables

Set the environment variables outside of your application code.

Bot Token

Once the slack application is installed, you will see the Bot User OAuth Token. Set this as an environment variable in your Chainlit app.

Copy the Bot Token

SLACK_BOT_TOKEN=your_bot_token

Signing Secret

Navigate to the Basic Information tab and copy the Signing Secret. Then set it as an environment variable in your Chainlit app.

Copy the Signing Secret

SLACK_SIGNING_SECRET=your_signing_secret

Start the Chainlit App

Since the Chainlit app is not running, the Slack app will not be able to communicate with it.

For the example, we will use this simple app:

my_app.py
import chainlit as cl

@cl.on_message
async def on_message(msg: cl.Message):
    # Access the original slack event
    print(cl.user_session.get("slack_event"))
    # Access the slack user
    print(cl.user_session.get("user"))

    # Access potential attached files
    attached_files = msg.elements

    await cl.Message(content="Hello World").send()

Reminder: Make sure the environment variables are set and that your local chainlit app is exposed to the internet via ngrok.

Start the Chainlit app:

chainlit run my_app.py -h

Using -h to not open the default Chainlit UI since we are using Slack.

You should now be able to interact with your Chainlit app through Slack.

Chat History

Chat history is directly available through the fetch_slack_message_history method. It will fetch the last messages from the current thread or DM channel.

import chainlit as cl
import discord


@cl.on_message
async def on_message(msg: cl.Message):
    fetch_slack_message_history = cl.user_session.get("fetch_slack_message_history")

    if fetch_slack_message_history:
        print(await fetch_slack_message_history(limit=10))

    # Your code here