-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[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
base: main
Are you sure you want to change the base?
Conversation
- 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.
- 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.
There was a problem hiding this 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!).
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! |
What is the status on this? |
@JCMarques15 Friendly poke. :) |
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.
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 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: |
@JCMarques15 @dokterbob What is the status on this ? |
Hi @gowtham3105! The PR is currently waiting for final review from the maintainers. I've added the requested unit tests and documentation, so from a technical standpoint it should be ready to go. @dokterbob when you have a moment, would you be able to take a look? Happy to make any additional adjustments if needed. Thanks for your interest in this feature! |
This PR is pending for review since quite some time, can one of you please have a look ? |
@JCMarques15 Can you please fix the failing test and linting issues? |
…t formatting Remove unused asyncio import and standardize comment spacing in test_slack_socket_mode.py
Replace string-based patch with object-based patch to avoid lazy import registry issues. The test was failing in CI/CD with KeyError: 'slack' because the patch() function tried to access chainlit.slack through the lazy import registry, but slack is not registered there. This fix imports the module directly then patches the object, bypassing the registry entirely. Fixes the test while maintaining the same test behavior and assertions.
Hi @asvishnyakov. I have dealt with the unused import and weird space character. For the test error: ProblemThe Root CauseThe issue was with the lazy import registry system. When using: with patch("chainlit.slack.app.AsyncSocketModeHandler", autospec=True): The SolutionI changed the test to use object-based patching instead of string-based patching: # Import the module first
slack_app_mod = importlib.import_module("chainlit.slack.app")
# Patch the object directly
with patch.object(slack_app_mod, "AsyncSocketModeHandler", autospec=True) as handler_cls: This bypasses the lazy import registry entirely by importing the module directly via An alternative would be to add I hope this fixes everything as requested. |
Add blank line after import and break long conditional statement across multiple lines for better readability
Apologies, I hadn’t realized Ruff was being used as the formatter. My local setup was applying Black by mistake. I’ve corrected the formatting in the affected file, and it should now pass all CI/CD checks. |
@asvishnyakov Can you please check the changes? |
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
server.py
start_socket_mode
function inslack/app.py
SLACK_WEBSOCKET_TOKEN
is setslack_websocket_test.py
for testing the Slack websocket handlerHow to Test
SLACK_BOT_TOKEN
SLACK_SIGNING_SECRET
SLACK_WEBSOCKET_TOKEN
Additional Notes
SLACK_BOT_TOKEN
andSLACK_SIGNING_SECRET
are providedSLACK_WEBSOCKET_TOKEN
is set, the application will prioritize Socket Mode over the HTTP handler