From 1e799262a140f3869f3fcd2c5936a7575417ae31 Mon Sep 17 00:00:00 2001 From: Angela Gloyna Date: Wed, 25 Oct 2023 13:02:21 -0500 Subject: [PATCH] #741 : Deprecate Slack chat.postMessage `as_user` argument and allow for new authorship arguments (#891) * remove the argument and add a warning that the usage is deprecated * remove usage of as_user from sample code * add in the user customization arguments in lieu of the deprecated as_user argument * add comment regarding the permissions required to use these arguments * use kwargs * surface the whole response * allow usage of the deprecated argument but surface the failed response better * add to retry * delete test file * fix linting * formatting to fix tests * fix if style * add warning for using thread_ts * move the documentation to the optional arguments --- parsons/notifications/slack.py | 47 +++++++++++++------ .../civis_job_status_slack_alert.py | 2 +- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/parsons/notifications/slack.py b/parsons/notifications/slack.py index 5b36aead45..ced3c20fa2 100644 --- a/parsons/notifications/slack.py +++ b/parsons/notifications/slack.py @@ -1,5 +1,6 @@ import os import time +import warnings from parsons.etl.table import Table from parsons.utilities.check_env import check @@ -12,9 +13,7 @@ class Slack(object): def __init__(self, api_key=None): - if api_key is None: - try: self.api_key = os.environ["SLACK_API_TOKEN"] @@ -25,7 +24,6 @@ def __init__(self, api_key=None): ) else: - self.api_key = api_key self.client = SlackClient(self.api_key) @@ -130,7 +128,7 @@ def message(cls, channel, text, webhook=None, parent_message_id=None): payload["thread_ts"] = parent_message_id return requests.post(webhook, json=payload) - def message_channel(self, channel, text, as_user=False, parent_message_id=None): + def message_channel(self, channel, text, parent_message_id=None, **kwargs): """ Send a message to a Slack channel @@ -140,35 +138,56 @@ def message_channel(self, channel, text, as_user=False, parent_message_id=None): an `im` (aka 1-1 message). text: str Text of the message to send. - as_user: str - Pass true to post the message as the authenticated user, - instead of as a bot. Defaults to false. See - https://api.slack.com/methods/chat.postMessage#authorship for - more information about Slack authorship. parent_message_id: str The `ts` value of the parent message. If used, this will thread the message. + **kwargs: kwargs + as_user: str + This is a deprecated argument. Use optional username, icon_url, and icon_emoji + args to customize the attributes of the user posting the message. + See https://api.slack.com/methods/chat.postMessage#legacy_authorship for + more information about legacy authorship + Additional arguments for chat.postMessage API call. See documentation + ` for more info. + + `Returns:` `dict`: A response json """ + + if "as_user" in kwargs: + warnings.warn( + "as_user is a deprecated argument on message_channel().", + DeprecationWarning, + stacklevel=2, + ) + if "thread_ts" in kwargs: + warnings.warn( + "thread_ts argument on message_channel() will be ignored. Use parent_message_id.", + Warning, + stacklevel=2, + ) + kwargs.pop("thread_ts", None) + resp = self.client.api_call( "chat.postMessage", channel=channel, text=text, - as_user=as_user, thread_ts=parent_message_id, + **kwargs, ) if not resp["ok"]: - if resp["error"] == "ratelimited": time.sleep(int(resp["headers"]["Retry-After"])) resp = self.client.api_call( - "chat.postMessage", channel=channel, text=text, as_user=as_user + "chat.postMessage", channel=channel, text=text, **kwargs ) - raise SlackClientError(resp["error"]) + resp.pop("headers", None) + + raise SlackClientError(resp) return resp @@ -221,7 +240,6 @@ def upload_file( ) if not resp["ok"]: - if resp["error"] == "ratelimited": time.sleep(int(resp["headers"]["Retry-After"])) @@ -250,7 +268,6 @@ def _paginate_request(self, endpoint, collection, **kwargs): resp = self.client.api_call(endpoint, cursor=cursor, limit=LIMIT, **kwargs) if not resp["ok"]: - if resp["error"] == "ratelimited": time.sleep(int(resp["headers"]["Retry-After"])) continue diff --git a/useful_resources/sample_code/civis_job_status_slack_alert.py b/useful_resources/sample_code/civis_job_status_slack_alert.py index 16504d838c..6c0885a49e 100644 --- a/useful_resources/sample_code/civis_job_status_slack_alert.py +++ b/useful_resources/sample_code/civis_job_status_slack_alert.py @@ -140,7 +140,7 @@ def main(): message = f"*{project_name} Status*\n{line_items}" logger.info(f"Posting message to Slack channel {SLACK_CHANNEL}") # Post message - slack.message_channel(SLACK_CHANNEL, message, as_user=True) + slack.message_channel(SLACK_CHANNEL, message) logger.info("Slack message posted")