Skip to content

Commit

Permalink
Post monthly update notifications to #openprescribing
Browse files Browse the repository at this point in the history
Adds a SLACK_OP_POST_KEY setting for posting messages to
the #openprescribing channel directly. This means they don't have to
be manually picked up and reposted from #tech-noise.

The new webhook URL is already created and set in the environment
file.
  • Loading branch information
rebkwok committed Sep 30, 2024
1 parent e26494d commit d2a0d84
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions environment-sample
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GUNICORN_LOG_LEVEL=warn

SLACK_TECHNOISE_POST_KEY=slack_technoise_post_key
SLACK_TEAM_POST_KEY=slack_team_post_key
SLACK_OP_POST_KEY=slack_op_post_key
CF_API_KEY=cf_api_key

# The path to a file containing your credentials for accessing Google Cloud
Expand Down
3 changes: 2 additions & 1 deletion openprescribing/openprescribing/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,10 @@

# For sending messages to Slack
# Webhook URLs for posting to different channels can be configured at
# https://api.slack.com/apps/A6B85C8KC/incoming-webhooks
# https://api.slack.com/apps/A03UM1N45JN/incoming-webhooks
SLACK_TECHNOISE_POST_KEY = utils.get_env_setting("SLACK_TECHNOISE_POST_KEY", default="")
SLACK_TEAM_POST_KEY = utils.get_env_setting("SLACK_TEAM_POST_KEY", default="")
SLACK_OP_POST_KEY = utils.get_env_setting("SLACK_OP_POST_KEY", default="")
SLACK_SENDING_ACTIVE = True


Expand Down
14 changes: 9 additions & 5 deletions openprescribing/openprescribing/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
from django.conf import settings


def notify_slack(message, is_error=False):
"""Posts the message to #technoise
def notify_slack(message, is_error=False, channel="default"):
"""Posts the message to #technoise by default
See https://my.slack.com/services/new/incoming-webhook/
"""
if not settings.SLACK_SENDING_ACTIVE:
return

webhook_url = settings.SLACK_TECHNOISE_POST_KEY
team_webhook_url = settings.SLACK_TEAM_POST_KEY
webhooks = {
"default": settings.SLACK_TECHNOISE_POST_KEY,
"op": settings.SLACK_OP_POST_KEY,
"dev_team": settings.SLACK_TEAM_POST_KEY,
}
webhook_url = webhooks.get(channel, webhooks["default"])
slack_data = {"text": message}

response = requests.post(webhook_url, json=slack_data)
if is_error:
# Also post error messages to relevant team channel
response = requests.post(team_webhook_url, json=slack_data)
response = requests.post(webhooks["dev_team"], json=slack_data)

if response.status_code != 200:
raise ValueError(
Expand Down
3 changes: 2 additions & 1 deletion openprescribing/pipeline/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ def run_all(year, month, under_test=False):
)

if not under_test:
notify_slack(msg)
# Notify the openprescribing slack channel
notify_slack(msg, channel="op")


def in_progress():
Expand Down

0 comments on commit d2a0d84

Please sign in to comment.