n8n-nodes-redis-message-aggregator
Version:
n8n custom node để gom tin nhắn từ nhiều trigger với Redis và intelligent delay
474 lines (317 loc) • 12.2 kB
Markdown

# n8n-nodes-redis-message-aggregator
🌐 **Language / Ngôn ngữ:** [English](#english) | [Tiếng Việt](#tiếng-việt)
---
## English
🚀 **n8n custom node for aggregating messages from multiple triggers with Redis and intelligent delay**
Solves the problem of scattered messages in chatbots/webhooks by intelligently combining multiple messages into one with smart waiting strategies.
### 📦 Installation
```bash
npm install n8n-nodes-redis-message-aggregator
```
After installation, restart n8n to load the new nodes.
### ⚡ Key Features
- ✅ **Smart message aggregation**: Automatically combines multiple scattered messages into one
- ✅ **2 processing strategies**: Smart Wait and Immediate on Complete
- ✅ **Real-time waiting**: Node actually waits, no external scheduler needed
- ✅ **Redis support**: Supports local and cloud Redis (Upstash, Redis Cloud)
- ✅ **Connection testing**: Separate Redis connection test node
- ✅ **Flexible keys**: Supports user ID, group ID, or combined keys
### 🎯 Problem Solved
**Before using:**
```
Trigger 1: "Hello" → Process immediately → Response "?"
Trigger 2: "there" → Process immediately → Response "?"
Trigger 3: "world!" → Process immediately → Response "?"
```
**After using:**
```
Trigger 1: "Hello" → Save & wait
Trigger 2: "there" → Save & wait
Trigger 3: "world!" → Save & wait 5s → Aggregate: "Hello there world!" → Response
```
### 🚀 Quick Start
#### Step 1: Setup Redis
**Option A - Upstash Redis (Recommended for beginners):**
1. Sign up at [upstash.com](https://upstash.com) (free tier available)
2. Create a new Redis database
3. Copy connection string like: `redis://default:xxx@xxx.upstash.io:6379`
**Option B - Local Redis:**
```bash
# Docker
docker run -d -p 6379:6379 redis:alpine
# macOS
brew install redis && redis-server
# Ubuntu
sudo apt install redis-server
```
#### Step 2: Create Credentials
1. In n8n, go to **Credentials** → **New** → **Redis API**
2. Choose connection type:
- **Connection String**: Paste connection string from Upstash
- **Host & Port**: Enter localhost:6379 for local Redis
#### Step 3: Test Connection
1. Add **Redis Connection Test** node
2. Select the credentials you just created
3. Choose **Complete Test** and execute
4. Check result: ✅ Connection successful!
#### Step 4: Basic Workflow
```
[Manual Trigger] → [Redis Message Aggregator] → [Set] (to see results)
```
**Configure Redis Message Aggregator:**
- **Key**: `user123` (or any string)
- **Message Content**: `Hello world!`
- **Wait Time**: `5` (seconds)
- **Strategy**: `Smart Wait`
### 📚 Node Details
#### Redis Message Aggregator
Main node for message aggregation with 2 simple strategies:
##### 🧠 Smart Wait (Recommended)
Intelligent waiting - only outputs when certain no new messages are coming.
**When to use:** Chatbots, webhooks receiving consecutive messages
**Example:**
```javascript
// Input: 3 messages with key "user123"
// - "Hello" (10:00:00)
// - "there" (10:00:03)
// - "world!" (10:00:06)
// Output after 5 seconds with no new messages (10:00:11):
{
"status": "messages_aggregated",
"key": "user123",
"aggregatedMessage": "Hello there world!",
"messageCount": 3,
"trigger": "smart_timeout"
}
```
##### 🎯 Immediate on Complete
Returns immediately when detecting message ending with defined words.
**When to use:** When you want quick responses with specific ending words
**Configuration:**
- **End Words**: `done, finished, ok, complete` (comma-separated)
**Example:**
```javascript
// End Words: "done, ok, complete"
"Hello there" → Not complete → Save & wait
"world ok" → Complete (ends with "ok") → Output immediately: "Hello there world ok"
```
#### Redis Connection Test
Node for testing and troubleshooting Redis connections.
**Test Types:**
- **Basic Connection**: Only test PING
- **Read/Write Test**: Test SET/GET/DEL operations
- **Server Info**: Get Redis server information
- **Complete Test**: Run all tests above
### 🔧 Advanced Configuration
#### Key Patterns
Use flexible keys for different scenarios:
```javascript
// 1-on-1 chat
key = 'user_123';
// Group chat
key = 'group_456';
// User in specific group
key = 'user_123_group_456';
// Session based
key = 'session_abc123';
// Multi-tenant
key = 'tenant_A_user_123';
```
#### Strategy Selection Guide
| Strategy | Use Case | Output Timing | Best For |
| --------------------- | ----------------- | -------------------------- | ----------------- |
| Smart Wait | Chatbot responses | After X seconds no new msg | Real-time chat |
| Immediate on Complete | Quick responses | When detect end words | Fast interactions |
### 💡 Workflow Examples
#### Example 1: Chatbot with Smart Wait
```
[Webhook] → [Redis Message Aggregator] → [OpenAI/ChatGPT] → [Response]
```
**Configuration:**
- Key: `{{$json.user_id}}`
- Message: `{{$json.message}}`
- Wait Time: `3` seconds
- Strategy: `Smart Wait`
#### Example 2: Order Processing with Immediate Complete
```
[Webhook] → [Redis Message Aggregator] → [Process Order] → [Confirmation]
```
**Configuration:**
- Key: `{{$json.order_id}}`
- Message: `{{$json.item}}`
- Strategy: `Immediate on Complete`
- End Words: `done, complete, finished`
### 📋 Requirements
- **n8n version**: 0.190.0 or higher
- **Node.js**: 20.15.0 or higher
- **Redis**: Any version (local or cloud)
### 🤝 Contributing
1. Fork the repository
2. Create your feature branch
3. Make your changes
4. Test with real n8n environment
5. Submit a pull request
### 📝 License
MIT License - see [LICENSE.md](LICENSE.md) for details.
### 🔗 Links
- **NPM Package**: https://www.npmjs.com/package/n8n-nodes-redis-message-aggregator
- **GitHub Repository**: https://github.com/nhh0718/n8n-nodes-gom-tin
- **n8n Community**: https://community.n8n.io
---
## Tiếng Việt
🚀 **n8n custom node để gom tin nhắn từ nhiều trigger với Redis và intelligent delay**
Giải quyết vấn đề tin nhắn rời rạc trong chatbot/webhook bằng cách gom nhiều tin nhắn thành một với chiến lược chờ thông minh.
### 📦 Cài đặt
```bash
npm install n8n-nodes-redis-message-aggregator
```
Sau khi cài đặt, restart n8n để load nodes mới.
### ⚡ Tính năng chính
- ✅ **Gom tin nhắn thông minh**: Tự động gom nhiều tin nhắn rời rạc thành một
- ✅ **2 chiến lược xử lý**: Smart Wait và Immediate on Complete
- ✅ **Real-time waiting**: Node thật sự chờ, không cần external scheduler
- ✅ **Redis support**: Hỗ trợ Redis local và cloud (Upstash, Redis Cloud)
- ✅ **Connection testing**: Node test kết nối Redis riêng biệt
- ✅ **Flexible keys**: Hỗ trợ user ID, group ID, hoặc kết hợp
### 🎯 Vấn đề giải quyết
**Trước khi sử dụng:**
```
Trigger 1: "Xin" → Xử lý ngay → Response "?"
Trigger 2: "chào" → Xử lý ngay → Response "?"
Trigger 3: "bạn!" → Xử lý ngay → Response "?"
```
**Sau khi sử dụng:**
```
Trigger 1: "Xin" → Lưu & chờ
Trigger 2: "chào" → Lưu & chờ
Trigger 3: "bạn!" → Lưu & chờ 5s → Gom: "Xin chào bạn!" → Response
```
### 🚀 Bắt đầu nhanh
#### Bước 1: Setup Redis
**Option A - Upstash Redis (Khuyến nghị cho người mới):**
1. Đăng ký tại [upstash.com](https://upstash.com) (free tier)
2. Tạo Redis database mới
3. Copy connection string dạng: `redis://default:xxx@xxx.upstash.io:6379`
**Option B - Redis local:**
```bash
# Docker
docker run -d -p 6379:6379 redis:alpine
# macOS
brew install redis && redis-server
# Ubuntu
sudo apt install redis-server
```
#### Bước 2: Tạo Credentials
1. Trong n8n, vào **Credentials** → **New** → **Redis API**
2. Chọn connection type:
- **Connection String**: Paste connection string từ Upstash
- **Host & Port**: Nhập localhost:6379 cho Redis local
#### Bước 3: Test Connection
1. Thêm node **Redis Connection Test**
2. Chọn credentials vừa tạo
3. Chọn **Complete Test** và execute
4. Xem kết quả: ✅ Connection successful!
#### Bước 4: Basic Workflow
```
[Manual Trigger] → [Redis Message Aggregator] → [Set] (để xem kết quả)
```
**Cấu hình Redis Message Aggregator:**
- **Key**: `user123` (hoặc bất kỳ string nào)
- **Message Content**: `Xin chào!`
- **Wait Time**: `5` (giây)
- **Strategy**: `Smart Wait`
### 📚 Chi tiết Nodes
#### Redis Message Aggregator
Node chính để gom tin nhắn với 2 chiến lược đơn giản:
##### 🧠 Smart Wait (Khuyến nghị)
Chờ thông minh - chỉ output khi chắc chắn không có tin nhắn mới.
**Khi nào dùng:** Chatbot, webhook nhận tin nhắn liên tiếp
**Ví dụ:**
```javascript
// Input: 3 tin nhắn với key "user123"
// - "Xin" (10:00:00)
// - "chào" (10:00:03)
// - "bạn!" (10:00:06)
// Output sau 5 giây không có tin mới (10:00:11):
{
"status": "messages_aggregated",
"key": "user123",
"aggregatedMessage": "Xin chào bạn!",
"messageCount": 3,
"trigger": "smart_timeout"
}
```
##### 🎯 Immediate on Complete
Trả về ngay khi phát hiện tin nhắn kết thúc bằng từ được định nghĩa.
**Khi nào dùng:** Khi muốn response nhanh với những từ kết thúc cụ thể
**Cấu hình:**
- **End Words**: `xong, rồi, nhé, ok, done` (ngăn cách bởi dấu phẩy)
**Ví dụ:**
```javascript
// End Words: "xong, rồi, nhé"
"Xin chào" → Chưa hoàn chỉnh → Lưu & chờ
"bạn nhé" → Hoàn chỉnh (kết thúc bằng "nhé") → Output ngay: "Xin chào bạn nhé"
```
#### Redis Connection Test
Node để test và troubleshoot Redis connection.
**Test Types:**
- **Basic Connection**: Chỉ test PING
- **Read/Write Test**: Test SET/GET/DEL
- **Server Info**: Lấy thông tin Redis server
- **Complete Test**: Chạy tất cả tests above
### 🔧 Cấu hình nâng cao
#### Key Patterns
Sử dụng key linh hoạt cho các tình huống khác nhau:
```javascript
// Chat 1-1
key = 'user_123';
// Group chat
key = 'group_456';
// User trong group cụ thể
key = 'user_123_group_456';
// Session based
key = 'session_abc123';
// Multi-tenant
key = 'tenant_A_user_123';
```
#### Hướng dẫn chọn Strategy
| Strategy | Use Case | Output Timing | Best For |
| --------------------- | ----------------- | ------------------------- | ----------------- |
| Smart Wait | Chatbot responses | Sau X giây no new message | Real-time chat |
| Immediate on Complete | Quick responses | Khi detect end words | Fast interactions |
### 💡 Ví dụ Workflow
#### Ví dụ 1: Chatbot với Smart Wait
```
[Webhook] → [Redis Message Aggregator] → [OpenAI/ChatGPT] → [Response]
```
**Cấu hình:**
- Key: `{{$json.user_id}}`
- Message: `{{$json.message}}`
- Wait Time: `3` giây
- Strategy: `Smart Wait`
#### Ví dụ 2: Xử lý đơn hàng với Immediate Complete
```
[Webhook] → [Redis Message Aggregator] → [Process Order] → [Confirmation]
```
**Cấu hình:**
- Key: `{{$json.order_id}}`
- Message: `{{$json.item}}`
- Strategy: `Immediate on Complete`
- End Words: `xong, hoàn thành, rồi`
### 📋 Yêu cầu hệ thống
- **n8n version**: 0.190.0 trở lên
- **Node.js**: 20.15.0 trở lên
- **Redis**: Bất kỳ version nào (local hoặc cloud)
### 🤝 Đóng góp
1. Fork repository
2. Tạo feature branch
3. Thực hiện thay đổi
4. Test với n8n environment thực tế
5. Tạo pull request
### 📝 License
MIT License - xem [LICENSE.md](LICENSE.md) để biết chi tiết.
### 🔗 Links
- **NPM Package**: https://www.npmjs.com/package/n8n-nodes-redis-message-aggregator
- **GitHub Repository**: https://github.com/nhh0718/n8n-nodes-gom-tin
- **n8n Community**: https://community.n8n.io