Skip to content

Commit c93ee5c

Browse files
authored
Send a Slack DM when user is not in channel (#1144)
# What this PR does Currently, when a user gets mentioned in an alert group thread and the user is not in the Slack channel, the Slack bot sends the following to the channel: > ⚠️ Tried to ask USER to look at incident. Unfortunately USER is not in this channel. Please, invite. This PR changes this behaviour to instead send a direct message to the user. The message contains a link to the main alert group message in Slack. <img width="806" alt="Screenshot 2023-01-17 at 19 25 36" src="https://user-images.githubusercontent.com/20116910/212996457-02db183f-2041-4998-b743-bd5b6c84b7b5.png"> ## Checklist - [ ] Tests updated (N/A) - [ ] Documentation added (N/A) - [x] `CHANGELOG.md` updated
1 parent 90def88 commit c93ee5c

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Allow messaging backends to be enabled/disabled per organization ([#1151](https://github.com/grafana/oncall/pull/1151))
1313

14+
### Changed
15+
16+
- Send a Slack DM when user is not in channel ([#1144](https://github.com/grafana/oncall/pull/1144))
17+
1418
## v1.1.17 (2023-01-18)
1519

1620
### Changed

engine/apps/slack/models/slack_message.py

+1-23
Original file line numberDiff line numberDiff line change
@@ -212,29 +212,7 @@ def send_slack_notification(self, user, alert_group, notification_policy):
212212

213213
if slack_user_identity.slack_id not in channel_members:
214214
time.sleep(5) # 2 messages in the same moment are ratelimited by Slack. Dirty hack.
215-
result = sc.api_call(
216-
"chat.postMessage",
217-
channel=channel_id,
218-
text=f":warning: Tried to ask {user_verbal} to look at incident. "
219-
f"Unfortunately {user_verbal} is not in this channel. Please, invite.",
220-
)
221-
SlackMessage(
222-
slack_id=result["ts"],
223-
organization=self.organization,
224-
_slack_team_identity=self.slack_team_identity,
225-
channel_id=channel_id,
226-
alert_group=alert_group,
227-
).save()
228-
UserNotificationPolicyLogRecord(
229-
author=user,
230-
type=UserNotificationPolicyLogRecord.TYPE_PERSONAL_NOTIFICATION_FAILED,
231-
notification_policy=notification_policy,
232-
alert_group=alert_group,
233-
reason="User is not in Slack channel",
234-
notification_step=notification_policy.step,
235-
notification_channel=notification_policy.notify_by,
236-
notification_error_code=UserNotificationPolicyLogRecord.ERROR_NOTIFICATION_IN_SLACK_USER_NOT_IN_CHANNEL,
237-
).save()
215+
slack_user_identity.send_link_to_slack_message(slack_message)
238216
except SlackAPITokenException as e:
239217
print(e)
240218
except SlackAPIException as e:

engine/apps/slack/models/slack_user_identity.py

+43
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,49 @@ class Meta:
9292
def __str__(self):
9393
return self.slack_login
9494

95+
def send_link_to_slack_message(self, slack_message):
96+
blocks = [
97+
{
98+
"type": "section",
99+
"text": {
100+
"type": "plain_text",
101+
"text": "You are invited to look at an alert group!",
102+
"emoji": True,
103+
},
104+
},
105+
{
106+
"type": "actions",
107+
"elements": [
108+
{
109+
"type": "button",
110+
"text": {"type": "plain_text", "text": "➡️ Go to the alert group"},
111+
"url": slack_message.permalink,
112+
"style": "primary",
113+
}
114+
],
115+
},
116+
{
117+
"type": "context",
118+
"elements": [
119+
{
120+
"type": "mrkdwn",
121+
"text": (
122+
f"You received this message because you're not a member of <#{slack_message.channel_id}>.\n"
123+
"Please join the channel to get notified right in the alert group thread."
124+
),
125+
}
126+
],
127+
},
128+
]
129+
130+
sc = SlackClientWithErrorHandling(self.slack_team_identity.bot_access_token)
131+
return sc.api_call(
132+
"chat.postMessage",
133+
channel=self.im_channel_id,
134+
text="You are invited to look at an alert group!",
135+
blocks=blocks,
136+
)
137+
95138
@property
96139
def slack_verbal(self):
97140
return (

0 commit comments

Comments
 (0)