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>
This commit is contained in:
Aaron 2025-11-03 20:36:56 -05:00
parent c680ed17f1
commit e7ac7457c1

View File

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