Skip to content

[examples] Add Slack Socket Mode integration demo #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions slack/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SLACK_BOT_TOKEN=
SLACK_SIGNING_SECRET=
SLACK_WEBSOCKET_TOKEN=
18 changes: 18 additions & 0 deletions slack/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This is a simple example to test the slack websocket handler.
# To initiate the websocket dont forget to set the variables:
# - SLACK_BOT_TOKEN
# - SLACK_SIGNING_SECRET
# - SLACK_WEBSOCKET_TOKEN <- this one dictates if websocket or http handler

from chainlit import Message, on_message, user_session


@on_message
async def main(message: Message):
client_type = user_session.get("client_type")
if client_type == "slack":
user_email = user_session.get("user").metadata.get("email")
print(f"Received a message from: {user_email}")
await Message(
content=f"Hi {user_email}, I have received the following message:\n{message.content}",
).send()
79 changes: 79 additions & 0 deletions slack/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Slack Socket Mode Example

A minimal, self‑contained demo that shows how to connect a **Chainlit** app to Slack via **Socket Mode** (WebSockets). It echoes each user message and greets the sender by e‑mail.

---

## Prerequisites

| Requirement | Notes |
|-------------|-------|
| Python ≥ 3.9 | Any recent 3.x works. |
| Slack workspace | You need permission to create and install an app. |
| Slack app tokens | *Bot* token (`SLACK_BOT_TOKEN`), *Socket‑mode* token (`SLACK_WEBSOCKET_TOKEN`), and *Signing Secret* (`SLACK_SIGNING_SECRET`). |

> **Tip:** In the Slack dashboard, enable **Socket Mode** and add the *`connections:write`* scope to generate the websocket token.

---

## Project layout

```
examples/slack_socket_mode/
│ .env.example # placeholder for the three Slack tokens
│ app.py # Chainlit echo bot
│ requirements.txt # exact Python deps
└ README.md # this file
```

---

## Quick start

```bash
# 1 – enter the example directory (from repo root)
cd examples/slack_socket_mode

# 2 – create and activate a virtualenv (optional but recommended)
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate

# 3 – install deps
pip install -r requirements.txt

# 4 – add your real tokens
cmp .env.example .env # then edit .env

# 5 – run the bot
python app.py
```

Invite the bot to a channel or send it a direct message in Slack. It should reply with something like:

> *Hi [email protected], I have received the following message:* **hello!**

---

## How it works

`app.py` spins up a Chainlit application and, if `SLACK_WEBSOCKET_TOKEN` is present, it starts Slack’s `AsyncSocketModeHandler`. Messages received over the websocket are handled in the `@on_message` function, which:

1. Detects the client is Slack (`client_type == "slack"`).
2. Reads the sender’s e‑mail from the metadata.
3. Sends an acknowledgement back to the same channel.

The HTTP event‑based handler remains available when only `SLACK_BOT_TOKEN` & `SLACK_SIGNING_SECRET` are defined.

---

## Troubleshooting

* **`xoxb-*** or **`xapp-*** token invalid** – make sure the app is installed in the workspace and the token is copied correctly.
* **Bot doesn’t respond** – check that it’s added to the channel and has the chat:write scope.
* **Firewall / proxy issues** – Socket Mode uses outbound WebSocket connections on port 443; ensure they’re not blocked.

---

## License

This example inherits the repository’s license.
1 change: 1 addition & 0 deletions slack/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chainlit