# tud8951/trirelay architecture

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

- Interactive diagram: https://gitdiagram.com/tud8951/trirelay
- Repository: https://github.com/tud8951/trirelay

## Overview

TriRelay is a Cloudflare Worker bridge between Telegram and Discord, with a password-protected web inbox as a second user-facing workflow. Users can send web-form messages and images to either platform; Telegram messages and Discord slash commands can also feed the inbox or bridge content across platforms. The Worker handles webhook and API routes, platform transfers, session checks, and inbox operations, with Cloudflare D1 storing inbox records and Discord attachment metadata. The README documents the main routes and capabilities; sampled source excerpts show selected handler and UI behavior, but not every route implementation.

Main entry points: [`src/frontend.ts`](https://github.com/tud8951/trirelay/blob/main/src/frontend.ts) (Web interface), [`src/index.ts`](https://github.com/tud8951/trirelay/blob/main/src/index.ts) (Worker routes), [`migrations`](https://github.com/tud8951/trirelay/tree/main/migrations) (Inbox database)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_bridge["Message Bridge"]
  node_telegram_inbound["Telegram webhook<br/>[index.ts]"]
  node_discord_interactions["Discord interactions<br/>[index.ts]"]
  node_discord_to_telegram["Discord to Telegram<br/>[index.ts]"]
  node_telegram_to_discord["Telegram to Discord<br/>[index.ts]"]
end

subgraph group_inbox["Web Inbox"]
  node_web_ui["Web interface<br/>[frontend.ts]"]
  node_web_auth["Web session auth<br/>[index.ts]"]
  node_web_send["Web message send<br/>[index.ts]"]
  node_inbox_commands["Inbox commands<br/>[index.ts]"]
  node_inbox_history["Inbox history<br/>[index.ts]"]
  node_image_proxy["Image proxy<br/>[index.ts]"]
  node_inbox_store[("Inbox database")]
end

subgraph group_worker["Worker Runtime"]
  node_worker_entry["Worker routes<br/>[index.ts]"]
  node_command_registration["Command registration<br/>[index.ts]"]
end

subgraph group_platforms["Messaging Platforms"]
  node_telegram{{"Telegram Bot API"}}
  node_discord{{"Discord API"}}
end

node_browser(("Browser user"))
node_telegram_user(("Telegram user"))
node_discord_user(("Discord user"))

node_browser -->|"opens inbox"| node_web_ui
node_web_ui -->|"calls Worker APIs"| node_worker_entry
node_worker_entry -->|"serves interface"| node_web_ui
node_worker_entry -->|"checks sessions"| node_web_auth
node_web_auth -->|"authorizes send"| node_web_send
node_web_send -->|"sends messages"| node_telegram
node_web_send -->|"sends messages"| node_discord
node_telegram_user -->|"sends /web"| node_telegram
node_telegram -->|"posts webhook"| node_telegram_inbound
node_telegram_inbound -->|"forwards content"| node_telegram_to_discord
node_telegram_to_discord -->|"sends content"| node_discord
node_telegram_inbound -->|"routes /web"| node_inbox_commands
node_discord_user -->|"runs commands"| node_discord
node_discord -->|"posts interaction"| node_discord_interactions
node_discord_interactions -->|"dispatches /bridge"| node_discord_to_telegram
node_discord_to_telegram -->|"sends content"| node_telegram
node_discord_interactions -->|"dispatches /web"| node_inbox_commands
node_inbox_commands -->|"writes messages"| node_inbox_store
node_web_send -->|"stores inbox data"| node_inbox_store
node_web_ui -->|"loads history"| node_inbox_history
node_inbox_history -->|"reads messages"| node_inbox_store
node_web_ui -->|"requests previews"| node_image_proxy
node_image_proxy -->|"reads attachment metadata"| node_inbox_store
node_image_proxy -->|"follows CDN links"| node_discord
node_web_ui -->|"registers commands"| node_worker_entry
node_worker_entry -->|"handles registration"| node_command_registration
node_command_registration -->|"registers commands"| node_discord

click node_worker_entry "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_web_ui "https://github.com/tud8951/trirelay/blob/main/src/frontend.ts"
click node_web_auth "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_web_send "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_inbox_commands "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_inbox_history "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_image_proxy "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_inbox_store "https://github.com/tud8951/trirelay/tree/main/migrations"
click node_telegram_inbound "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_discord_interactions "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_discord_to_telegram "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_telegram_to_discord "https://github.com/tud8951/trirelay/blob/main/src/index.ts"
click node_command_registration "https://github.com/tud8951/trirelay/blob/main/src/index.ts"

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_telegram_inbound,node_discord_interactions,node_discord_to_telegram,node_telegram_to_discord,node_browser,node_telegram_user,node_discord_user toneBlue
class node_web_ui,node_web_auth,node_web_send,node_inbox_commands,node_inbox_history,node_image_proxy,node_inbox_store toneAmber
class node_worker_entry,node_command_registration toneMint
class node_telegram,node_discord toneRose
```

## Components

### Message Bridge

- [Telegram webhook](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`
- [Discord interactions](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`
- [Discord to Telegram](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`
- [Telegram to Discord](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`

### Web Inbox

- [Web interface](https://github.com/tud8951/trirelay/blob/main/src/frontend.ts): `src/frontend.ts`
- [Web session auth](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`
- [Web message send](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`
- [Inbox commands](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`
- [Inbox history](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`
- [Image proxy](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`
- [Inbox database](https://github.com/tud8951/trirelay/tree/main/migrations): `migrations`

### Worker Runtime

- [Worker routes](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`
- [Command registration](https://github.com/tud8951/trirelay/blob/main/src/index.ts): `src/index.ts`

### Messaging Platforms

- **Telegram Bot API**
- **Discord API**

### Other components

- **Browser user**
- **Telegram user**
- **Discord user**

## Connections

- Browser user → Web interface: opens inbox (evidence: `README.md`)
- Web interface → Worker routes: calls Worker APIs (evidence: `README.md`)
- Worker routes → Web interface: serves interface (evidence: `src/index.ts`)
- Worker routes → Web session auth: checks sessions (evidence: `src/index.ts`)
- Web session auth → Web message send: authorizes send (evidence: `src/index.ts`)
- Web message send → Telegram Bot API: sends messages (evidence: `README.md`)
- Web message send → Discord API: sends messages (evidence: `README.md`)
- Telegram user → Telegram Bot API: sends /web (evidence: `README.md`)
- Telegram Bot API → Telegram webhook: posts webhook (evidence: `README.md`)
- Telegram webhook → Telegram to Discord: forwards content (evidence: `README.md`)
- Telegram to Discord → Discord API: sends content (evidence: `README.md`)
- Telegram webhook → Inbox commands: routes /web (evidence: `README.md`)
- Discord user → Discord API: runs commands (evidence: `README.md`)
- Discord API → Discord interactions: posts interaction (evidence: `README.md`)
- Discord interactions → Discord to Telegram: dispatches /bridge (evidence: `src/index.ts`)
- Discord to Telegram → Telegram Bot API: sends content (evidence: `README.md`)
- Discord interactions → Inbox commands: dispatches /web (evidence: `src/index.ts`)
- Inbox commands → Inbox database: writes messages (evidence: `README.md`)
- Web message send → Inbox database: stores inbox data (evidence: `README.md`)
- Web interface → Inbox history: loads history (evidence: `README.md`)
- Inbox history → Inbox database: reads messages (evidence: `README.md`)
- Web interface → Image proxy: requests previews (evidence: `README.md`)
- Image proxy → Inbox database: reads attachment metadata (evidence: `README.md`)
- Image proxy → Discord API: follows CDN links (evidence: `README.md`)
- Web interface → Worker routes: registers commands (evidence: `README.md`)
- Worker routes → Command registration: handles registration (evidence: `src/index.ts`)
- Command registration → Discord API: registers commands (evidence: `src/index.ts`)
