How pandagangsters/ticketbot_pro works
1. Overall shape
- This repo is a serverless Telegram support system centered on a single Supabase Edge Function: supabase/functions/ticketing-bot/index.ts.
- The persistent layer is Supabase PostgreSQL, with schema and reset scripts in schema.sql and reset_database.sql.
- There is also a small Vite + React frontend shell (src/, index.html), but the README describes the operational system as the Telegram bot + database, not a separate web app.
2. Core runtime boundary
- The main runtime is Deno/TypeScript inside the Supabase Edge Function.
- Telegram sends webhook updates to the function.
- The function acts as the orchestration layer: it interprets commands, routes messages, writes state to PostgreSQL, and calls the Telegram Bot API for sending/copying messages.
3. Primary data flow: user ticket creation
- A Telegram user sends one or more messages to the bot.
- The Edge Function receives the webhook payload and stores/updates ticket state in the database.
- User content is relayed to the staff side using copyMessage, which preserves multimedia and formatting fidelity.
- The system supports multi-message tickets before final confirmation and keeps the conversation open until explicitly closed.
4. Primary data flow: staff handling
- Staff operate in a supergroup with Topics enabled.
- For each ticket, the bot creates or uses a dedicated forum topic in the staff group.
- Staff replies are forwarded back to the user, maintaining the ticket conversation in two directions.
- Messages starting with . are treated as internal notes and ignored by the bot, allowing staff-only discussion without affecting the ticket.
5. Ticket closure and evidence generation
- When an admin closes a ticket, the function generates an HTML evidence document containing the full conversation history.
- Media is embedded into that evidence document, but downloads are capped at 3 MB to avoid memory/OOM issues.
- The final evidence is sent to a dedicated history topic in the staff group, or to a topic configured via DB/secret.
6. Configuration and admin control
- Administrative control is exposed through Telegram commands like /admin and /logout.
- The admin panel can update system settings such as the ticket prefix and history topic ID.
- Some runtime configuration comes from Supabase Edge Function secrets:
- TELEGRAM_BOT_TOKEN
- ID_GRUPO_STAFF
- MASTER_PASSWORD
- optional TOPIC_EVIDENCIA
- optional BOT_TIMEZONE
7. Persistence model
- PostgreSQL is the system of record for tickets, settings, and likely message/ticket metadata.
- The README explicitly mentions RLS and use of the service role key, meaning the Edge Function bypasses user-facing restrictions and is the trusted backend boundary.
- schema.sql defines the production schema, and reset_database.sql is a full destructive reset path for rebuilding the system.
8. External integrations
- The bot depends on the Telegram Bot API for all user/staff interaction, including webhook handling, message copying, topic management, and evidence delivery.
- Supabase provides the managed infrastructure: database + serverless function hosting.
- No other external services are described as core to the architecture.
9. Frontend subsystem
- src/main.tsx, src/App.tsx, and src/index.css form a client-side React app built with Vite.
- Based on the README, this frontend is not the primary runtime path for ticket handling; the actual business workflow lives in Telegram and the Edge Function.
- For an architecture diagram, this should be shown as an auxiliary UI surface unless further code inspection shows it participates in admin/config flows.
10. Operational constraints that matter architecturally
- The staff group must be a supergroup with Topics enabled; otherwise ticket-topic routing falls back to the general chat.
- The bot is deployed as a single webhook endpoint, so the function is stateless between requests and all durable state must live in PostgreSQL.
- The architecture is intentionally minimal: one serverless handler, one database, and Telegram as the interaction layer.
Main entry points: index.ts Ticket bot · schema.sql Schema · main.tsx App entry · reset_database.sql Reset SQL
Components 18
Bot runtime
Webhook-driven Telegram support orchestration inside a Supabase Edge Function.
- Ticket bot · Single webhook handler that routes commands, messages, ticket state, and replies.
supabase/functions/ticketing-bot/index.ts - Command router · Handles admin commands, logout, and operational command paths.
supabase/functions/ticketing-bot/index.ts - Ticket flow · Manages multi-message ticket creation, open/closed status, and staff/user linkage.
supabase/functions/ticketing-bot/index.ts - Staff topic · Creates or reuses a dedicated staff-topic thread for each ticket.
supabase/functions/ticketing-bot/index.ts - Notes filter · Ignores staff messages starting with '.' so they remain internal notes.
supabase/functions/ticketing-bot/index.ts - Evidence export · Builds the closing evidence document from the full conversation history.
supabase/functions/ticketing-bot/index.ts - Media embed · Includes media in evidence output with a 3 MB download cap.
supabase/functions/ticketing-bot/index.ts
Persistence
Database schema and reset path for tickets, settings, and history.
- Settings · Persists ticket prefix, history topic ID, and other admin-controlled settings.
schema.sql - Tickets · Stores ticket records, status, and message metadata used by the webhook handler.
schema.sql - History log · Keeps conversation evidence and closure history for staff review.
schema.sql - Schema · Production PostgreSQL schema for the support system.
schema.sql - Reset SQL · Destructive rebuild script for restoring the database from scratch.
reset_database.sql
Aux UI
Small Vite/React surface that sits alongside the Telegram workflow.
- React shell · Auxiliary client-side UI surface, not the primary ticketing runtime.
src/App.tsx - App entry · Vite application bootstrap and render entry point.
src/main.tsx - Styles · Client UI styling for the auxiliary React shell.
src/index.css
Deployment
Supabase and Telegram configuration boundary.
- Telegram · User and staff interaction channel, including webhooks, messages, and forum topics.
- Supabase · Managed host for the edge function and PostgreSQL database.
- Secrets · Environment configuration such as bot token, staff group, password, and optional topic/timezone.
.env.example
Connections 22
- Telegram → Ticket bot: webhook updates · Telegram delivers incoming bot events to the edge function.
- Ticket bot → Telegram: send/copy · The function replies to users and staff through the Bot API.
- Ticket bot → Command router: dispatch · Routes admin and control commands to the command handler.
- Ticket bot → Ticket flow: state machine · Coordinates ticket creation, message threading, and closure.
- Ticket flow → Staff topic: route to staff · Places each ticket into its own staff forum topic when available.
- Staff topic → Notes filter: internal notes · Staff-side note messages are filtered out before ticket relay.
- Ticket flow → Tickets: persist · Writes and updates durable ticket state in PostgreSQL.
- Command router → Settings: update config · Admin commands change operational settings stored in the database.
- Ticket flow → History log: close ticket · Records closure history and evidence references.
- Ticket flow → Evidence export: generate · Builds the final evidence document when a ticket is closed.
- Evidence export → Media embed: attach media · Embeds attachments while respecting the 3 MB download cap.
- Evidence export → History log: archive · Stores or references the generated evidence in history records.
- Ticket bot → Schema: reads schema · Operates against the defined PostgreSQL tables and relationships.
- Schema → Settings: defines · Schema contains persistent configuration records.
- Schema → Tickets: defines · Schema contains ticket state tables.
- Schema → History log: defines · Schema contains history and evidence storage.
- Reset SQL → Schema: rebuilds · Reset script recreates the production schema from scratch.
- React shell → App entry: bootstraps · React app starts from the main entry point.
- App entry → Styles: loads · Application entry imports global styling.
- Supabase → Ticket bot: hosts · Supabase runs the serverless bot function.
- Supabase → Schema: stores · Supabase PostgreSQL holds the durable database state.
- Secrets → Ticket bot: configures · Secrets provide bot token, staff group, password, and optional settings.