-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
base: dev
Are you sure you want to change the base?
Fix Slack file upload #135818
Conversation
…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.
Hey there @tkdrob, @FletcherAU, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
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.
CI is failing
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
@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: |
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.
this is exactly the same code as in sensor, should we put this in a util function?
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.
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!
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.
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]: |
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.
if this is not a dict anymore, how does L43 and follows still work?
This PR fixes the Slack integration file upload functionality.
Key Changes:
WebClient
toAsyncWebClient
, removing the deprecated run_async parameter.files_upload
withfiles_upload_v2
, adding support for per-channel uploads.The script I created contains several tests of Slack’s notification capabilities, covering text, local files, remote files, and advanced block formatting
Testing:
Further general context about why these changes are needed provided below:
files.upload
DeprecationContext: 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
toAsyncWebClient
, 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
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
.To help with the load of incoming pull requests: