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
This commit is contained in:
Vadim Stepanov 2023-01-18 16:08:15 +00:00 committed by GitHub
parent 90def88752
commit c93ee5c554
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 23 deletions

View file

@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow messaging backends to be enabled/disabled per organization ([#1151](https://github.com/grafana/oncall/pull/1151))
### Changed
- Send a Slack DM when user is not in channel ([#1144](https://github.com/grafana/oncall/pull/1144))
## v1.1.17 (2023-01-18)
### Changed

View file

@ -212,29 +212,7 @@ class SlackMessage(models.Model):
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:

View file

@ -92,6 +92,49 @@ class SlackUserIdentity(models.Model):
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 (