Skip to content

Commit 6036c2e

Browse files
authored
Slack: use json= to send a message using webhooks (#11536)
* 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
1 parent 0d6deed commit 6036c2e

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

readthedocs/subscriptions/event_handlers.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def subscription_canceled(event):
229229
except SSOIntegration.DoesNotExist:
230230
sso_integration = "Read the Docs Auth"
231231

232+
# https://api.slack.com/surfaces/messages#payloads
232233
slack_message = {
233234
"blocks": [
234235
{
@@ -241,19 +242,11 @@ def subscription_canceled(event):
241242
"fields": [
242243
{
243244
"type": "mrkdwn",
244-
"text": f":office: *Name:* {organization.name}",
245+
"text": f":office: *Name:* <{settings.ADMIN_URL}/organizations/organization/{organization.pk}/change/|{organization.name}>",
245246
},
246247
{
247248
"type": "mrkdwn",
248-
"text": f":dollar: *Plan:* {stripe_subscription.plan.id}",
249-
},
250-
{
251-
"type": "mrkdwn",
252-
"text": f":hash: *Slug:* {organization.slug}",
253-
},
254-
{
255-
"type": "mrkdwn",
256-
"text": f":person_frowning: *Stripe customer:* <https://dashboard.stripe.com/customers/{stripe_subscription.customer_id}|{stripe_subscription.customer_id}>",
249+
"text": f":dollar: *Plan:* <https://dashboard.stripe.com/customers/{stripe_subscription.customer_id}|{stripe_subscription.plan.product.name}> (${str(total_spent)})",
257250
},
258251
{
259252
"type": "mrkdwn",
@@ -286,11 +279,14 @@ def subscription_canceled(event):
286279
]
287280
}
288281
try:
289-
requests.post(
282+
response = requests.post(
290283
settings.SLACK_WEBHOOK_SALES_CHANNEL,
291-
data=slack_message,
284+
json=slack_message,
292285
timeout=3,
293286
)
287+
if not response.ok:
288+
log.error("There was an issue when sending a message to Slack webhook")
289+
294290
except requests.Timeout:
295291
log.warning("Timeout sending a message to Slack webhook")
296292

0 commit comments

Comments
 (0)