From fa65be2ad2f62935ce7933f2fbf486ee322c4d19 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 20 Mar 2024 18:47:13 -0700 Subject: [PATCH] refac: post webhook --- backend/apps/web/routers/auths.py | 1 + backend/utils/webhook.py | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/backend/apps/web/routers/auths.py b/backend/apps/web/routers/auths.py index 0f9704974..d881ec746 100644 --- a/backend/apps/web/routers/auths.py +++ b/backend/apps/web/routers/auths.py @@ -159,6 +159,7 @@ async def signup(request: Request, form_data: SignupForm): if request.app.state.WEBHOOK_URL: post_webhook( request.app.state.WEBHOOK_URL, + WEBHOOK_MESSAGES.USER_SIGNUP(user.name), { "action": "signup", "message": WEBHOOK_MESSAGES.USER_SIGNUP(user.name), diff --git a/backend/utils/webhook.py b/backend/utils/webhook.py index c1d03ac71..1bc5a6048 100644 --- a/backend/utils/webhook.py +++ b/backend/utils/webhook.py @@ -1,9 +1,18 @@ import requests -def post_webhook(url: str, json: dict) -> bool: +def post_webhook(url: str, message: str, event_data: dict) -> bool: try: - r = requests.post(url, json=json) + payload = {} + + if "https://hooks.slack.com" in url: + payload["text"] = message + elif "https://discord.com/api/webhooks" in url: + payload["content"] = message + else: + payload = {**event_data} + + r = requests.post(url, json=payload) r.raise_for_status() return True except Exception as e: