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.


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. 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.
  name: { APP_NAME }
    display_name: { APP_NAME }
    always_online: false
      - im:history
      - app_mentions:read
      - channels:read
      - chat:write
      - files:read
      - files:write
      - im:history
      - im:read
      - im:write
      - users:read
      - channels:history
    request_url: https://{ CHAINLIT_APP_HOST }/slack/events
      - app_home_opened
      - app_mention
    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

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


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


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:
import chainlit as cl

async def on_message(msg: cl.Message):
    # Access the original slack event
    # Access the slack 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 -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

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