# pandagangsters/ticketbot_pro architecture

> Architecture diagram of the GitHub repository pandagangsters/ticketbot_pro, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pandagangsters/ticketbot_pro
- Repository: https://github.com/pandagangsters/ticketbot_pro

## Overview

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: [`supabase/functions/ticketing-bot/index.ts`](https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts) (Ticket bot), [`schema.sql`](https://github.com/pandagangsters/ticketbot_pro/blob/main/schema.sql) (Schema), [`src/main.tsx`](https://github.com/pandagangsters/ticketbot_pro/blob/main/src/main.tsx) (App entry), [`reset_database.sql`](https://github.com/pandagangsters/ticketbot_pro/blob/main/reset_database.sql) (Reset SQL)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Bot runtime"]
  node_edge_fn["Ticket bot<br/>supabase edge fn<br/>[index.ts]"]
  node_cmd_router["Command router<br/>bot logic<br/>[index.ts]"]
  node_ticket_flow["Ticket flow<br/>conversation state<br/>[index.ts]"]
  node_staff_topic["Staff topic<br/>forum routing<br/>[index.ts]"]
  node_notes_filter["Notes filter<br/>message rule<br/>[index.ts]"]
  node_evidence_gen["Evidence export<br/>html report<br/>[index.ts]"]
  node_media_embed["Media embed<br/>attachment handling<br/>[index.ts]"]
end

subgraph group_data["Persistence"]
  node_settings_store[("Settings<br/>db-backed config<br/>[schema.sql]")]
  node_tickets_store[("Tickets<br/>db-backed state<br/>[schema.sql]")]
  node_history_store[("History log<br/>conversation record<br/>[schema.sql]")]
  node_schema["Schema<br/>postgres ddl<br/>[schema.sql]"]
  node_reset_sql["Reset SQL<br/>postgres reset<br/>[reset_database.sql]"]
end

subgraph group_ui["Aux UI"]
  node_frontend["React shell<br/>vite app<br/>[App.tsx]"]
  node_main_tsx["App entry<br/>react entry<br/>[main.tsx]"]
  node_styles["Styles<br/>css<br/>[index.css]"]
end

subgraph group_ops["Deployment"]
  node_telegram(("Telegram<br/>external platform"))
  node_supabase{{"Supabase<br/>platform"}}
  node_secrets["Secrets<br/>runtime config<br/>[.env.example]"]
end

node_telegram -->|"webhook updates"| node_edge_fn
node_edge_fn -->|"send/copy"| node_telegram
node_edge_fn -->|"dispatch"| node_cmd_router
node_edge_fn -->|"state machine"| node_ticket_flow
node_ticket_flow -->|"route to staff"| node_staff_topic
node_staff_topic -->|"internal notes"| node_notes_filter
node_ticket_flow -->|"persist"| node_tickets_store
node_cmd_router -->|"update config"| node_settings_store
node_ticket_flow -->|"close ticket"| node_history_store
node_ticket_flow -->|"generate"| node_evidence_gen
node_evidence_gen -->|"attach media"| node_media_embed
node_evidence_gen -->|"archive"| node_history_store
node_edge_fn -->|"reads schema"| node_schema
node_schema -->|"defines"| node_settings_store
node_schema -->|"defines"| node_tickets_store
node_schema -->|"defines"| node_history_store
node_reset_sql -->|"rebuilds"| node_schema
node_frontend -->|"bootstraps"| node_main_tsx
node_main_tsx -->|"loads"| node_styles
node_supabase -->|"hosts"| node_edge_fn
node_supabase -->|"stores"| node_schema
node_secrets -->|"configures"| node_edge_fn

click node_edge_fn "https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts"
click node_cmd_router "https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts"
click node_ticket_flow "https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts"
click node_staff_topic "https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts"
click node_notes_filter "https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts"
click node_evidence_gen "https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts"
click node_media_embed "https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts"
click node_settings_store "https://github.com/pandagangsters/ticketbot_pro/blob/main/schema.sql"
click node_tickets_store "https://github.com/pandagangsters/ticketbot_pro/blob/main/schema.sql"
click node_history_store "https://github.com/pandagangsters/ticketbot_pro/blob/main/schema.sql"
click node_schema "https://github.com/pandagangsters/ticketbot_pro/blob/main/schema.sql"
click node_reset_sql "https://github.com/pandagangsters/ticketbot_pro/blob/main/reset_database.sql"
click node_frontend "https://github.com/pandagangsters/ticketbot_pro/blob/main/src/App.tsx"
click node_main_tsx "https://github.com/pandagangsters/ticketbot_pro/blob/main/src/main.tsx"
click node_styles "https://github.com/pandagangsters/ticketbot_pro/blob/main/src/index.css"
click node_secrets "https://github.com/pandagangsters/ticketbot_pro/blob/main/.env.example"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_edge_fn,node_cmd_router,node_ticket_flow,node_staff_topic,node_notes_filter,node_evidence_gen,node_media_embed toneBlue
class node_settings_store,node_tickets_store,node_history_store,node_schema,node_reset_sql toneAmber
class node_frontend,node_main_tsx,node_styles toneMint
class node_telegram,node_supabase,node_secrets toneRose
```

## Components

### Bot runtime

Webhook-driven Telegram support orchestration inside a Supabase Edge Function.

- [Ticket bot](https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts): supabase edge fn · Single webhook handler that routes commands, messages, ticket state, and replies. · `supabase/functions/ticketing-bot/index.ts`
- [Command router](https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts): bot logic · Handles admin commands, logout, and operational command paths. · `supabase/functions/ticketing-bot/index.ts`
- [Ticket flow](https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts): conversation state · Manages multi-message ticket creation, open/closed status, and staff/user linkage. · `supabase/functions/ticketing-bot/index.ts`
- [Staff topic](https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts): forum routing · Creates or reuses a dedicated staff-topic thread for each ticket. · `supabase/functions/ticketing-bot/index.ts`
- [Notes filter](https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts): message rule · Ignores staff messages starting with '.' so they remain internal notes. · `supabase/functions/ticketing-bot/index.ts`
- [Evidence export](https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts): html report · Builds the closing evidence document from the full conversation history. · `supabase/functions/ticketing-bot/index.ts`
- [Media embed](https://github.com/pandagangsters/ticketbot_pro/blob/main/supabase/functions/ticketing-bot/index.ts): attachment handling · 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](https://github.com/pandagangsters/ticketbot_pro/blob/main/schema.sql): db-backed config · Persists ticket prefix, history topic ID, and other admin-controlled settings. · `schema.sql`
- [Tickets](https://github.com/pandagangsters/ticketbot_pro/blob/main/schema.sql): db-backed state · Stores ticket records, status, and message metadata used by the webhook handler. · `schema.sql`
- [History log](https://github.com/pandagangsters/ticketbot_pro/blob/main/schema.sql): conversation record · Keeps conversation evidence and closure history for staff review. · `schema.sql`
- [Schema](https://github.com/pandagangsters/ticketbot_pro/blob/main/schema.sql): postgres ddl · Production PostgreSQL schema for the support system. · `schema.sql`
- [Reset SQL](https://github.com/pandagangsters/ticketbot_pro/blob/main/reset_database.sql): postgres reset · 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](https://github.com/pandagangsters/ticketbot_pro/blob/main/src/App.tsx): vite app · Auxiliary client-side UI surface, not the primary ticketing runtime. · `src/App.tsx`
- [App entry](https://github.com/pandagangsters/ticketbot_pro/blob/main/src/main.tsx): react entry · Vite application bootstrap and render entry point. · `src/main.tsx`
- [Styles](https://github.com/pandagangsters/ticketbot_pro/blob/main/src/index.css): css · Client UI styling for the auxiliary React shell. · `src/index.css`

### Deployment

Supabase and Telegram configuration boundary.

- **Telegram**: external platform · User and staff interaction channel, including webhooks, messages, and forum topics.
- **Supabase**: platform · Managed host for the edge function and PostgreSQL database.
- [Secrets](https://github.com/pandagangsters/ticketbot_pro/blob/main/.env.example): runtime config · Environment configuration such as bot token, staff group, password, and optional topic/timezone. · `.env.example`

## Connections

- 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.
