From 7e9ac42113106851f01876686cc4231d0d62b7f7 Mon Sep 17 00:00:00 2001 From: petechd Date: Tue, 17 Dec 2024 13:11:22 +0000 Subject: [PATCH] Slack channel changes --- scripts/slack_notification.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/scripts/slack_notification.py b/scripts/slack_notification.py index 82947f1..3301a8f 100644 --- a/scripts/slack_notification.py +++ b/scripts/slack_notification.py @@ -50,6 +50,26 @@ def parse_environment_variables(): } +def get_channel_id(client, channel_name): + try: + conversation_data = client.conversations_list() + if not conversation_data.get("ok", False): + print("Failed to fetch channels") + sys.exit(2) + + channels = conversation_data.get("channels", []) + for channel in channels: + if channel.get("name") == channel_name: + return channel.get("id") + + print(f"Channel: '{channel_name}' not found") + sys.exit(1) + + except SlackApiError as e: + print(f'Error fetching channel list \nError: {e.response["error"]}') + sys.exit(2) + + def post_slack_notification( slack_auth_token, slack_channel, @@ -60,11 +80,11 @@ def post_slack_notification( title, ): client = slack.WebClient(token=slack_auth_token) - + slack_channel_id = get_channel_id(client, slack_channel) try: if content: response = client.files_upload_v2( - channel=slack_channel, + channel=slack_channel_id, content=content, title=title, initial_comment=initial_comment, @@ -72,7 +92,7 @@ def post_slack_notification( else: response = client.files_upload_v2( - channel=slack_channel, + channel=slack_channel_id, title=title, initial_comment=initial_comment, )