Refactor discord_webhook.py: add is_alert parameter to send_discord_message for flexible webhook usage
This commit is contained in:
parent
4ef7b00b74
commit
fc318bb74d
@ -10,12 +10,18 @@ def _escape_json_str(s: str) -> str:
|
|||||||
s = s.replace("\t", "\\t")
|
s = s.replace("\t", "\\t")
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def send_discord_message(message, username="Auto Garden Bot"):
|
def send_discord_message(message, username="Auto Garden Bot", is_alert=False):
|
||||||
resp = None
|
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 = secrets.get('discord_webhook_url')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not url:
|
if not url:
|
||||||
print("Error: no webhook URL in secrets")
|
# print("DEBUG: no webhook URL in secrets")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
url = url.strip().strip('\'"')
|
url = url.strip().strip('\'"')
|
||||||
@ -32,14 +38,14 @@ def send_discord_message(message, username="Auto Garden Bot"):
|
|||||||
status = getattr(resp, "status", getattr(resp, "status_code", None))
|
status = getattr(resp, "status", getattr(resp, "status_code", None))
|
||||||
|
|
||||||
if status and 200 <= status < 300:
|
if status and 200 <= status < 300:
|
||||||
print("Discord message sent")
|
# print("Discord message sent")
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
print(f"Discord webhook failed with status {status}")
|
# print(f"Discord webhook failed with status {status}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Failed to send Discord message:", e)
|
# print("Failed to send Discord message:", e)
|
||||||
return False
|
return False
|
||||||
finally:
|
finally:
|
||||||
if resp:
|
if resp:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user