oncall-engine/engine/apps/slack/utils.py
Joey Orlando a9155130df
update slack_sdk dependency to latest version (#2947)
# What this PR does

- update `slackclient` dependency to latest version. The version we were
using was 5 years old 😲
- first followed the v2 migration guide
[here](https://github.com/slackapi/python-slack-sdk/wiki/Migrating-to-2.x)
followed by the v3 migration guide
[here](https://slack.dev/python-slack-sdk/v3-migration/). The main
changes were:
    - The PyPI project was renamed from `slackclient` to `slack_sdk`
- it is discouraged/harder to call `api_call` and encouraged to call the
helper methods (ex. `chat_postMessage`;
[note](https://github.com/slackapi/python-slack-sdk/wiki/Migrating-to-2.x#web-client-api-changes)
in migration guide docs)
- In 1.x, a failed api call would return the error payload to you and
have you handle the error. In 2.x, a failed api call will throw an
exception. To handle this in your code, you will have to wrap api calls
with a try except block. Since we overload `WebClient.api_call` this was
an easy change and only required a one line change
- remove `apps.slack.slack_client.slack_server.SlackClientServer` class.
The new version of `slack_sdk` handles the case that we needed to
overload for in the first place.
- merged `apps/slack/slack_client/slack_client.py` and
`apps/slack/slack_client/exceptions.py` into `apps/slack/client.py`

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
2023-09-05 11:31:59 +02:00

102 lines
3.5 KiB
Python

import enum
import typing
from datetime import datetime
from apps.slack.client import SlackAPIException, SlackClientWithErrorHandling
if typing.TYPE_CHECKING:
from apps.user_management.models import Organization
class SlackDateFormat(enum.StrEnum):
"""
https://api.slack.com/reference/surfaces/formatting#date-formatting
"""
DATE_NUM = "date_num"
"""
Displayed as `2014-02-18`. It will include leading zeros before the month and date and is
probably best for more technical integrations that require a developer-friendly date format.
"""
DATE = "date"
"""
Displayed as `February 18th, 2014`. The year will be omitted if the date is less than six months in the past or future.
"""
DATE_SHORT = "date_short"
"""
Displayed as `Feb 18, 2014`. The year will be omitted if the date is less than six months in the past or future.
"""
DATE_LONG = "date_long"
"""
Displayed as `Tuesday, February 18th, 2014`. The year will be omitted if the date is less than six months in the past or future.
"""
DATE_PRETTY = "date_pretty"
"""
Displays the same as `{date}` but uses "yesterday", "today", or "tomorrow" where appropriate.
"""
DATE_SHORT_PRETTY = "date_short_pretty"
"""
Displays the same as `{date_short}` but uses "yesterday", "today", or "tomorrow" where appropriate.
"""
DATE_LONG_PRETTY = "date_long_pretty"
"""
Displays the same as `{date_long}` but uses "yesterday", "today", or "tomorrow" where appropriate.
"""
TIME = "time"
"""
Displayed as `6:39 AM` or `6:39 PM` in 12-hour format. If the client is set to show 24-hour format, it is displayed as `06:39` or `18:39`.
"""
TIME_SECS = "time_secs"
"""
Displayed as `6:39:45 AM` `6:39:42 PM` in 12-hour format. In 24-hour format it is displayed as `06:39:45` or `18:39:42`.
"""
def post_message_to_channel(organization: "Organization", channel_id: str, text: str) -> None:
if organization.slack_team_identity:
slack_client = SlackClientWithErrorHandling(organization.slack_team_identity.bot_access_token)
try:
slack_client.chat_postMessage(channel=channel_id, text=text)
except SlackAPIException as e:
if e.response["error"] == "channel_not_found":
pass
else:
raise e
def _format_datetime_to_slack(timestamp: float, format: str) -> str:
fallback = datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M (UTC)")
return f"<!date^{int(timestamp)}^{format}|{fallback}>"
def format_datetime_to_slack(timestamp: float, format: SlackDateFormat = SlackDateFormat.DATE_SHORT) -> str:
"""
See the docs [here](https://api.slack.com/reference/surfaces/formatting#date-formatting) for
more information
"""
return _format_datetime_to_slack(timestamp, f"{{{format}}}")
def format_datetime_to_slack_with_time(timestamp: float, format: SlackDateFormat = SlackDateFormat.DATE_SHORT) -> str:
"""
See the docs [here](https://api.slack.com/reference/surfaces/formatting#date-formatting) for
more information
"""
return _format_datetime_to_slack(timestamp, f"{{{format}}} {{time}}")
def get_cache_key_update_incident_slack_message(alert_group_pk: str) -> str:
CACHE_KEY_PREFIX = "update_incident_slack_message"
return f"{CACHE_KEY_PREFIX}_{alert_group_pk}"
def get_populate_slack_channel_task_id_key(slack_team_identity_id: str) -> str:
return f"SLACK_CHANNELS_TASK_ID_TEAM_{slack_team_identity_id}"