Spaces:
Sleeping
Sleeping
| """Register (or inspect) the Telegram webhook for the JAA resume bot. | |
| Usage (from your machine — token can come from env or arg): | |
| python scripts/telegram_set_webhook.py set https://<your-space>.hf.space | |
| python scripts/telegram_set_webhook.py info | |
| python scripts/telegram_set_webhook.py delete | |
| Env: | |
| TELEGRAM_BOT_TOKEN (required) | |
| TELEGRAM_WEBHOOK_SECRET (optional but recommended — must match the HF secret) | |
| """ | |
| import os | |
| import sys | |
| import json | |
| import urllib.request | |
| import urllib.parse | |
| def _call(token: str, method: str, params: dict) -> dict: | |
| url = f"https://api.telegram.org/bot{token}/{method}" | |
| data = urllib.parse.urlencode(params).encode() if params else None | |
| with urllib.request.urlopen(urllib.request.Request(url, data=data), timeout=30) as r: | |
| return json.loads(r.read().decode()) | |
| def main() -> int: | |
| token = os.getenv("TELEGRAM_BOT_TOKEN", "").strip() | |
| if not token: | |
| print("Set TELEGRAM_BOT_TOKEN first.") | |
| return 1 | |
| secret = os.getenv("TELEGRAM_WEBHOOK_SECRET", "").strip() | |
| action = sys.argv[1] if len(sys.argv) > 1 else "info" | |
| if action == "set": | |
| if len(sys.argv) < 3: | |
| print("Usage: telegram_set_webhook.py set https://<space>.hf.space") | |
| return 1 | |
| base = sys.argv[2].rstrip("/") | |
| params = {"url": f"{base}/telegram/webhook", | |
| "allowed_updates": json.dumps(["message", "edited_message"])} | |
| if secret: | |
| params["secret_token"] = secret | |
| print(json.dumps(_call(token, "setWebhook", params), indent=2)) | |
| elif action == "delete": | |
| print(json.dumps(_call(token, "deleteWebhook", {}), indent=2)) | |
| else: | |
| print(json.dumps(_call(token, "getWebhookInfo", {}), indent=2)) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |