Short answer: To build a ChatGPT WeChat bot, subscribe to the message webhook, pass the user's message and context to an LLM, then call the send endpoint to reply in WeChat. The round trip usually takes 1–3 seconds.
Architecture
- The hosted account receives a message and POSTs
message.receivedto your webhook. - Your service verifies the signature and decides whether to reply (private chat, or the bot was mentioned in a group).
- Load recent turns and knowledge base snippets, then call ChatGPT or another LLM API for a reply.
- Call
send_textback to the conversation; hand off to a human and tag the user when needed.
Sample code (Python)
import hmac, hashlib, requests
from flask import Flask, request, abort
app = Flask(__name__)
SECRET = b"YOUR_CALLBACK_SECRET"
HEADERS = {"Authorization": "Bearer YOUR_API_KEY"}
def ask_llm(user_text, history):
# call your LLM API with a system prompt, knowledge snippets and history
return "This is the AI reply"
@app.post("/callback")
def callback():
sig = hmac.new(SECRET, request.data, hashlib.sha256).hexdigest()
if not hmac.compare_digest(sig, request.headers.get("X-Signature", "")):
abort(401)
evt = request.get_json()
d = evt.get("data", {})
if evt["event"] == "message.received" and d.get("type") == "text":
if d.get("room_id") and not d.get("at_me"):
return {"ok": True} # ignore group messages that don't mention the bot
reply = ask_llm(d["content"], history=[])
requests.post("https://api.wechatapilabs.com/v1/message/send_text", headers=HEADERS, json={
"account_id": evt["account_id"],
"to": d.get("room_id") or d["from"],
"content": reply,
}, timeout=10)
return {"ok": True}
Going live
- Return within 5 seconds; run the LLM call in an async queue.
- Add a random 1–3 second delay before your ChatGPT WeChat bot replies. It feels more human and lowers risk-control triggers.
- Set hand-off keywords such as "human" or "complaint" that stop auto-replies.
- Keep the last few turns per conversation as context, with an expiry.
Official APIs can't do what you need? See the personal WeChat and WeCom API.
Related: WeChat API overview: Official Accounts, Mini Programs, Pay, Open Platform and WeCom · WeCom webhook bot: how to send group messages · Is there a personal WeChat API? Limits and options