Complete Implementation Plan for Enterprise WeChat AI Auto-Reply System (Runable Version)

Below is a complete, production-ready implementation plan for an “Enterprise WeChat AI Bot (DeepSeek + Redis + Worker Architecture)” that integrates all the pitfalls I have already encountered (callback, queue, worker, IP whitelist, send failures, etc.) into a standardized step‑by‑step guide.


I. Overall Architecture Design

This is a typical “event‑driven + queue‑decoupled + AI‑processing” architecture:

Enterprise WeChat User
Enterprise WeChat Server
Nginx / OpenResty (Reverse Proxy + HTTPS)
FastAPI (Callback Reception / Decryption / Enqueue)
Redis Queue (wecom_queue)
Worker (Task Consumer)
      ├── Calls DeepSeek (AI Generation)
Enterprise WeChat Message Send API
User Receives Reply

II. Environment Setup

1. Install Base Dependencies

apt update
apt install -y docker docker-compose

2. Create Project Directory

mkdir -p /opt/ai/wecom-bot
cd /opt/ai/wecom-bot

3. Basic Docker Compose Services

services:
  wecom-bot:
    build: .
    container_name: wecom-bot
    ports:
      - "8000:8000"
    depends_on:
      - redis

  worker:
    build: .
    container_name: wecom-worker
    command: python -m app.worker
    depends_on:
      - redis

  redis:
    image: redis:7-alpine
    container_name: ai-redis

III. FastAPI Callback Service (Core Entry)

File: app/main.py

Core responsibilities:

  • Receive Enterprise WeChat callback
  • Decrypt XML
  • Parse message
  • Write to Redis queue

Key logic:

rdb.rpush("wecom_queue", json.dumps(task))

Callback Flow

Enterprise WeChat POST /wecom/callback
Verify msg_signature
AES decrypt XML
Extract content / from_user
Write to Redis queue
Return "success"

IV. Redis Queue Design

Queue name:

wecom_queue

Data structure:

{
  "content": "User question",
  "from_user": "UserId",
  "ts": 123456789
}

V. Worker (Core Execution Engine)

File: app/worker.py

1. Redis Connection (Critical Optimization)

rdb = redis.Redis(
    host="redis",
    port=6379,
    decode_responses=True,
    socket_timeout=None,
    socket_connect_timeout=5,
    health_check_interval=30
)

2. Consumption Logic

item = rdb.brpop("wecom_queue", timeout=10)

3. Processing Flow

Pop message
Parse JSON
Call DeepSeek
Generate reply
Send to Enterprise WeChat

VI. DeepSeek Calling Module

File: app/deepseek.py

Responsibilities:

  • Receive prompt
  • Call DeepSeek API
  • Return text result

Recommendations:

  • Timeout control
  • Fallback reply

VII. Enterprise WeChat Sending Module

File: app/wecom_sender.py


1. Get access_token (with caching)

get_access_token()

2. Send Message

POST https://qyapi.weixin.qq.com/cgi-bin/message/send

3. Must‑Watch Issues (Critical Pitfalls)

❌ Error 60020 (The Pitfall You Encountered)

not allow to access from your ip

Solution:

In the Enterprise WeChat admin console:

App Management → Development Configuration → IP Whitelist

You must add:

Server public egress IP

VIII. Complete Runtime Flow (Actual Execution Path)

1. User Sends a Message

“Recommendations for Suzhou Stomatology Hospital”

2. Enterprise WeChat Callback

POST /wecom/callback

3. FastAPI Processing

Decrypt → Parse → Enqueue to Redis

4. Worker Consumes

BRPOP wecom_queue

5. Calls DeepSeek

Returns AI reply

6. Sends to Enterprise WeChat

message/send

7. User Receives Reply

List of recommended dental hospitals

IX. Summary of Key Issues (Pitfalls You Have Already Encountered)

1️⃣ IP Whitelist (60020)

Must be configured, otherwise sending messages will fail.


2️⃣ Redis socket timeout

Solution:

socket_timeout=None

3️⃣ Worker blocking exceptions

BRPOP blocks normally; it should not be treated as an exception.


4️⃣ Queue inconsistency

Must be consistent across all components:

wecom_queue

X. System Optimization Suggestions (Advanced)

1. Add Conversation Memory

user_id → history

2. Add Retry Mechanism on Failures

  • DeepSeek retry
  • Send retry

3. Rate Limiting

Prevent API abuse:

1 user / 5s

4. Scale Multiple Workers

worker-1
worker-2
worker-3

5. Integrate with n8n (Already Available)

Can be extended to:

  • Auto‑generate customer analysis
  • Auto‑write marketing content
  • Auto‑export Excel

XI. Final Conclusion

Your system has now reached:

✔ Production‑ready architecture

Capabilities include:

  • Enterprise WeChat callback handling
  • AI auto‑reply
  • Queue decoupling
  • Worker consumption
  • DeepSeek integration
  • Closed‑loop message sending

Next‑Step Upgrades (Suggested Roadmap)

You can upgrade it into a “commercial‑grade version” along one of the following three directions:

  • Retry mechanisms
  • Monitoring
  • Logging system
  • Multiple workers

B. AI Lead‑Generation System Edition

  • Automated scripts
  • Customer profiling
  • Conversion workflows

C. RAG Knowledge‑Base Edition

  • Enterprise knowledge Q&A
  • Vector database
  • Document retrieval augmentation