Skip to content

Commit

Permalink
Slack: use json= to send a message using webhooks (#11536)
Browse files Browse the repository at this point in the history
* Slack: use `json=` to send a message using webhooks

It was giving us 400 because we weren't sending the data as JSON.
I also add a small log line, just in case.

* Simplify the message to remove some icons and add more links
  • Loading branch information
humitos authored Aug 14, 2024
1 parent 0d6deed commit 6036c2e
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions readthedocs/subscriptions/event_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def subscription_canceled(event):
except SSOIntegration.DoesNotExist:
sso_integration = "Read the Docs Auth"

# https://api.slack.com/surfaces/messages#payloads
slack_message = {
"blocks": [
{
Expand All @@ -241,19 +242,11 @@ def subscription_canceled(event):
"fields": [
{
"type": "mrkdwn",
"text": f":office: *Name:* {organization.name}",
"text": f":office: *Name:* <{settings.ADMIN_URL}/organizations/organization/{organization.pk}/change/|{organization.name}>",
},
{
"type": "mrkdwn",
"text": f":dollar: *Plan:* {stripe_subscription.plan.id}",
},
{
"type": "mrkdwn",
"text": f":hash: *Slug:* {organization.slug}",
},
{
"type": "mrkdwn",
"text": f":person_frowning: *Stripe customer:* <https://dashboard.stripe.com/customers/{stripe_subscription.customer_id}|{stripe_subscription.customer_id}>",
"text": f":dollar: *Plan:* <https://dashboard.stripe.com/customers/{stripe_subscription.customer_id}|{stripe_subscription.plan.product.name}> (${str(total_spent)})",
},
{
"type": "mrkdwn",
Expand Down Expand Up @@ -286,11 +279,14 @@ def subscription_canceled(event):
]
}
try:
requests.post(
response = requests.post(
settings.SLACK_WEBHOOK_SALES_CHANNEL,
data=slack_message,
json=slack_message,
timeout=3,
)
if not response.ok:
log.error("There was an issue when sending a message to Slack webhook")

except requests.Timeout:
log.warning("Timeout sending a message to Slack webhook")

Expand Down

0 comments on commit 6036c2e

Please sign in to comment.