Build a WeChat Public Account Bot with Python and Nginx

This tutorial walks through setting up a personal WeChat account, registering a public account, configuring an Nginx server on a public‑IP host, verifying WeChat signatures with Python, and implementing simple text, image, and QR‑code interactions using the official API.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build a WeChat Public Account Bot with Python and Nginx

Background

To follow this guide you need a personal WeChat account (no need to register a new one) and access to the WeChat public platform for testing. You can use the official test sandbox without registering a full public account.

Useful links (plain URLs): WeChat public account FAQ: http://kf.qq.com/faq/120911VrYVrA130805byM32u.html Registration site: https://mp.weixin.qq.com/ Sandbox testing: https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login Developer documentation: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432

Tip: Using a server with a public IP and a registered domain simplifies testing; the example uses an Alibaba Cloud host.

Connecting to the WeChat Platform

The integration follows the official API documentation. The process is similar to setting up a Zabbix‑based WeChat alert.

Step 1 – Configure the Server (Nginx)

Log in to the free test platform, fill in the interface configuration, and set the URL to point to your Nginx service (WeChat only calls port 80).

The essential part of the Nginx configuration on the Alibaba Cloud host is shown below:

Step 2 – Verify Requests Come from WeChat

When you submit the interface information, WeChat sends a GET request with four parameters:

1. signature – encrypted signature (combines token, timestamp, nonce)<br/>2. timestamp – time stamp<br/>3. nonce – random number<br/>4. echostr – random string

The server must compute the signature, compare it, and if valid, return the echostr value unchanged.

Python example (using the Tornado framework) to perform this verification:

Run the script: python wechat.py After submitting the configuration on the WeChat platform, a success message appears.

Feature Examples

Example 1 – Simple Text Echo

When a user follows the public account and sends a text message, the bot echoes the same string.

Core Python code (images):

Execute the script: python wechat.py Scanning the test QR code on the public platform and sending a message results in the reply “i like python”.

Example 2 – Image Message Send/Receive

Modify the same code to handle image messages; other logic remains unchanged.

After running, sending a smiley image from the WeChat client results in the same image being echoed.

Example 3 – QR Code with Parameters

Generate a QR code that encodes a custom parameter (e.g., sid=8000) and scan it with WeChat.

Code runs both on a local VM and the Alibaba Cloud host; the local server provides the QR image at http://192.168.1.250:8000/qrcode?sid=8000.

Scanning the QR code displays “You scanned 8000”, demonstrating how to pass custom data into further development.

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.

BackendPythonAPINGINX
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.