Skip to content

[Add] Implement Slack Socket Mode support #1436

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 9 commits into
base: main
Choose a base branch
from

Conversation

JCMarques15
Copy link

Description

This PR introduces support for Slack Socket Mode, enabling real-time message processing using websockets. This feature enhances the Slack integration by allowing for more efficient and responsive communication, and a way to bypass ingress restrictive networks.

Changes

  • Added Slack Socket Mode handler in server.py
  • Created start_socket_mode function in slack/app.py
  • Implemented conditional logic to use Socket Mode when SLACK_WEBSOCKET_TOKEN is set
  • Added an example script slack_websocket_test.py for testing the Slack websocket handler

How to Test

  1. Set the following environment variables:
    • SLACK_BOT_TOKEN
    • SLACK_SIGNING_SECRET
    • SLACK_WEBSOCKET_TOKEN
  2. Run the application and verify that the Slack integration connects using Socket Mode
  3. Test sending messages through Slack and ensure they are processed correctly

Additional Notes

  • The existing HTTP handler remains active when only SLACK_BOT_TOKEN and SLACK_SIGNING_SECRET are provided
  • When SLACK_WEBSOCKET_TOKEN is set, the application will prioritize Socket Mode over the HTTP handler

- Add Slack Socket Mode handler in server.py
- Create start_socket_mode function in slack/app.py
- Add example script for testing Slack websocket handler

Introduce Slack Socket Mode support alongside existing HTTP handler.
This allows for real-time message processing using websockets when
SLACK_WEBSOCKET_TOKEN is set. The HTTP handler remains active when
only SLACK_BOT_TOKEN and SLACK_SIGNING_SECRET are provided.
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. backend Pertains to the Python backend. enhancement New feature or request labels Oct 15, 2024
JCMarques15 and others added 4 commits October 15, 2024 13:52
- Update comment to mention "SLACK_WEBSOCKET_TOKEN" instead of
  "SLACK_APP_TOKEN"

Fix a typo in the comment describing the environment variable check
for the Slack socket handler. This change aligns the comment with
the actual code implementation, improving code readability and
preventing potential confusion.
- Update docstring to mention "SLACK_WEBSOCKET_TOKEN" instead of
  "SLACK_APP_TOKEN"

Correct the documentation in the `start_socket_mode` function to
accurately reflect the environment variable used for Slack
authentication. This change aligns the docstring with the actual
code implementation, improving clarity and preventing potential
confusion for developers.
@dokterbob dokterbob added unit-tests Has unit tests. and removed unit-tests Has unit tests. labels Nov 6, 2024
Copy link
Collaborator

@dokterbob dokterbob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JCMarques15 This is a great feature, thanks for the contrib!

I would really like to have at least basic unit test coverage here. That way, we can ensure that Slack integration stays stable going forward. Please let me know whether you're willing to give that a try! I recommend using Claude Sonnet to help along if you get stuck (but I'm happy to offer support as well!).

@dokterbob dokterbob added the blocked Awaiting update or feedback from user after initial review/comments. label Nov 6, 2024
@JCMarques15
Copy link
Author

Hi @dokterbob, sorry for the late reply—it’s been a hectic few weeks!

Thanks for the detailed suggestions; they’re super helpful. I’ll give the unit testing a try and work on monkeypatching AsyncSocketModeHandler and AsyncApp to cover both legacy and websocket behavior. I’m still getting familiar with this kind of setup, so it might take me a bit of time, but I’m happy to dive in.

If I hit any roadblocks or it ends up taking longer than expected, I’ll focus on submitting a PR for the cookbook and documentation instead. Let me know if there’s anything else I should keep in mind as I get started!

@willydouhard
Copy link
Collaborator

What is the status on this?

@dokterbob
Copy link
Collaborator

@JCMarques15 Friendly poke. :)

@JCMarques15
Copy link
Author

My apologies for the delay. The end and beginning of the year have been quite demanding work-wise, and I wasn’t able to find the time to address this earlier.

I’ve now picked it up and am actively working on it. You can expect an update by the end of next week. Thank you for your patience!

- Add `node_modules` to ignored files
- Add `.pnpm-store` to ignored files

Update .gitignore to exclude node package manager related directories to keep
repository clean and prevent committing dependencies.
- Remove `slack_websocket_test.py` from backend/chainlit directory
- Relocate example to Cookbook repository with other test examples

Consolidated example files by moving Slack websocket test to the Cookbook
repository where other test examples are located for better organization and
discoverability.
- Create test file `test_slack_socket_mode.py` with integration tests
- Add test for socket mode handler initialization and startup
- Add test for HTTP route registration with classic tokens

Adds comprehensive tests for Slack integration modes, covering both WebSocket and HTTP endpoints. Tests verify proper handler initialization,
token usage, and route registration.
@JCMarques15
Copy link
Author

Hi @dokterbob I have made the changes you had requested.

I’ve moved the testing script to the cookbook as a minimal Slack bot integration example and added documentation for the new behaviour in the docs repo.

Additionally, I’ve added unit tests for both the legacy and websocket Slack behaviours using AsyncSocketModeHandler and AsyncApp with mocks, as suggested. These tests assert that Slack functions are properly called and simulate relevant callback hooks.

I believe I’ve covered everything you outlined, but I’d really appreciate it if you could take a quick look to confirm whether this aligns with your expectations or if anything still needs to be adjusted.

The relevant PRs are:
cookbook repository: #195
docs repository: #230

@JCMarques15 JCMarques15 requested a review from dokterbob May 11, 2025 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Pertains to the Python backend. blocked Awaiting update or feedback from user after initial review/comments. enhancement New feature or request size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants