# duckll/pchome_onsale_bot architecture

> Architecture diagram of the GitHub repository duckll/pchome_onsale_bot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/duckll/pchome_onsale_bot
- Repository: https://github.com/duckll/pchome_onsale_bot

## Overview

##### Project type and purpose
- This is an open-source Python “monitor and notify” Telegram bot.
- Purpose: users paste PChome product URLs; the service stores them, fetches prices hourly, and notifies via Telegram only on price drops.

##### File structure
- Top-level:
- Pipfile, Pipfile.lock – Python dependency management
- docker-compose.yml – orchestrates containers
- README.md, LICENSE, .gitignore
- app/ directory contains:
- bot.py – handles incoming Telegram messages (list, delete, add)
- monitor.py – runs the hourly price-check loop
- index.py – likely the webhook or entrypoint for incoming requests
- crontab – cron schedule definition
- init.sh – container startup script (sets up cron + launches services)
- utils.py – HTTP fetching and parsing of PChome pages
- requirements.txt – pinned Python dependencies for the app

##### Key insights from README
- Core features: add via URL, list/delete, hourly fetch, notify on price drops only.
- No database folder—persistence may be a flat file, in-memory, or simple key-value store.

##### Main components
a. External Users/Clients
– Telegram User (mobile/desktop client)
– Telegram Bot API (external service)
– PChome Web Server (for price scraping)
b. Internal Services
– Bot Service Container
- bot.py & index.py: receives Telegram commands via polling or webhook
- utils.py: request/parse product pages
- local store (flat file or in-container DB) for user subscriptions
– Scheduler/Monitor Container (could be the same container with cron)
- cron daemon (defined in app/crontab + init.sh)
- monitor.py: hourly price checks, logic to compare old vs new prices
- triggers notifications via the Bot Service
c. Infrastructure
– Docker Compose: defines and links the bot + scheduler service(s)
– (Optional) Volume or persistent storage for subscription data

##### Relationships & interactions
- User → Telegram Bot API → Bot Service: “/add <URL>”, “/list”, “/delete”
- Bot Service → local storage: save/delete subscription entries
- Cron daemon → Monitor Service: triggers monitor.py hourly
- Monitor Service → PChome Web Server: HTTP GET requests for product pages
- Monitor Service → comparison logic → if price_drop → send message via Bot Service
- Bot Service → Telegram Bot API → User: price-drop notification

##### Architectural patterns & design principles
- Modular separation: one module for command handling (bot.py), one for monitoring (monitor.py), a shared util layer (utils.py).
- Scheduler pattern: cron-based periodic execution.
- Single-responsibility: each script does one job.
- Containerization: environment consistency via Docker + Docker Compose.

- Clear callouts for technologies: Python, Docker, Docker Compose, Cron, Telegram Bot API, HTTP scraping

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Systems
    TelegramUser["Telegram User"]:::external
    TelegramAPI["Telegram Bot API"]:::external
    PChomeWS["PChome Web Server"]:::external

    %% Bot Service Container
    subgraph "Bot Service Container"
        direction TB
        BotIndex["index.py"]:::internal
        BotMain["bot.py"]:::internal
        Utils["utils.py"]:::internal
    end

    %% Subscription Store
    SubsStore["Subscription Store"]:::datastore

    %% Scheduler & Monitor Container
    subgraph "Scheduler & Monitor Container"
        direction TB
        Crontab["crontab"]:::internal
        Init["init.sh"]:::internal
        Monitor["monitor.py"]:::internal
        Utils2["utils.py"]:::internal
    end

    %% Infrastructure & Docs
    Compose["docker-compose.yml"]:::infra
    PipfileNode["Pipfile"]:::infra
    PipfileLockNode["Pipfile.lock"]:::infra
    Reqs["requirements.txt"]:::infra
    Readme["README.md"]:::infra
    License["LICENSE"]:::infra
    Gitignore[".gitignore"]:::infra

    %% Data Flows & Interactions
    TelegramUser -->|"commands (/add /list /delete)"| TelegramAPI
    TelegramAPI -->|"/webhook or polling"| BotIndex
    BotIndex -->|invokes| BotMain
    BotMain -->|uses| Utils
    BotMain -->|read/write| SubsStore
    Crontab -->|"cron triggers"| Monitor
    Init -->|"sets up cron"| Crontab
    Init -->|"launches"| Monitor
    Monitor -->|uses| Utils2
    Monitor -->|"HTTP GET product page"| PChomeWS
    Monitor -->|"fetch prices & compare"| SubsStore
    Monitor -->|"on price drop notify"| BotMain
    BotMain -->|"sendMessage API call"| TelegramAPI
    TelegramAPI -->|"notification"| TelegramUser

    Compose -->|"orchestrates"| BotIndex
    Compose -->|"orchestrates"| Monitor
    Compose -->|"mounts volume"| SubsStore

    %% Click Events
    click BotMain "https://github.com/duckll/pchome_onsale_bot/blob/main/app/bot.py"
    click BotIndex "https://github.com/duckll/pchome_onsale_bot/blob/main/app/index.py"
    click Monitor "https://github.com/duckll/pchome_onsale_bot/blob/main/app/monitor.py"
    click Utils "https://github.com/duckll/pchome_onsale_bot/blob/main/app/utils.py"
    click Crontab "https://github.com/duckll/pchome_onsale_bot/tree/main/app/crontab"
    click Init "https://github.com/duckll/pchome_onsale_bot/blob/main/app/init.sh"
    click Compose "https://github.com/duckll/pchome_onsale_bot/blob/main/docker-compose.yml"
    click PipfileNode "https://github.com/duckll/pchome_onsale_bot/tree/main/Pipfile"
    click PipfileLockNode "https://github.com/duckll/pchome_onsale_bot/blob/main/Pipfile.lock"
    click Reqs "https://github.com/duckll/pchome_onsale_bot/blob/main/app/requirements.txt"
    click Readme "https://github.com/duckll/pchome_onsale_bot/blob/main/README.md"
    click License "https://github.com/duckll/pchome_onsale_bot/tree/main/LICENSE"
    click Gitignore "https://github.com/duckll/pchome_onsale_bot/blob/main/.gitignore"

    %% Styles
    classDef external fill:#D3E5FF,stroke:#1E90FF,color:#000
    classDef internal fill:#D0F0C0,stroke:#228B22,color:#000
    classDef datastore fill:#FFF5BA,stroke:#DAA520,color:#000
    classDef infra fill:#F0E68C,stroke:#BDB76B,color:#000
```
