forked from nathanabay/bespo_notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_credentials.py
More file actions
38 lines (36 loc) · 1.66 KB
/
test_credentials.py
File metadata and controls
38 lines (36 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import frappe
from bespo_notifications.bespo_notifications.utils import get_bot_info
import requests
def run():
frappe.init(site="erp.bespo.et", sites_path="/home/frappe/frappe-bench/sites")
frappe.connect()
print("--- TESTING BOT CREDENTIALS ---")
bots = frappe.get_all("Telegram Bot", fields=["name", "enabled", "bot_username"])
for b in bots:
print(f"Bot '{b.name}' (Enabled: {b.enabled}, Username: {b.bot_username})")
info = get_bot_info(b.name)
if not info:
print(" [ERROR] get_bot_info() returned None")
continue
token = info.get("token")
if not token:
print(" [ERROR] Token missing from get_bot_info()")
continue
print(" [OK] Token retrieved from frappe.utils.password")
api_url = (info.get("api_url") or "https://api.telegram.org/bot").rstrip('/')
try:
resp = requests.post(f"{api_url}{token}/getWebhookInfo", timeout=10)
data = resp.json()
if data.get("ok"):
wh = data.get("result", {})
print(f" [OK] Telegram API reachable. Webhook URL: {wh.get('url', 'Not Set')}")
if wh.get('pending_update_count', 0) > 0:
print(f" [WARN] Webhook has {wh['pending_update_count']} pending updates")
if wh.get('last_error_message'):
print(f" [ERROR] Webhook last error: {wh['last_error_message']}")
else:
print(f" [ERROR] Telegram API failed: {data.get('description')}")
except Exception as e:
print(f" [EXCEPTION] Connection error: {e}")
if __name__ == "__main__":
run()