distribusi: notifikasi Telegram mulai/selesai/gagal (telegram.py, V30)
This commit is contained in:
1 parent
397e20ec3a
commit
025f46d399
8 files changed
+170
-7
No files matched your search
+56
@@ -0,0 +1,56 @@
|
||||
"""Kirim notifikasi status distribusi via Telegram Bot API (best-effort).
|
||||
|
||||
Token + daftar chat id dari env (`TELEGRAM_BOT_TOKEN`, `TELEGRAM_CHAT_IDS`
|
||||
CSV) via `config.py`. Tanpa token/daftar → no-op senyap (⊥ ganggu run lokal).
|
||||
Gagal kirim hanya dicatat — ⊥ pernah crash distribusi.
|
||||
"""
|
||||
|
||||
import requests
|
||||
|
||||
from config import TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_IDS
|
||||
|
||||
|
||||
def send_text(text):
|
||||
"""Kirim `text` ke semua chat id tujuan. Best-effort: gagal → log saja."""
|
||||
if not TELEGRAM_BOT_TOKEN or not TELEGRAM_CHAT_IDS:
|
||||
return
|
||||
url = f'https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage'
|
||||
for chat_id in TELEGRAM_CHAT_IDS:
|
||||
try:
|
||||
resp = requests.post(url, json={'chat_id': chat_id, 'text': text},
|
||||
timeout=10)
|
||||
resp.raise_for_status()
|
||||
except requests.RequestException as exc:
|
||||
print(f'[Peringatan] Gagal kirim Telegram ke {chat_id}: {exc}',
|
||||
flush=True)
|
||||
|
||||
|
||||
def notify_start(run_date, n_voters, total_share, balance):
|
||||
"""Notifikasi run nyata dimulai (setelah guard no-op + key siap)."""
|
||||
send_text(
|
||||
f'DISTRIBUSI MULAI\n'
|
||||
f'Tanggal: {run_date}\n'
|
||||
f'Pemilih: {n_voters}\n'
|
||||
f'Total share: {total_share} VEX\n'
|
||||
f'Saldo: {balance} VEX'
|
||||
)
|
||||
|
||||
|
||||
def notify_finish(status, sent, total, total_share, failed, run_date):
|
||||
"""Notifikasi run selesai dengan ringkasan (ok | partial)."""
|
||||
send_text(
|
||||
f'DISTRIBUSI SELESAI\n'
|
||||
f'Tanggal: {run_date}\n'
|
||||
f'Status: {status}\n'
|
||||
f'Terkirim: {sent}/{total} pemilih\n'
|
||||
f'Total share: {total_share} VEX\n'
|
||||
f'Gagal: {len(failed)}'
|
||||
)
|
||||
|
||||
|
||||
def notify_failure(exc):
|
||||
"""Notifikasi run gagal total (exception di main)."""
|
||||
send_text(
|
||||
f'DISTRIBUSI GAGAL\n'
|
||||
f'Error: {exc}'
|
||||
)
|
||||
Reference in new issue
Block a user