Coverage for kube_notify/notifications/discord.py: 100%
13 statements
« prev ^ index » next coverage.py v7.6.0, created at 2025-02-07 09:16 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2025-02-07 09:16 +0000
1import json
3import requests
5from kube_notify.utils import logger
8def send_discord_webhook(
9 webhook_url: str,
10 title: str,
11 description: str,
12 fields: dict[str, str],
13 username: str = "kube-notify",
14 avatar_url: str = None,
15) -> None:
16 # Construct the HTTP request for sending a message to Discord via the Webhook
17 headers = {"Content-Type": "application/json"}
19 data = {
20 "embeds": [
21 {
22 "title": title,
23 "description": description,
24 "fields": [
25 {"name": key, "value": value, "inline": True}
26 for key, value in fields.items()
27 ],
28 }
29 ]
30 }
31 # Optionally add username and avatar_url to customize the webhook message sender
32 if username:
33 data["username"] = username
34 if avatar_url:
35 data["avatar_url"] = avatar_url
36 response = requests.post(webhook_url, headers=headers, data=json.dumps(data))
37 if response.status_code != 204:
38 logger.logger.error("Failed to send notification to Discord")