Developer Guide · 07
WeChat Integration
Full iLink protocol details, CDN encryption/decryption, and gotcha notes: see
docs/wechat-integration-guide.md.
7.1 Dual-Stack Architecture
text
┌──────────────────────────────────────────────────────────┐
│ Service Channel │ Admin Self-onboarding │
├────────────────────────────────┼─────────────────────────┤
│ /api/wc/* │ /api/admin/wechat/* │
│ router.py │ admin_router.py │
│ bridge.py │ admin_bridge.py │
│ Consumer Agent (channel=wechat│ Admin Agent │
│ Convos in service/conversations│ Convos in admin convos │
│ Sessions in wechat_sessions.json│ admin_wechat_session. │
│ Permissions per service caps │ Full docs/scripts perms │
└────────────────────────────────┴─────────────────────────┘
Shared: client.py / media.py / delivery.py / rate_limiter.py7.2 Key Modules
| Module | Responsibility |
|---|---|
client.py | iLink protocol: getconfig / getupdates / sendmessage / getuploadurl / cdn upload/download |
bridge.py | Service Bridge: message routing + multimodal construction + send_message interception + auto-approve HITL |
admin_bridge.py | Admin Bridge: full permissions + inline send_message handling |
session_manager.py | Session lifecycle + persistence + reconnection (exponential backoff) |
media.py | AES-128-ECB encryption/decryption + CDN upload/download |
delivery.py | Unified delivery (with <<FILE:>> tag parsing) |
rate_limiter.py | Per-user 10/60s + QR 5/60s + global session limit |
router.py / admin_router.py | HTTP API endpoints |
7.3 Multimedia Delivery
| Direction | Flow |
|---|---|
| Receive image | CDN GET → AES decrypt → base64 multimodal → Agent Vision |
| Send image | getuploadurl → AES encrypt → CDN POST → sendmessage(image_item.media) |
| Receive voice | CDN GET → AES decrypt → SILK→WAV (pysilk) → Whisper transcription |
| Send TTS | MP3 under /generated/audio/ sent as file attachment via send_file |
Voice message (voice_item) approach suspended: pysilk-encoded SILK always silent in WeChat client at 24kHz/16kHz. Under further investigation.
7.4 <<FILE:>> Tag Parsing
delivery.py::extract_media_tags(text) -> (cleaned_text, [paths]):
python
_MEDIA_TAG_RE = re.compile(r"<<FILE:([^>]+?)>>")- Actively extracts
<<FILE:path>>tags fromsend_messagetext, converts to additional media delivery - Remaining cleaned_text is sent as text
- Supports both
media_pathparameter and<<FILE:>>tag usage - Bridge and Scheduler share this module, avoiding code duplication
7.5 Session Management Key Points
- Exponential backoff reconnect (auto-removes after 20 failures)
- Sessions with
from_user_iddon't participate in 24h idle cleanup (long-term retention) - Sessions without
from_user_id(empty sessions) are cleaned based on inactivity - Empty polling only discards "not-yet-established" sessions after 50 attempts
- Multi-Admin isolation:
list_sessions/remove_sessionfilter byadmin_id
7.6 Admin Self-Onboarding
- Endpoints:
POST /api/admin/wechat/qrcode/GET qrcode/status/GET/DELETE session/GET messages - Session persistence:
users/{user_id}/admin_wechat_session.json, auto-restored after Docker/restart _save_admin_session()writes on session creation, firstfrom_user_idcapture,context_tokenupdaterestore_admin_sessions()called onmain.pystartupshutdown_admin_sessions()only stops polling/closes connections, does not delete persistence file