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

Fix Slack file upload #135818

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from

Conversation

jsuar
Copy link

@jsuar jsuar commented Jan 17, 2025

This PR fixes the Slack integration file upload functionality.

Key Changes:

  1. Migrated from WebClient to AsyncWebClient, removing the deprecated run_async parameter.
  2. Replaced deprecated files_upload with files_upload_v2, adding support for per-channel uploads.
  3. Added a helper function _async_get_channel_id to resolve channel names to IDs.
  4. Upgraded dependency from slackclient==2.5.0 to slack_sdk==3.33.4.
  5. Improved error handling and logging for channel resolution and file uploads.

The script I created contains several tests of Slack’s notification capabilities, covering text, local files, remote files, and advanced block formatting

Testing:

  • Verified Slack authentication (auth_test).
  • Tested file uploads (local and remote) with files_upload_v2.
  • Confirmed public/private channel resolution.

Further general context about why these changes are needed provided below:

files.upload Deprecation

Context: In April 2024, Slack announced adjustments to their file upload methods, making it clear that a newer, more stable approach to uploading and managing files is here to stay. This clarification deprecates older workflows and reinforces that developers should rely on updated, officially supported methods (like files.remote.* and files.upload.v2) for future compatibility and consistent behavior. By aligning the Home Assistant Slack integration with these updated methods, we ensure the integration remains robust and compatible with Slack’s evolving platform standards.

Slack Client Upgrade

The slackclient PyPI project is in maintenance mode now and slack-sdk project is the successor. The v3 SDK provides more functionalities such as Socket Mode, OAuth flow module, SCIM API, Audit Logs API, better asyncio support, and retry handlers.

via https://tools.slack.dev/python-slack-sdk/legacy/

Proposed change

This PR updates the Slack integration to modernize its codebase and improve compatibility with the latest Slack API and SDK. Key changes include migrating from WebClient to AsyncWebClient, replacing the deprecated files_upload method with files_upload_v2, and enhancing error handling for file uploads and channel resolution. This ensures the integration is aligned with current best practices and remains functional with Slack’s evolving platform.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

…ad_v2

- Replaced deprecated WebClient with AsyncWebClient throughout the integration.
- Removed the unsupported `run_async` parameter.
- Added a helper function to resolve channel names to channel IDs.
- Updated `_async_send_local_file_message` and `_async_send_remote_file_message` to handle Slack's new API requirements, including per-channel uploads.
- Updated dependency from slackclient==2.5.0 to slack-sdk>=3.0.0.
- Improved error handling and logging for channel resolution and file uploads.
@home-assistant
Copy link

Hey there @tkdrob, @FletcherAU, mind taking a look at this pull request as it has been labeled with an integration (slack) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of slack can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign slack Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@jsuar jsuar marked this pull request as ready for review January 17, 2025 03:15
@zweckj zweckj changed the title Fix Slack file upload (resolves #121672) Fix Slack file upload Jan 17, 2025
Copy link
Member

@zweckj zweckj left a comment

Choose a reason for hiding this comment

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

CI is failing

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft January 17, 2025 09:00
@jsuar jsuar marked this pull request as ready for review January 17, 2025 16:22
@home-assistant home-assistant bot requested a review from zweckj January 17, 2025 16:22
@jsuar jsuar marked this pull request as draft January 17, 2025 16:55
@jsuar jsuar marked this pull request as ready for review January 17, 2025 17:28
@jsuar
Copy link
Author

jsuar commented Jan 17, 2025

@zweckj CI should pass now. I was able to reproduce and fix the error locally. Thanks for the catch. I wrongly assumed it was a transient issue

)
except (SlackApiError, ClientError) as err:
_LOGGER.error("Error while uploading file-based message: %r", err)
for target in targets:
Copy link
Member

Choose a reason for hiding this comment

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

this is exactly the same code as in sensor, should we put this in a util function?

Copy link
Author

Choose a reason for hiding this comment

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

You’re absolutely right, the logic is very similar, but there’s a key difference in how files_upload_v2 handles local vs. remote files. For local files, Slack’s SDK reads the file directly, while for remote files, we pass the file content explicitly.

I accounted for this in the util function by ensuring it supports both cases, allowing the calling function to handle any file preparation (e.g., reading content for remote files). While the parameters differ slightly, this approach keeps the utility reusable and ensures consistency in handling file uploads across integrations.

Local testing looks good, and all unit tests are passing as well. Let me know if you have additional thoughts!

@home-assistant home-assistant bot marked this pull request as draft January 18, 2025 09:47
@jsuar jsuar marked this pull request as ready for review January 18, 2025 19:32
@home-assistant home-assistant bot requested a review from zweckj January 18, 2025 19:32
Copy link
Member

@zweckj zweckj left a comment

Choose a reason for hiding this comment

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

no need to rebase onto dev all the time btw

@@ -57,10 +57,10 @@ async def async_step_user(

async def _async_try_connect(
self, token: str
) -> tuple[str, None] | tuple[None, dict[str, str]]:
) -> tuple[str, None] | tuple[None, AsyncSlackResponse]:
Copy link
Member

Choose a reason for hiding this comment

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

if this is not a dict anymore, how does L43 and follows still work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants