smart-line-bot / docs /architecture.md
Smiel2's picture
Initial commit
2eae977 verified
|
Raw
History Blame Contribute Delete
8.93 kB

System Architecture

Overview of the Smart LINE Bot + Dashboard + Scraper Pipeline system architecture.

High-Level Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                              Clients                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ LINE    β”‚  β”‚ Web UI   β”‚  β”‚ Mobile  β”‚  β”‚ API    β”‚  β”‚ Webhook      β”‚  β”‚
β”‚  β”‚ Users   β”‚  β”‚ (Admin)  β”‚  β”‚  App    β”‚  β”‚ Client β”‚  β”‚ (External)   β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚       β”‚            β”‚              β”‚            β”‚              β”‚          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚            β”‚              β”‚            β”‚              β”‚
        β–Ό            β–Ό              β–Ό            β–Ό              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                           API Gateway (FastAPI)                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  /api/v1/line    β”‚  /api/v1/dashboard  β”‚  /api/v1/scraping  β”‚ AI  β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                           Request ID Middleware                         β”‚
β”‚                           Rate Limiting                                 β”‚
β”‚                           JWT Authentication                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  LINE Service β”‚    β”‚ Dashboard       β”‚    β”‚ Scraping       β”‚
β”‚  - Messaging  β”‚    β”‚ Service         β”‚    β”‚ Service        β”‚
β”‚  - User Mgmt  β”‚    β”‚ - Users         β”‚    β”‚ - Job Queue    β”‚
β”‚  - Webhooks   β”‚    β”‚ - Stats         β”‚    β”‚ - Botsaurus    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚                     β”‚                     β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό                     β–Ό                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   PostgreSQL     β”‚  β”‚      Redis       β”‚  β”‚   External APIs  β”‚
β”‚   (Primary DB)   β”‚  β”‚   (Cache + MQ)   β”‚  β”‚   - LINE API     β”‚
β”‚                  β”‚  β”‚                  β”‚  β”‚   - OpenAI API   β”‚
β”‚   - Users        β”‚  β”‚   - Celery       β”‚  β”‚   - Scraping     β”‚
β”‚   - LINE Users   β”‚  β”‚     Broker       β”‚  β”‚     Targets      β”‚
β”‚   - Jobs         β”‚  β”‚   - Cache        β”‚  β”‚                  β”‚
β”‚   - Conversationsβ”‚  β”‚   - Sessions     β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό                     β–Ό                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Celery Worker   β”‚  β”‚  Celery Beat     β”‚  β”‚   Scraping       β”‚
β”‚  - Async Tasks   β”‚  β”‚  - Scheduled     β”‚  β”‚   Pipeline       β”‚
β”‚  - LINE Messagesβ”‚  β”‚    Tasks         β”‚  β”‚   - Spiders      β”‚
β”‚  - AI Processing β”‚  β”‚                  β”‚  β”‚   - Pipelines    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Component Details

API Layer (FastAPI)

  • app/main.py: FastAPI application entry point
  • Request ID Middleware: Adds unique ID to every request for tracing
  • Structured Logging: JSON logs with request context
  • CORS Middleware: Configured for allowed origins
  • Error Handlers: Consistent error response format

Services Layer

  • LINE Service: LINE Bot API integration
    • User management
    • Message sending (text, flex, template)
    • OAuth flow handling
  • Dashboard Service: Admin dashboard operations
    • User CRUD
    • Statistics aggregation
    • Operation logging
  • Scraping Service: Web scraping management
    • Job queue management
    • Botsaurus integration
    • Result storage
  • AI Service: AI-powered features
    • LangGraph agent integration
    • Conversation management
    • OpenAI API calls

Data Layer

  • PostgreSQL: Primary database
    • Users, LINE users, scraping jobs, AI conversations
    • Full-text search capabilities
    • Transaction support
  • Redis: Cache and message queue
    • Celery broker
    • Session cache
    • Rate limiting

Task Queue (Celery)

  • Web: API request handling
  • Worker: Async task processing
  • Beat: Scheduled tasks

Data Flow

LINE Message Flow

LINE User β†’ LINE Server β†’ Webhook β†’ FastAPI β†’ LINE Service β†’
  β†’ Celery Task β†’ Process Message β†’ LINE Service β†’ Send Reply

Scraping Flow

User β†’ API β†’ Scraping Service β†’ Create Job β†’
  β†’ Celery Task β†’ Botsaurus Spider β†’ Results β†’
  β†’ Database β†’ Notify User

AI Chat Flow

User β†’ API β†’ AI Service β†’ Create/Update Conversation β†’
  β†’ Celery Task β†’ OpenAI/LangGraph β†’ Response β†’
  β†’ Save to Database β†’ Return to User

Security

Authentication

  • JWT tokens with HS256 algorithm
  • Token expiration: 7 days
  • Refresh tokens: 30 days

Authorization

  • Role-based access control
  • Superuser for admin functions
  • LINE user binding for LINE-specific features

API Security

  • Rate limiting: 60 requests/minute
  • Request validation with Pydantic
  • SQL injection prevention via SQLAlchemy
  • XSS protection

External API Protection

  • Circuit breaker for LINE/OpenAI calls
  • Retry with exponential backoff
  • Request timeout handling

Scalability

Horizontal Scaling

  • Multiple web replicas (load balancer)
  • Multiple worker instances
  • Redis Pub/Sub for inter-instance communication

Caching Strategy

  • Redis for frequently accessed data
  • ETag support for API responses
  • Static file caching

Database Optimization

  • Indexing on common queries
  • Connection pooling
  • Query optimization

Monitoring

Health Checks

  • /health: Basic health
  • /ready: Readiness check (DB, Redis)

Metrics

  • Prometheus metrics endpoint
  • Request/response timing
  • Error rates
  • Celery task statistics

Logging

  • Structured JSON logs
  • Request ID tracking
  • Error stack traces
  • Log levels: DEBUG, INFO, WARNING, ERROR