Auto-Garden/Scripts/discord_webhook.py
sickprodigy e7ac7457c1 now this error:
Discord webhook HTTP 400 body: <html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
2025-11-03 20:36:56 -05:00

46 lines
1.3 KiB
Python

import urequests as requests
try:
import ujson as json
except Exception:
import json
from secrets import secrets
def send_discord_message(message, username="Auto Garden Bot"):
resp = None
try:
payload = {"content": message, "username": username}
body = json.dumps(payload)
if isinstance(body, str):
body = body.encode("utf-8")
headers = {
"Content-Type": "application/json",
"Content-Length": str(len(body))
}
resp = requests.post(secrets['discord_webhook_url'], data=body, headers=headers)
status = getattr(resp, "status", getattr(resp, "status_code", None))
body_text = ""
try:
body_text = resp.text
except Exception:
try:
body_text = resp.content
except Exception:
body_text = ""
if status and 200 <= status < 300:
print("Discord message sent")
return True
else:
print("Discord webhook HTTP", status, "body:", body_text)
return False
except Exception as e:
print("Failed to send Discord message:", e)
return False
finally:
if resp:
try:
resp.close()
except:
pass