Mục lục

Project: Enterprise Blog Bot

Kiến trúc hệ thống Blog Bot cấp độ Enterprise: Quản lý Session, Queue, Scheduled Publishing và tối ưu Vercel Deploy.

📊 Tổng quan hệ thống

Code:
┌─────────────────────────────────────────────────────────────────────────┐
│                         ENTERPRISE BLOG BOT                              │
├─────────────────────────────────────────────────────────────────────────┤
│                                                                          │
│   ┌──────────────┐     ┌──────────────┐     ┌──────────────┐            │
│   │   Telegram   │────▶│   n8n Bot    │────▶│   Session    │            │
│   │     User     │◀────│   Workflow   │◀────│    Store     │            │
│   └──────────────┘     └──────────────┘     └──────────────┘            │
│                               │                    │                     │
│                               ▼                    │                     │
│                        ┌──────────────┐            │                     │
│                        │   Groq AI    │            │                     │
│                        │   (LLaMA)    │            │                     │
│                        └──────────────┘            │                     │
│                               │                    │                     │
│                               ▼                    ▼                     │
│   ┌──────────────┐     ┌──────────────┐     ┌──────────────┐            │
│   │   Scheduled  │────▶│   Publish    │────▶│   GitHub     │            │
│   │   Publisher  │     │    Queue     │     │    Repo      │            │
│   └──────────────┘     └──────────────┘     └──────────────┘            │
│         │                                          │                     │
│         │ 9:00 AM Daily                            │                     │
│         ▼                                          ▼                     │
│   ┌──────────────┐                          ┌──────────────┐            │
│   │    Batch     │─────────────────────────▶│   Vercel     │            │
│   │   Publish    │    (1 deploy/day)        │   Deploy     │            │
│   └──────────────┘                          └──────────────┘            │
│                                                                          │
└─────────────────────────────────────────────────────────────────────────┘

📁 File Structure

Code:
n8n-workflows/
├── enterprise-blog-bot-v1.json    # Main bot workflow
├── scheduled-publisher.json        # Scheduled batch publisher
└── README.md                       # This documentation

🔄 User Flow

Flow 1: Tạo bài viết mới

Code:
User                          Bot                           System
  │                            │                              │
  │ /new Docker tutorial       │                              │
  │ ─────────────────────────▶ │                              │
  │                            │ Create session               │
  │                            │ ─────────────────────────────▶
  │                            │                              │
  │ ✅ Session created         │                              │
  │ ◀───────────────────────── │                              │
  │                            │                              │
  │ /chat thêm phần Compose    │                              │
  │ ─────────────────────────▶ │                              │
  │                            │ Update conversation          │
  │                            │ Call Groq AI                 │
  │                            │ ─────────────────────────────▶
  │                            │                              │
  │ 🤖 AI response             │                              │
  │ ◀───────────────────────── │                              │
  │                            │                              │
  │ /outline                   │                              │
  │ ─────────────────────────▶ │                              │
  │                            │ Generate outline             │
  │                            │ ─────────────────────────────▶
  │                            │                              │
  │ 📋 Outline                 │                              │
  │ ◀───────────────────────── │                              │
  │                            │                              │
  │ /draft                     │                              │
  │ ─────────────────────────▶ │                              │
  │                            │ Generate full draft          │
  │                            │ ─────────────────────────────▶
  │                            │                              │
  │ 📝 Draft preview           │                              │
  │ ◀───────────────────────── │                              │
  │                            │                              │
  │ /edit ngắn gọn intro       │                              │
  │ ─────────────────────────▶ │                              │
  │                            │ Revise with feedback         │
  │                            │ ─────────────────────────────▶
  │                            │                              │
  │ ✏️ Updated draft           │                              │
  │ ◀───────────────────────── │                              │
  │                            │                              │
  │ /approve                   │                              │
  │ ─────────────────────────▶ │                              │
  │                            │ Add to queue                 │
  │                            │ ─────────────────────────────▶
  │                            │                              │
  │ ✅ Added to queue          │                              │
  │ ◀───────────────────────── │                              │
  │                            │                              │

Flow 2: Scheduled Publishing

Code:
Time: 9:00 AM Daily
┌─────────────────┐
│ Load Queue      │
└────────┬────────┘
    Has items?
    │         │
    Yes       No
    │         │
    ▼         ▼
┌─────────┐  Done
│ Process │
│  Batch  │
└────┬────┘
     ▼ (for each article)
┌─────────────────┐
│ Create Branch   │
│ Commit File     │
│ Create PR       │
│ Merge PR        │
│ Notify User     │
└─────────────────┘
   Done
(Only 1 Vercel deploy triggered)

📊 Session State Machine

Code:
┌─────────────┐
                    │    START    │
                    └──────┬──────┘
                           │ /new [topic]
                    ┌─────────────┐
             ┌──────│   TOPIC     │──────┐
             │      │  CONFIRMED  │      │
             │      └─────────────┘      │
             │             │             │
    /chat    │             │ /outline    │ /draft
             │             ▼             │
             │      ┌─────────────┐      │
             │      │  OUTLINED   │      │
             │      └──────┬──────┘      │
             │             │             │
             │             │ /draft      │
             ▼             ▼             ▼
         ┌─────────────────────────────────┐
         │           DRAFTING              │
         │  ┌───────────────────────────┐  │
         │  │  /edit → update draft    │  │
         │  │  /preview → show draft   │  │
         │  │  /chat → refine          │  │
         │  └───────────────────────────┘  │
         └────────────────┬────────────────┘
                          │ /approve
                   ┌─────────────┐
                   │  APPROVED   │
                   │  (in queue) │
                   └──────┬──────┘
                          │ Scheduled publish
                   ┌─────────────┐
                   │  PUBLISHED  │
                   └─────────────┘

🗄️ Data Models

Session Object

javascript:
{
  sessionId: "123456_1704067200000",
  userId: 123456,
  username: "huynv",
  status: "drafting", // topic_confirmed | outlined | drafting | approved | published
  
  topic: {
    type: "tutorial",    // tutorial | comparison | interview | experience | guide
    title: "Docker containers cho beginners",
    raw: "Docker containers cho beginners"
  },
  
  conversation: [
    { role: "user", content: "...", timestamp: 1704067200000 },
    { role: "assistant", content: "...", timestamp: 1704067201000 }
  ],
  
  outline: "## 1. Introduction\n## 2. Prerequisites\n...",
  
  draft: {
    content: "# Docker Tutorial\n\n...",
    wordCount: 1500,
    version: 3
  },
  
  versions: [
    { version: 1, content: "...", timestamp: 1704067300000 },
    { version: 2, content: "...", timestamp: 1704067400000 }
  ],
  
  createdAt: 1704067200000,
  updatedAt: 1704067500000,
  approvedAt: null,
  publishedAt: null
}

Queue Item

javascript:
{
  queueId: "q_abc123",
  sessionId: "123456_1704067200000",
  
  topic: { type: "tutorial", title: "..." },
  content: { base64: "...", wordCount: 1500 },
  
  telegram: { chatId: 123456, username: "huynv" },
  
  priority: 1,  // 1 = normal, 2 = high
  
  scheduledFor: "2024-01-02",  // null = next batch
  
  createdAt: 1704067500000,
  status: "pending"  // pending | publishing | published | failed
}

🛠️ Setup Instructions

1. Import Workflows

bash:
# Import main bot
n8n import:workflow --input=enterprise-blog-bot-v1.json

# Import scheduler
n8n import:workflow --input=scheduled-publisher.json

2. Configure Credentials

CredentialForConfig
Telegram accountBotBotFather token
Groq APIAIAuthorization: Bearer gsk_...
GitHub APIRepoAuthorization: Bearer github_pat_...

3. Setup Session Store (Optional)

Có 3 cách lưu session:

Option A: n8n Static Data (Simple)

javascript:
// Trong Code node, dùng $getWorkflowStaticData()
const staticData = $getWorkflowStaticData('global');
staticData.sessions = staticData.sessions || {};

Option B: Redis (Recommended)

bash:
# docker-compose.yml
services:
  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

Option C: PostgreSQL (Enterprise)

sql:
CREATE TABLE sessions (
  session_id VARCHAR PRIMARY KEY,
  user_id BIGINT,
  status VARCHAR,
  data JSONB,
  created_at TIMESTAMP,
  updated_at TIMESTAMP
);

CREATE TABLE queue (
  queue_id VARCHAR PRIMARY KEY,
  session_id VARCHAR REFERENCES sessions,
  scheduled_for DATE,
  status VARCHAR,
  created_at TIMESTAMP
);

4. Activate Workflows

  1. Enterprise Blog Bot → Toggle ON
  2. Scheduled Publisher → Toggle ON

📈 Benefits

AspectBeforeAfter
Server Load10 deploys/day1 deploy/day
QualityPublish ngayReview → Approve → Publish
UX1 commandInteractive conversation
ControlNoneQueue management
VersioningNoneDraft versions saved

🔒 Security Considerations

  1. Rate Limiting: Giới hạn 5 bài/user/ngày
  2. User Whitelist: Chỉ cho phép users cụ thể
  3. Content Validation: Check content trước khi approve
  4. Audit Log: Lưu lại tất cả actions

🚀 Future Improvements

  • Multi-language support
  • Image generation integration
  • SEO optimization suggestions
  • Analytics dashboard
  • Team collaboration features
  • A/B testing for titles
  • Scheduled social media posts

📞 Commands Reference

CommandDescriptionExample
/new [topic]Bắt đầu session mới/new Docker basics
/chat [msg]Trao đổi với AI/chat thêm phần về volumes
/outlineTạo/xem dàn ý/outline
/draftTạo bản nháp/draft
/previewXem trước bài/preview
/edit [feedback]Chỉnh sửa/edit ngắn gọn hơn
/approveDuyệt vào queue/approve
/publishPublish ngay/publish
/queueXem hàng đợi/queue
/statusTrạng thái session/status
/cancelHủy session/cancel
/helpXem hướng dẫn/help
Quảng cáo
mdhorizontal