oncall-engine/engine/apps/slack/utils.py
Joey Orlando f77a54b518
Shift Swap Requests in Slack + improve typing for Slack django app (#2653)
# What this PR does

**Shift Swap Requests**

https://www.loom.com/share/860c3337b338412cbd2ac4024260f3e8?sid=3d91b558-b4de-4351-8b45-8a99b7302346

**Other**
- Drastically improve the typing in the `slack` Django app, and several
other models/functions that were consumed by logic within the `slack`
Django app (ex. setting `RelatedManager` type hints on various models)
https://www.loom.com/share/da6b9984519c48d59a45d3c93c08d7dc

## 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-07-28 15:11:38 +00:00

34 lines
1.3 KiB
Python

import typing
from datetime import datetime
from apps.slack.slack_client import SlackClientWithErrorHandling
from apps.slack.slack_client.exceptions import SlackAPIException
if typing.TYPE_CHECKING:
from apps.user_management.models import Organization
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.api_call("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="date_short") -> str:
fallback = datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M (UTC)")
return f"<!date^{timestamp}^{{{format}}} {{time}}|{fallback}>"
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}"