Files
modsetter--surfsense/surfsense_backend/scripts/register_webhook.py
T
wehub-resource-sync 6ede33ccdb
Build and Push Docker Images / create_manifest (web, surfsense-web, , cpu) (push) Has been cancelled
Build and Push Docker Images / finalize_release (push) Has been cancelled
Obsidian Plugin Lint / lint (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-24.04-arm, linux/arm64, arm64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-latest, linux/amd64, amd64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / compute_version (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / verify_digests (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, , cpu) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda, cuda) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda126, cuda126) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:33:44 +08:00

63 lines
1.8 KiB
Python

"""Register the SurfSense Telegram webhook."""
from __future__ import annotations
import asyncio
import os
import re
import sys
from dotenv import load_dotenv
from telegram import Bot
from app.db import async_session_maker
from app.gateway.accounts import get_or_create_system_telegram_account
load_dotenv()
WEBHOOK_SECRET_RE = re.compile(r"^[A-Za-z0-9_-]{1,256}$")
async def main() -> int:
token = os.getenv("TELEGRAM_SHARED_BOT_TOKEN")
secret = os.getenv("TELEGRAM_WEBHOOK_SECRET")
base_url = os.getenv("GATEWAY_BASE_URL") or os.getenv("BACKEND_URL")
if not token or not secret or not base_url:
print(
"Missing TELEGRAM_SHARED_BOT_TOKEN, TELEGRAM_WEBHOOK_SECRET, or GATEWAY_BASE_URL/BACKEND_URL",
file=sys.stderr,
)
return 1
if not WEBHOOK_SECRET_RE.fullmatch(secret):
print(
"TELEGRAM_WEBHOOK_SECRET must be 1-256 chars and contain only A-Z, a-z, 0-9, '_' or '-'",
file=sys.stderr,
)
return 1
async with async_session_maker() as session:
account = await get_or_create_system_telegram_account(session)
account.webhook_secret = secret
await session.commit()
account_id = int(account.id)
webhook_url = (
f"{base_url.rstrip('/')}/api/v1/gateway/webhooks/telegram/{account_id}"
)
bot = Bot(token=token)
ok = await bot.set_webhook(
url=webhook_url,
secret_token=secret,
allowed_updates=["message", "edited_message"],
drop_pending_updates=True,
)
if not ok:
print("Telegram rejected webhook registration", file=sys.stderr)
return 1
print(f"Registered Telegram webhook: {webhook_url}")
return 0
if __name__ == "__main__":
raise SystemExit(asyncio.run(main()))