Skip to content
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

Open
wants to merge 5 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!).

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()
Copy link
Collaborator

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:

  1. 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!
  2. Unit tests. It would be absolutely amazing to have a unit test for Slack -- both the legacy behaviour as well as websocket behaviour.
  3. 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:

  1. Whether all Slack functions are properly called by calling assertions on the mock.
  2. 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.

@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!

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.

2 participants