📊 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.json2. Configure Credentials
| Credential | For | Config |
|---|---|---|
Telegram account | Bot | BotFather token |
Groq API | AI | Authorization: Bearer gsk_... |
GitHub API | Repo | Authorization: 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
- Enterprise Blog Bot → Toggle ON
- Scheduled Publisher → Toggle ON
📈 Benefits
| Aspect | Before | After |
|---|---|---|
| Server Load | 10 deploys/day | 1 deploy/day |
| Quality | Publish ngay | Review → Approve → Publish |
| UX | 1 command | Interactive conversation |
| Control | None | Queue management |
| Versioning | None | Draft versions saved |
🔒 Security Considerations
- Rate Limiting: Giới hạn 5 bài/user/ngày
- User Whitelist: Chỉ cho phép users cụ thể
- Content Validation: Check content trước khi approve
- 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
| Command | Description | Example |
|---|---|---|
/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 |
/outline | Tạo/xem dàn ý | /outline |
/draft | Tạo bản nháp | /draft |
/preview | Xem trước bài | /preview |
/edit [feedback] | Chỉnh sửa | /edit ngắn gọn hơn |
/approve | Duyệt vào queue | /approve |
/publish | Publish ngay | /publish |
/queue | Xem hàng đợi | /queue |
/status | Trạng thái session | /status |
/cancel | Hủy session | /cancel |
/help | Xem hướng dẫn | /help |