You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by el...@apache.org on 2024/05/31 18:48:23 UTC
(superset) 01/01: use channel id with new slack api for file uploads
This is an automated email from the ASF dual-hosted git repository.
elizabeth pushed a commit to branch elizabeth/slack-update
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 6e1f6331c93779737c42ea94478bb9912b4cda91
Author: Elizabeth Thompson <es...@gmail.com>
AuthorDate: Fri May 31 11:48:09 2024 -0700
use channel id with new slack api for file uploads
---
superset/reports/notifications/slack.py | 15 +++++++++++++--
superset/tasks/slack_util.py | 14 ++++++++++++--
2 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/superset/reports/notifications/slack.py b/superset/reports/notifications/slack.py
index 9ecf8de6d5..bb97eb7c78 100644
--- a/superset/reports/notifications/slack.py
+++ b/superset/reports/notifications/slack.py
@@ -182,17 +182,28 @@ Error: %(text)s
try:
client = get_slack_client()
# files_upload returns SlackResponse as we run it in sync mode.
+
+ channels_list_response = client.conversations_list(types="public_channel,private_channel")
+
+ # Iterate through the channels to find the one with the matching name
+ slack_channel = next((c for c in channels_list_response["channels"] if c["name"] == channel), None)
+
+ if slack_channel is not None:
+ channel_id = slack_channel["id"]
+ else:
+ logger.warning(f"No channel named '{channel}' found")
+
if files:
for file in files:
client.files_upload_v2(
- channels=channel,
+ channels=channel_id,
file=file,
initial_comment=body,
title=title,
filetype=file_type,
)
else:
- client.chat_postMessage(channel=channel, text=body)
+ client.chat_postMessage(channel=channel_id, text=body)
logger.info(
"Report sent to slack",
extra={
diff --git a/superset/tasks/slack_util.py b/superset/tasks/slack_util.py
index 5226f50dc6..ba0552c7b4 100644
--- a/superset/tasks/slack_util.py
+++ b/superset/tasks/slack_util.py
@@ -44,19 +44,29 @@ def deliver_slack_msg(
if callable(token):
token = token()
client = WebClient(token=token, proxy=config["SLACK_PROXY"])
+ channels_list_response = client.conversations_list(types="public_channel,private_channel")
+
+ # Iterate through the channels to find the one with the matching name
+ channel = next((c for c in channels_list_response["channels"] if c["name"] == slack_channel), None)
+
+ if channel is not None:
+ channel_id = channel["id"]
+ else:
+ logger.warning(f"No channel named '{slack_channel}' found")
+
# files_upload returns SlackResponse as we run it in sync mode.
if file:
response = cast(
SlackResponse,
client.files_upload_v2(
- channels=slack_channel, file=file, initial_comment=body, title=subject
+ channels=channel_id, file=file, initial_comment=body, title=subject
),
)
assert response["file"], str(response) # the uploaded file
else:
response = cast(
SlackResponse,
- client.chat_postMessage(channel=slack_channel, text=body),
+ client.chat_postMessage(channel=channel_id, text=body),
)
assert response["message"]["text"], str(response)
logger.info("Sent the report to the slack %s", slack_channel)