From 9f8626fa1f0ca3557344159183ae288b7a804ddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Carlos=20de=20Novais=20Marques?= Date: Wed, 30 Apr 2025 11:48:45 +0000 Subject: [PATCH 1/2] [Add] Slack integration example with websocket handler - Create `.env.example` with Slack token configuration - Add basic Slack websocket handler in `app.py` - Include requirements.txt with chainlit dependency Implements a simple Slack integration example using chainlit to demonstrate websocket handling. The example shows how to process messages from Slack users and respond back with acknowledgment including the user's email. --- slack/.env.example | 3 +++ slack/app.py | 18 ++++++++++++++++++ slack/requirements.txt | 1 + 3 files changed, 22 insertions(+) create mode 100644 slack/.env.example create mode 100644 slack/app.py create mode 100644 slack/requirements.txt diff --git a/slack/.env.example b/slack/.env.example new file mode 100644 index 00000000..7d249750 --- /dev/null +++ b/slack/.env.example @@ -0,0 +1,3 @@ +SLACK_BOT_TOKEN= +SLACK_SIGNING_SECRET= +SLACK_WEBSOCKET_TOKEN= diff --git a/slack/app.py b/slack/app.py new file mode 100644 index 00000000..1df6e1e2 --- /dev/null +++ b/slack/app.py @@ -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() diff --git a/slack/requirements.txt b/slack/requirements.txt new file mode 100644 index 00000000..f3a94bfc --- /dev/null +++ b/slack/requirements.txt @@ -0,0 +1 @@ +chainlit From 1a43e37bf7409918631f479be18a978224d2ac71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Carlos=20de=20Novais=20Marques?= Date: Wed, 30 Apr 2025 11:59:01 +0000 Subject: [PATCH 2/2] [Add] Slack Socket Mode example documentation - Create readme.md for Slack Socket Mode integration - Document prerequisites, setup, and troubleshooting - Add project layout and quick start guide - Include detailed explanation of how the integration works Adds comprehensive documentation for the Slack Socket Mode example, including setup instructions, requirements, and troubleshooting tips. The documentation covers token configuration, project structure, and implementation details for connecting Chainlit to Slack via WebSockets. --- slack/readme.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 slack/readme.md diff --git a/slack/readme.md b/slack/readme.md new file mode 100644 index 00000000..6d532a93 --- /dev/null +++ b/slack/readme.md @@ -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 john.doe@example.com, 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.