feat: verstuur dashcam-notificatie via Matrix (sherpa) i.p.v. WhatsApp
wwebjs-api is vervangen door wuzapi; notificatiekanaal is nu Matrix: - send_matrix_notification() POST -> sherpa compat-token uit ~/whatsed-new/secrets/sherpa-token naar eddy-report-room - zelfde patronen als watchdog-report/WAN IP Guard - tests: SendWhatsappTest -> SendMatrixTest (+2 asserts)
This commit is contained in:
parent
0227dbda3f
commit
55c282906e
3 changed files with 88 additions and 40 deletions
|
|
@ -26,8 +26,9 @@ MAX_RETRIES = 3
|
|||
RETRY_DELAY = 2
|
||||
MAX_CONSECUTIVE_ERRORS = 5
|
||||
|
||||
WHATSAPP_API = "http://localhost:3001/client/sendMessage/EdGpt" # localhost = nextcloud server, niet Mac Mini!
|
||||
WHATSAPP_CHAT = "31620248011@c.us"
|
||||
MATRIX_API = "https://whatsed.eddydevink.nl/_matrix/client/v3"
|
||||
MATRIX_TOKEN_FILE = "/home/aiaiaiaiagent/whatsed-new/secrets/sherpa-token"
|
||||
MATRIX_ROOM_FILE = "/home/aiaiaiaiagent/whatsed-new/secrets/eddy-report-room"
|
||||
|
||||
UNREADABLE_REASON = (
|
||||
"SD-kaart onleesbaar (I/O-fout): lezer/kaart defect of niet netjes afgemeld. "
|
||||
|
|
@ -237,7 +238,7 @@ def transfer_files(files, source_dir, target_base):
|
|||
return ok, errors
|
||||
|
||||
|
||||
def send_whatsapp_notification(ok, errors, total_bytes, aborted=False, reason=None):
|
||||
def send_matrix_notification(ok, errors, total_bytes, aborted=False, reason=None):
|
||||
gb = total_bytes / (1024 ** 3)
|
||||
if aborted:
|
||||
body = (
|
||||
|
|
@ -254,16 +255,26 @@ def send_whatsapp_notification(ok, errors, total_bytes, aborted=False, reason=No
|
|||
f"❌ Dashcam transfer voltooid: {ok} bestanden ({gb:.1f} GB) "
|
||||
f"overgezet, {errors} fout(en)"
|
||||
)
|
||||
payload = {"chatId": WHATSAPP_CHAT, "content": body, "contentType": "string"}
|
||||
try:
|
||||
with open(MATRIX_TOKEN_FILE) as f:
|
||||
token = f.read().strip()
|
||||
with open(MATRIX_ROOM_FILE) as f:
|
||||
room = f.read().strip()
|
||||
if not token or not room:
|
||||
raise OSError("token of room-ID onbekend (lege secret-file)")
|
||||
txn_id = "dashcam-%d" % int(time.time() * 1000)
|
||||
url = "%s/rooms/%s/send/m.room.message/%s" % (MATRIX_API, room, txn_id)
|
||||
payload = {"msgtype": "m.text", "body": body}
|
||||
data = json.dumps(payload).encode()
|
||||
req = urllib.request.Request(
|
||||
WHATSAPP_API, data=data, headers={"Content-Type": "application/json"}
|
||||
url, data=data, method="PUT",
|
||||
headers={"Content-Type": "application/json",
|
||||
"Authorization": "Bearer " + token},
|
||||
)
|
||||
urllib.request.urlopen(req, timeout=10)
|
||||
logging.info("WhatsApp notificatie verstuurd")
|
||||
logging.info("Matrix notificatie verstuurd")
|
||||
except Exception as e:
|
||||
logging.warning("WhatsApp notificatie mislukt: %s", e)
|
||||
logging.warning("Matrix notificatie mislukt: %s", e)
|
||||
|
||||
|
||||
def parse_args():
|
||||
|
|
@ -285,7 +296,7 @@ def main():
|
|||
|
||||
files = get_mp4_files(SOURCE_DIR)
|
||||
if files is None:
|
||||
send_whatsapp_notification(0, 0, 0, aborted=True, reason=UNREADABLE_REASON)
|
||||
send_matrix_notification(0, 0, 0, aborted=True, reason=UNREADABLE_REASON)
|
||||
return
|
||||
if not files:
|
||||
logging.info("Geen MP4 bestanden gevonden in %s", SOURCE_DIR)
|
||||
|
|
@ -295,7 +306,7 @@ def main():
|
|||
files = files[:args.limit]
|
||||
|
||||
if not check_source_readable(SOURCE_DIR, files):
|
||||
send_whatsapp_notification(0, 0, 0, aborted=True, reason=UNREADABLE_REASON)
|
||||
send_matrix_notification(0, 0, 0, aborted=True, reason=UNREADABLE_REASON)
|
||||
return
|
||||
|
||||
total_size = 0
|
||||
|
|
@ -304,7 +315,7 @@ def main():
|
|||
total_size += os.path.getsize(os.path.join(SOURCE_DIR, fname))
|
||||
except OSError as e:
|
||||
logging.error("Grootte van %s niet leesbaar: %s", fname, e)
|
||||
send_whatsapp_notification(0, 0, 0, aborted=True, reason=UNREADABLE_REASON)
|
||||
send_matrix_notification(0, 0, 0, aborted=True, reason=UNREADABLE_REASON)
|
||||
return
|
||||
|
||||
logging.info("Start transfer: %d bestanden, %d bytes", len(files), total_size)
|
||||
|
|
@ -312,7 +323,7 @@ def main():
|
|||
try:
|
||||
ensure_free_space(total_size, TARGET_BASE)
|
||||
except SystemExit:
|
||||
send_whatsapp_notification(
|
||||
send_matrix_notification(
|
||||
0, 0, total_size, aborted=True,
|
||||
reason="Onvoldoende vrije ruimte op de SSD, ook na opschonen.",
|
||||
)
|
||||
|
|
@ -322,11 +333,11 @@ def main():
|
|||
ok, errors = transfer_files(files, SOURCE_DIR, TARGET_BASE)
|
||||
except TransferAborted as e:
|
||||
logging.info("Transfer gestopt: %d ok, %d fouten", e.ok, e.errors)
|
||||
send_whatsapp_notification(e.ok, e.errors, total_size, aborted=True, reason=e.reason)
|
||||
send_matrix_notification(e.ok, e.errors, total_size, aborted=True, reason=e.reason)
|
||||
return
|
||||
|
||||
logging.info("Transfer voltooid: %d ok, %d fouten", ok, errors)
|
||||
send_whatsapp_notification(ok, errors, total_size)
|
||||
send_matrix_notification(ok, errors, total_size)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Reference in a new issue