|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Set Telegram webhook for FrontChapter group gate bot. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + export TELEGRAM_BOT_TOKEN=... |
| 6 | + export TELEGRAM_WEBHOOK_SECRET=... # random string you also set as Supabase secret |
| 7 | + export SUPABASE_PROJECT_REF=yuwsoyhknbjipfouemen |
| 8 | + python3 scripts/set-telegram-webhook.py |
| 9 | +
|
| 10 | +Optional: |
| 11 | + TELEGRAM_GROUP_CHAT_ID=-100… (or let bot learn it on first join /chatid) |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import json |
| 17 | +import os |
| 18 | +import sys |
| 19 | +import urllib.parse |
| 20 | +import urllib.request |
| 21 | + |
| 22 | +TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN", "").strip() |
| 23 | +SECRET = os.environ.get("TELEGRAM_WEBHOOK_SECRET", "").strip() |
| 24 | +REF = os.environ.get("SUPABASE_PROJECT_REF", "yuwsoyhknbjipfouemen").strip() |
| 25 | + |
| 26 | +if not TOKEN or not SECRET: |
| 27 | + print("Need TELEGRAM_BOT_TOKEN and TELEGRAM_WEBHOOK_SECRET", file=sys.stderr) |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | +url = f"https://{REF}.supabase.co/functions/v1/telegram-bot" |
| 31 | +payload = { |
| 32 | + "url": url, |
| 33 | + "secret_token": SECRET, |
| 34 | + "allowed_updates": ["chat_member", "message"], |
| 35 | + "drop_pending_updates": True, |
| 36 | +} |
| 37 | + |
| 38 | +req = urllib.request.Request( |
| 39 | + f"https://api.telegram.org/bot{TOKEN}/setWebhook", |
| 40 | + data=json.dumps(payload).encode(), |
| 41 | + headers={"Content-Type": "application/json"}, |
| 42 | + method="POST", |
| 43 | +) |
| 44 | +with urllib.request.urlopen(req) as res: |
| 45 | + body = json.loads(res.read().decode()) |
| 46 | +print(json.dumps(body, indent=2, ensure_ascii=False)) |
| 47 | + |
| 48 | +info_req = urllib.request.Request( |
| 49 | + f"https://api.telegram.org/bot{TOKEN}/getWebhookInfo" |
| 50 | +) |
| 51 | +with urllib.request.urlopen(info_req) as res: |
| 52 | + print(json.dumps(json.loads(res.read().decode()), indent=2, ensure_ascii=False)) |
0 commit comments