Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send a Slack DM when user is not in channel #1144

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions engine/apps/slack/models/slack_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,29 +212,7 @@ def send_slack_notification(self, user, alert_group, notification_policy):

if slack_user_identity.slack_id not in channel_members:
time.sleep(5) # 2 messages in the same moment are ratelimited by Slack. Dirty hack.
result = sc.api_call(
"chat.postMessage",
channel=channel_id,
text=f":warning: Tried to ask {user_verbal} to look at incident. "
f"Unfortunately {user_verbal} is not in this channel. Please, invite.",
)
SlackMessage(
slack_id=result["ts"],
organization=self.organization,
_slack_team_identity=self.slack_team_identity,
channel_id=channel_id,
alert_group=alert_group,
).save()
UserNotificationPolicyLogRecord(
author=user,
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_FAILED,
notification_policy=notification_policy,
alert_group=alert_group,
reason="User is not in Slack channel",
notification_step=notification_policy.step,
notification_channel=notification_policy.notify_by,
notification_error_code=UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_IN_SLACK_USER_NOT_IN_CHANNEL,
).save()
slack_user_identity.send_link_to_slack_message(slack_message)
except SlackAPITokenException as e:
print(e)
except SlackAPIException as e:
Expand Down
43 changes: 43 additions & 0 deletions engine/apps/slack/models/slack_user_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,49 @@ class Meta:
def __str__(self):
return self.slack_login

def send_link_to_slack_message(self, slack_message):
blocks = [
{
"type": "section",
"text": {
"type": "plain_text",
"text": "You are invited to look at an alert group!",
"emoji": True,
},
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {"type": "plain_text", "text": "➡️ Go to the alert group"},
"url": slack_message.permalink,
"style": "primary",
}
],
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": (
f"You received this message because you're not a member of <#{slack_message.channel_id}>.\n"
"Please join the channel to get notified right in the alert group thread."
),
}
],
},
]

sc = SlackClientWithErrorHandling(self.slack_team_identity.bot_access_token)
return sc.api_call(
"chat.postMessage",
channel=self.im_channel_id,
text="You are invited to look at an alert group!",
blocks=blocks,
)

@property
def slack_verbal(self):
return (
Expand Down