# geunsam2/korail_ktx_macro_telegrambot architecture

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

- Interactive diagram: https://gitdiagram.com/geunsam2/korail_ktx_macro_telegrambot
- Repository: https://github.com/geunsam2/korail_ktx_macro_telegrambot

## Overview

This is a Telegram bot that monitors sold-out KTX journeys and attempts reservations when seats become available. Users submit credentials and journey preferences through a multi-step conversation; the reservation service coordinates Korail search/reservation, then webhook callbacks report results and initiate payment reminders. Command, admin, subscription, and multi-seat reminder capabilities are also represented. Evidence is limited to the README and indexed excerpts; background-process and some service internals were not fully sampled, so uncertain links are omitted.

Main entry points: [`src/api/telegram_webhook.py`](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/api/telegram_webhook.py) (Webhook routes), [`src/storage/redis.py`](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/storage/redis.py) (Redis state), [`src/handlers/conversation_handler.py`](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/handlers/conversation_handler.py) (Reservation dialogue), [`src/services/korail_service.py`](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/korail_service.py) (Korail search and booking)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["App and Webhooks"]
  node_app["Flask app<br/>[app.py]"]
  node_webhook["Webhook routes"]
  node_payment_api["Payment check<br/>[payment_check.py]"]
  node_settings["Configuration<br/>[settings.py]"]
  node_logger["Logging<br/>[logger.py]"]
end

subgraph group_conversation["User Interaction"]
  node_commands["Bot commands<br/>[command_handler.py]"]
  node_dialog["Reservation dialogue"]
  node_messages["Message templates<br/>[messages.py]"]
  node_validation["Input validation<br/>[validators.py]"]
end

subgraph group_reservation["Reservation Domain"]
  node_reservation_service["Reservation orchestration"]
  node_korail_service["Korail search and booking<br/>[korail_service.py]"]
  node_background["Reservation worker"]
  node_station["Station codes<br/>[station_codes.py]"]
  node_models["Reservation models<br/>[reservation.py]"]
end

subgraph group_state["State and Reminders"]
  node_storage_api["Storage interface<br/>[base.py]"]
  node_redis[("Redis state<br/>[redis.py]")]
  node_telegram_service["Telegram messaging"]
  node_payment_reminder["Payment reminders"]
  node_multi_reminder["Multi-seat reminders"]
end

node_user(("Telegram user"))
node_telegram["Telegram Bot API"]
node_korail["Korail service"]

node_user -->|"sends updates"| node_telegram
node_telegram -->|"posts updates"| node_webhook
node_app -->|"registers routes"| node_webhook
node_webhook -->|"dispatches commands"| node_commands
node_webhook -->|"dispatches messages"| node_dialog
node_dialog -->|"validates input"| node_validation
node_dialog -->|"starts booking"| node_reservation_service
node_commands -->|"reads/writes state"| node_redis
node_dialog -->|"reads/writes sessions"| node_redis
node_dialog -->|"uses templates"| node_messages
node_commands -->|"uses templates"| node_messages
node_reservation_service -->|"searches and reserves"| node_korail_service
node_korail_service -->|"calls Korail API"| node_korail
node_reservation_service -.->|"starts worker"| node_background
node_background -.->|"reports results"| node_webhook
node_webhook -->|"starts reminders"| node_payment_reminder
node_webhook -->|"tracks multi-seat payment"| node_multi_reminder
node_payment_reminder -->|"stores payment state"| node_redis
node_multi_reminder -->|"stores reminder state"| node_redis
node_payment_reminder -->|"sends reminders"| node_telegram_service
node_multi_reminder -->|"sends reminders"| node_telegram_service
node_webhook -->|"sends responses"| node_telegram_service
node_app -->|"configures storage"| node_redis
node_app -->|"loads configuration"| node_settings
node_payment_api -->|"checks payment state"| node_redis
node_storage_api -->|"defines stored types"| node_models
node_redis -->|"implements interface"| node_storage_api
node_korail_service -->|"reads settings"| node_settings
node_webhook -->|"logs events"| node_logger
node_korail_service -->|"logs activity"| node_logger

click node_app "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/app.py"
click node_webhook "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/api/telegram_webhook.py"
click node_payment_api "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/api/payment_check.py"
click node_commands "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/handlers/command_handler.py"
click node_dialog "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/handlers/conversation_handler.py"
click node_messages "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/telegramBot/messages.py"
click node_validation "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/utils/validators.py"
click node_reservation_service "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/reservation_service.py"
click node_korail_service "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/korail_service.py"
click node_background "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/telegramBot/telebotBackProcess.py"
click node_station "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/utils/station_codes.py"
click node_models "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/models/reservation.py"
click node_storage_api "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/storage/base.py"
click node_redis "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/storage/redis.py"
click node_telegram_service "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/telegram_service.py"
click node_payment_reminder "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/payment_reminder_service.py"
click node_multi_reminder "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/multi_reservation_reminder_service.py"
click node_settings "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/config/settings.py"
click node_logger "https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/utils/logger.py"

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_app,node_webhook,node_payment_api,node_settings,node_logger,node_user toneBlue
class node_commands,node_dialog,node_messages,node_validation toneAmber
class node_reservation_service,node_korail_service,node_background,node_station,node_models,node_telegram toneMint
class node_storage_api,node_redis,node_telegram_service,node_payment_reminder,node_multi_reminder toneRose
class node_korail toneIndigo
```

## Components

### App and Webhooks

- [Flask app](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/app.py): `src/app.py`
- [Webhook routes](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/api/telegram_webhook.py): `src/api/telegram_webhook.py`
- [Payment check](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/api/payment_check.py): `src/api/payment_check.py`
- [Configuration](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/config/settings.py): `src/config/settings.py`
- [Logging](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/utils/logger.py): `src/utils/logger.py`

### User Interaction

- [Bot commands](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/handlers/command_handler.py): `src/handlers/command_handler.py`
- [Reservation dialogue](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/handlers/conversation_handler.py): `src/handlers/conversation_handler.py`
- [Message templates](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/telegramBot/messages.py): `src/telegramBot/messages.py`
- [Input validation](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/utils/validators.py): `src/utils/validators.py`

### Reservation Domain

- [Reservation orchestration](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/reservation_service.py): `src/services/reservation_service.py`
- [Korail search and booking](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/korail_service.py): `src/services/korail_service.py`
- [Reservation worker](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/telegramBot/telebotBackProcess.py): `src/telegramBot/telebotBackProcess.py`
- [Station codes](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/utils/station_codes.py): `src/utils/station_codes.py`
- [Reservation models](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/models/reservation.py): `src/models/reservation.py`

### State and Reminders

- [Storage interface](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/storage/base.py): `src/storage/base.py`
- [Redis state](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/storage/redis.py): `src/storage/redis.py`
- [Telegram messaging](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/telegram_service.py): `src/services/telegram_service.py`
- [Payment reminders](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/payment_reminder_service.py): `src/services/payment_reminder_service.py`
- [Multi-seat reminders](https://github.com/geunsam2/korail_ktx_macro_telegrambot/blob/master/src/services/multi_reservation_reminder_service.py): `src/services/multi_reservation_reminder_service.py`

### Other components

- **Telegram user**
- **Telegram Bot API**
- **Korail service**

## Connections

- Telegram user → Telegram Bot API: sends updates (evidence: `README.md`)
- Telegram Bot API → Webhook routes: posts updates (evidence: `README.md`)
- Flask app → Webhook routes: registers routes (evidence: `src/app.py`)
- Webhook routes → Bot commands: dispatches commands (evidence: `src/api/telegram_webhook.py`)
- Webhook routes → Reservation dialogue: dispatches messages (evidence: `src/api/telegram_webhook.py`)
- Reservation dialogue → Input validation: validates input (evidence: `src/handlers/conversation_handler.py`)
- Reservation dialogue → Reservation orchestration: starts booking (evidence: `src/handlers/conversation_handler.py`)
- Bot commands → Redis state: reads/writes state (evidence: `src/handlers/command_handler.py`)
- Reservation dialogue → Redis state: reads/writes sessions (evidence: `src/handlers/conversation_handler.py`)
- Reservation dialogue → Message templates: uses templates (evidence: `src/handlers/conversation_handler.py`)
- Bot commands → Message templates: uses templates (evidence: `src/handlers/command_handler.py`)
- Reservation orchestration → Korail search and booking: searches and reserves (evidence: `src/services/reservation_service.py`)
- Korail search and booking → Korail service: calls Korail API (evidence: `README.md`)
- Reservation orchestration → Reservation worker: starts worker
- Reservation worker → Webhook routes: reports results
- Webhook routes → Payment reminders: starts reminders (evidence: `src/api/telegram_webhook.py`)
- Webhook routes → Multi-seat reminders: tracks multi-seat payment (evidence: `src/api/telegram_webhook.py`)
- Payment reminders → Redis state: stores payment state (evidence: `src/services/payment_reminder_service.py`)
- Multi-seat reminders → Redis state: stores reminder state (evidence: `src/services/multi_reservation_reminder_service.py`)
- Payment reminders → Telegram messaging: sends reminders (evidence: `src/services/payment_reminder_service.py`)
- Multi-seat reminders → Telegram messaging: sends reminders (evidence: `src/services/multi_reservation_reminder_service.py`)
- Webhook routes → Telegram messaging: sends responses (evidence: `src/api/telegram_webhook.py`)
- Flask app → Redis state: configures storage (evidence: `src/app.py`)
- Flask app → Configuration: loads configuration (evidence: `src/app.py`)
- Payment check → Redis state: checks payment state (evidence: `src/api/payment_check.py`)
- Storage interface → Reservation models: defines stored types (evidence: `src/storage/base.py`)
- Redis state → Storage interface: implements interface (evidence: `src/storage/redis.py`)
- Korail search and booking → Configuration: reads settings (evidence: `src/services/korail_service.py`)
- Webhook routes → Logging: logs events (evidence: `src/api/telegram_webhook.py`)
- Korail search and booking → Logging: logs activity (evidence: `src/services/korail_service.py`)
