Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:A616567126 GPT WEB JAVA WeChat Official Account

From Leeroopedia
Knowledge Sources
Domains WeChat_Integration, Bot_Framework
Last Updated 2026-10-13 00:00 GMT

Overview

Integration pattern that receives WeChat Official Account messages via XML callback, routes user commands to handlers, and delivers responses via XML text messages or asynchronous kefu (customer service) messages.

Description

WeChat Official Accounts communicate with backend servers via XML-formatted HTTP callbacks. When a user sends a message to the Official Account, WeChat's server POSTs the message as XML to a registered callback URL. The server must respond synchronously (within 5 seconds) with an XML text message, or use the kefu (customer service) message API for asynchronous responses.

This repository uses the WxJava library to parse XML messages (WxMpXmlMessage.fromXml()), route commands by content prefix matching, and send responses via both synchronous XML and asynchronous kefu messages. Kefu messages support rich content (images, links) that XML responses cannot include.

Usage

Use this principle when:

  • Building a WeChat Official Account bot
  • Needing both synchronous (XML) and asynchronous (kefu) message delivery
  • Routing user commands to different AI services via WeChat

Theoretical Basis

Message Lifecycle:

1. User → WeChat Server: Sends text message "/画画 一只狗"
2. WeChat Server → Backend: POST /wx/callBack (XML body)
3. Backend: Parse XML → WxMpXmlMessage
4. Backend: Route by content prefix → WxOutServiceImpl.draw()
5. Backend → WeChat Server: XML text response (synchronous, <5s)
6. Backend → WeChat API: Kefu message (asynchronous, image + text)

Command Routing by Prefix:

# Abstract algorithm (NOT real implementation)
def route_message(content, from_user):
    if content.startswith("绑定-"):     return bind(content, from_user)
    if content.startswith("开通"):      return opened(content, from_user)
    if content == "查询":               return query(content, from_user)
    if content.startswith("/画画"):     return draw(content, from_user)
    if content.startswith("/U") or "/V": return painting_changes(content, from_user)
    if content.startswith("/咒语解析"): return spell_analysis(content, from_user)
    if content.startswith("兑换"):     return exchange(content, from_user)
    if content.startswith("/recharge"): return recharge(content, from_user)
    ...

Key Properties:

  • XML parsing: WxMpXmlMessage.fromXml(InputStream) (WxJava)
  • Prefix routing: Content prefix determines command handler
  • Kefu for async: WxMpKefuMessage.IMAGE/TEXT for delayed results (MJ task completion)
  • QR ticket login: Temporary QR codes with scene_id for WeChat scan login
  • 5-second limit: Synchronous XML response must be within 5 seconds; kefu for longer operations

Practical Guide

The repository implements this in WxServiceImpl, WxOutServiceImpl, and WxApi:

  1. WxApi.token() — GET endpoint for WeChat signature verification
  2. WxApi.message() — POST endpoint that delegates to WxServiceImpl.callbackEvent()
  3. WxServiceImpl.callbackEvent() — parses XML, handles events (subscribe/scan), routes text commands
  4. WxServiceImpl.wxUserLogin() — binds WeChat user to web session via QR scan
  5. WxOutServiceImpl.draw() — submits MJ task with subType=2 for WeChat delivery
  6. WxApi.getTicket() — creates temporary QR ticket for scan-to-login

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment