Add example configuration file, moved everything from secrets.py to here.
Feat: Also refactored some of the logic in discord_webhook.py and networking.py to be more friendly towards the pico with ram usage. Fixes #26
This commit is contained in:
@@ -1,8 +1,23 @@
|
||||
import urequests as requests # type: ignore
|
||||
from secrets import secrets
|
||||
# Minimal module-level state (only what we need)
|
||||
_CONFIG = {"discord_webhook_url": None, "discord_alert_webhook_url": None}
|
||||
|
||||
def set_config(cfg: dict):
|
||||
"""Initialize module with minimal values from loaded config (call from main)."""
|
||||
global _CONFIG
|
||||
if not cfg:
|
||||
_CONFIG = {"discord_webhook_url": None, "discord_alert_webhook_url": None}
|
||||
return
|
||||
_CONFIG = {
|
||||
"discord_webhook_url": cfg.get("discord_webhook_url"),
|
||||
"discord_alert_webhook_url": cfg.get("discord_alert_webhook_url"),
|
||||
}
|
||||
|
||||
def _get_webhook_url(is_alert: bool = False):
|
||||
if is_alert:
|
||||
return _CONFIG.get("discord_alert_webhook_url") or _CONFIG.get("discord_webhook_url")
|
||||
return _CONFIG.get("discord_webhook_url")
|
||||
|
||||
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")
|
||||
@@ -11,44 +26,52 @@ def _escape_json_str(s: str) -> str:
|
||||
return s
|
||||
|
||||
def send_discord_message(message, username="Auto Garden Bot", is_alert=False):
|
||||
"""Send Discord message with 3-second timeout to prevent blocking."""
|
||||
"""
|
||||
Send Discord message. Import urequests locally to avoid occupying RAM when idle.
|
||||
Returns True on success, False otherwise.
|
||||
"""
|
||||
resp = None
|
||||
|
||||
# Use alert webhook if specified, otherwise normal webhook
|
||||
if is_alert:
|
||||
url = secrets.get('discord_alert_webhook_url') or secrets.get('discord_webhook_url')
|
||||
else:
|
||||
url = secrets.get('discord_webhook_url')
|
||||
|
||||
url = _get_webhook_url(is_alert=is_alert)
|
||||
if not url:
|
||||
return False
|
||||
|
||||
try:
|
||||
if not url:
|
||||
return False
|
||||
# local import to save RAM
|
||||
import urequests as requests # type: ignore
|
||||
import gc
|
||||
|
||||
url = url.strip().strip('\'"')
|
||||
|
||||
# Build JSON manually to preserve emoji/unicode as UTF-8
|
||||
url = str(url).strip().strip('\'"')
|
||||
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"}
|
||||
|
||||
# Make POST request (urequests has built-in ~5s timeout)
|
||||
resp = requests.post(url, data=body_bytes, headers=headers)
|
||||
|
||||
status = getattr(resp, "status", getattr(resp, "status_code", None))
|
||||
|
||||
if status and 200 <= status < 300:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
success = bool(status and 200 <= status < 300)
|
||||
if not success:
|
||||
# optional: print status for debugging, but avoid spamming
|
||||
print("Discord webhook failed, status:", status)
|
||||
return success
|
||||
|
||||
except Exception as e:
|
||||
# Silently fail (don't spam console with Discord errors)
|
||||
# avoid raising to prevent crashing monitors; print minimal info
|
||||
print("Discord webhook exception:", e)
|
||||
return False
|
||||
|
||||
finally:
|
||||
if resp:
|
||||
try:
|
||||
try:
|
||||
if resp:
|
||||
resp.close()
|
||||
except:
|
||||
pass
|
||||
except:
|
||||
pass
|
||||
# free large objects and modules, then force GC
|
||||
try:
|
||||
del resp
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
gc.collect()
|
||||
except:
|
||||
pass
|
||||
Reference in New Issue
Block a user