Build a Custom WeChat Group Chat Bot in 30 Lines of Python

This guide walks you through setting up a lightweight Python-based WeChat group chat assistant using itchat and the Turing chatbot API, covering environment setup, library installation, API key acquisition, and a concise 30‑line script that automates group conversations.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Build a Custom WeChat Group Chat Bot in 30 Lines of Python

Preparation

Python development environment – macOS includes Python by default; Windows users should download Python from the official website and verify the version with python .

Install the itchat library via pip install itchat .

Obtain a free Turing Robot API key by registering on the Turing Robot website.

Implementation of the WeChat Chat Assistant

The following script uses itchat to receive messages and the Turing API to generate replies. It works for both private chats and group chats.

import itchat
import requests
def get_response(msg):
    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {
        'key': '8edce3ce905a4c1dbb965e6b35c3834d',  # Turing Key
        'info': msg,  # message content
        'userid': 'wechat-robot',
    }
    r = requests.post(apiUrl, data=data).json()
    return r.get('text')
@itchat.msg_register(itchat.content.TEXT)
def print_content(msg):
    return get_response(msg['Text'])
@itchat.msg_register([itchat.content.TEXT], isGroupChat=True)
def print_content(msg):
    return get_response(msg['Text'])
itchat.auto_login(True)
itchat.run()

Explanation

The script relies on two libraries: itchat for WeChat interaction and requests for HTTP calls to the Turing API.

@itchat.msg_register(itchat.content.TEXT) handles messages from individual contacts.

@itchat.msg_register([itchat.content.TEXT], isGroupChat=True) handles messages from group chats.

When a message is received, get_response() sends it to the Turing API and returns the reply, which is then sent back to WeChat.

itchat.auto_login(True) displays a QR code for login; after scanning, the bot operates automatically.

Conclusion

The lightweight Python bot demonstrates how a few lines of code can automate WeChat group interactions, and the approach can be extended for various use cases such as product recommendation, moderation, or custom notifications.

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.

PythonAutomationChatbotWeChat botTuring API
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

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.