-
Notifications
You must be signed in to change notification settings - Fork 963
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
[Add] Implement Slack Socket Mode support #1436
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!).
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() |
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.
It's really great that you've added this script to facilitate testing, but this really isn't the place for it!
Rather, we have several designated places for such functionality:
- The chainlit cookbook. It would be great to have a minimal example of slack bot integration there! In addition, it's also a great guidance for users to get started with this!
- Unit tests. It would be absolutely amazing to have a unit test for Slack -- both the legacy behaviour as well as websocket behaviour.
- E2E tests, they live in
https://github.com/Chainlit/chainlit/tree/main/cypress/e2e
. But I don't think it's relevant for Slack as I don't think there's a browser UX in this case!
The way to test this is to monkeypatch chainlit.slack.app.AsyncSocketModeHandler
and AsyncApp
with spec=AsyncSocketModeHandler
and AsyncApp
, respectively.
You could then test:
- Whether all Slack functions are properly called by calling assertions on the mock.
- Where relevant, manually call Slack's callback hooks as if the user sends messages.
For now, just having any testing for Slack integration would be a huuuuuuge contrib. If you can't make it, that's okay, just remove this test file and file a PR for the cookbook with it, as well as a PR for the docs discussing this new behaviour.
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! |
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