feat: persist browser sessions and update admin workflows
This commit is contained in:
@@ -15,6 +15,7 @@ from app.utils.dingtalk import (
|
||||
format_dingtalk_rate_changed,
|
||||
format_dingtalk_website_rate_changed,
|
||||
format_dingtalk_status,
|
||||
format_dingtalk_balance_low,
|
||||
)
|
||||
|
||||
|
||||
@@ -185,6 +186,43 @@ def send_status_event(
|
||||
_log(db, wh, event, generic_payload, "failed", str(exc))
|
||||
|
||||
|
||||
def send_balance_low(
|
||||
db: Session,
|
||||
upstream_id: int,
|
||||
upstream_name: str,
|
||||
base_url: str,
|
||||
balance: float,
|
||||
threshold: float,
|
||||
) -> None:
|
||||
webhooks = (
|
||||
db.query(WebhookConfig)
|
||||
.filter(WebhookConfig.enabled == True)
|
||||
.all()
|
||||
)
|
||||
event = "upstream_balance_low"
|
||||
changed_at = _now_iso()
|
||||
generic_payload = {
|
||||
"event": event,
|
||||
"upstream": {"id": upstream_id, "name": upstream_name, "base_url": base_url},
|
||||
"balance": balance,
|
||||
"threshold": threshold,
|
||||
"changed_at": changed_at,
|
||||
}
|
||||
for wh in webhooks:
|
||||
events = json.loads(wh.events_json or "[]")
|
||||
if event not in events:
|
||||
continue
|
||||
try:
|
||||
if wh.type == "dingtalk":
|
||||
msg = format_dingtalk_balance_low(upstream_name, balance, threshold, changed_at)
|
||||
resp_text = _send_dingtalk(wh.url, wh.secret, msg)
|
||||
else:
|
||||
resp_text = _send_generic(wh.url, generic_payload)
|
||||
_log(db, wh, event, generic_payload, "success", resp_text)
|
||||
except Exception as exc:
|
||||
_log(db, wh, event, generic_payload, "failed", str(exc))
|
||||
|
||||
|
||||
def send_test_notification(db: Session, webhook: WebhookConfig) -> tuple[bool, str]:
|
||||
payload = {
|
||||
"event": "test",
|
||||
|
||||
Reference in New Issue
Block a user