refactor: streamline Discord message sending by removing JSON library dependency and enhancing debug output

This commit is contained in:
Aaron 2025-11-04 17:30:57 -05:00
parent 92d49daee5
commit 78c22d6f3e

View File

@ -1,30 +1,38 @@
import urequests as requests import urequests as requests
try:
import ujson as json
except Exception:
import json
from secrets import secrets from secrets import secrets
def _escape_json_str(s: str) -> str:
# minimal JSON string escaper for quotes/backslashes and control chars
s = s.replace("\\", "\\\\")
s = s.replace('"', '\\"')
s = s.replace("\n", "\\n")
s = s.replace("\r", "\\r")
s = s.replace("\t", "\\t")
return s
def send_discord_message(message, username="Auto Garden Bot"): def send_discord_message(message, username="Auto Garden Bot"):
resp = None resp = None
url = secrets.get('discord_webhook_url') url = secrets.get('discord_webhook_url')
try: try:
payload = {"content": message, "username": username} if not url:
body = json.dumps(payload) print("DEBUG: no webhook URL in secrets")
if isinstance(body, str): return False
body = body.encode("utf-8")
headers = {
"Content-Type": "application/json",
"Content-Length": str(len(body))
}
# DEBUG: print exact values being sent url = url.strip().strip('\'"')
# build JSON by hand so emoji (and other unicode) are preserved as UTF-8 bytes
content = _escape_json_str(message)
user = _escape_json_str(username)
body_bytes = ('{"content":"%s","username":"%s"}' % (content, user)).encode("utf-8")
headers = {"Content-Type": "application/json; charset=utf-8"}
# DEBUG
print("DEBUG: webhook url repr:", repr(url)) print("DEBUG: webhook url repr:", repr(url))
print("DEBUG: body (bytes):", body) print("DEBUG: body (bytes):", body_bytes)
print("DEBUG: headers:", headers) print("DEBUG: headers:", headers)
resp = requests.post(url, data=body, headers=headers) resp = requests.post(url, data=body_bytes, headers=headers)
status = getattr(resp, "status", getattr(resp, "status_code", None)) status = getattr(resp, "status", getattr(resp, "status_code", None))
text = "" text = ""