Skip to content

Commit

Permalink
#741 : Deprecate Slack chat.postMessage as_user argument and allow …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
angloyna authored Oct 25, 2023
1 parent 5b627fb commit 1e79926
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
47 changes: 32 additions & 15 deletions parsons/notifications/slack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import time
import warnings

from parsons.etl.table import Table
from parsons.utilities.check_env import check
Expand All @@ -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"]

Expand All @@ -25,7 +24,6 @@ def __init__(self, api_key=None):
)

else:

self.api_key = api_key

self.client = SlackClient(self.api_key)
Expand Down Expand Up @@ -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
Expand All @@ -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
<https://api.slack.com/methods/chat.postMessage>` 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

Expand Down Expand Up @@ -221,7 +240,6 @@ def upload_file(
)

if not resp["ok"]:

if resp["error"] == "ratelimited":
time.sleep(int(resp["headers"]["Retry-After"]))

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down

0 comments on commit 1e79926

Please sign in to comment.