How to Build a WeChat Public Account Bot with Python: Step‑by‑Step Guide

Learn how to set up a personal WeChat public account, configure a public platform test server on Alibaba Cloud, verify messages via Python and Tornado, and implement basic text, image, and QR‑code interactions using the official WeChat API, complete with code snippets and configuration details.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build a WeChat Public Account Bot with Python: Step‑by‑Step Guide

Background Introduction

First you need a personal WeChat account for testing. For learning you can use the WeChat public platform test sandbox without registering a full official account.

Useful links: http://kf.qq.com/faq/120911VrYVrA130805byM32u.html (FAQ), https://mp.weixin.qq.com/ (registration site), https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login (free sandbox), https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432 (developer documentation).

Connecting to the WeChat Platform

The API usage follows the official documentation. You need to be familiar with the chosen programming language and read the API docs carefully.

Step 1: Configure Server (nginx)

Log into the free sandbox, fill in the interface configuration. Note that the WeChat API only supports port 80, so the URL should point to an nginx web service on a public‑IP server.

Example nginx configuration on an Alibaba Cloud host:

Step 2: Verify Messages Originate from WeChat

After submitting the server URL, WeChat sends a GET request with four parameters: signature, timestamp, nonce, and echostr. The server must compute the signature using the token, timestamp, and nonce, and return echostr unchanged to confirm the connection.

Python example using Tornado:

python wechat.py

After successful verification, the configuration shows as successful in the WeChat platform.

Example 1: Simple Text Echo

When a user follows the account and sends a text message, the server echoes the same text back.

Relevant API: Message Management.

Python code:

python wechat.py

After scanning the test QR code and sending a message, the reply "i like python" is received.

Example 2: Image Message Send/Receive

Modify the code to handle image messages while keeping other parts unchanged.

Example 3: QR Code with Parameters

Generate a QR code that includes a custom parameter (e.g., sid=8000) by running a local server on port 8000. Scanning the QR code in WeChat displays the parameter, enabling further custom logic.

Further advanced features can be added later as needed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentAPINginxWeChatPublic Account
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.