How to Build and Extend a Python WeChat Bot with wxpy: Tips, Wrappers, and Common Pitfalls

This article shares practical experience using the Python wxpy library to automate WeChat, detailing outdated API challenges, custom wrapper functions for friends, groups, message recall, image sending, and Turing chatbot integration, along with code snippets and logging tips.

FunTester
FunTester
FunTester
How to Build and Extend a Python WeChat Bot with wxpy: Tips, Wrappers, and Common Pitfalls

I recently used the Python version of WeChat automation with the wxpy wrapper library, connecting a chatbot to the Turing API; other functionalities are not yet implemented.

During development I encountered many issues because the underlying Web WeChat API is outdated—its last update on GitHub was in 2017, so recent changes such as adding friends or detecting @ mentions are not supported.

Friend and group listing : Instead of using the cumbersome search method, I switched to direct index access. The following helper functions enumerate friends and groups and print each item with its index.

def fslist():
    fs = bot.friends()
    a = 0
    for i in fs:
        print(a)
        a += 1
        print(i)

def gslist():
    gs = bot.groups()
    a = 0
    for i in gs:
        print(a)
        a += 1
        print(i)

These functions allow retrieving a friend or group simply by its list index.

Message recall wrapper : I wrapped the original recall() method to make it easier to use during debugging.

def re(*msg):
    if msg != ():
        ms = bot.messages.search(msg)[-1]
        ms.recall()
    else:
        ms = bot.messages[-1]
        ms.recall()
    print(ms)

Sending image files : A small helper sends an image from a local directory and prints a confirmation.

def sendpic(user, name):
    user.send_image('/Users/Vicky/Downloads/' + name)
    print(user)
    print('Sent image ' + name)

Chatbot interaction : The Turing chatbot is integrated via a wrapper function getTulingReplay. It builds the required JSON payload, posts it to the Turing OpenAPI, extracts the reply, prints it, and forwards the response to the friend. Note that the free tier of Turing has been discontinued.

def getTulingReplay(msg, friend):
    userid = ""
    if friend in fs:
        userid = fs.index(friend)
    if friend in gs:
        userid = gs.index(friend)
    j = dict(
        reqType=0,
        perception=dict(
            inputText=dict(
                text=msg
            )
        ),
        userInfo=dict(
            apiKey="***",
            userId=userid
        )
    )
    r = requests.post("http://openapi.tuling123.com/openapi/api/v2", json=j)
    info = json.loads(r.text)["results"]
    c = info[0]["values"]
    d = c.keys()
    m = c[d[0]]
    print(m)
    friend.send(m)

Saving chat logs : The original requirement was to archive group chat records, including recalled messages, for later analysis. Currently the logs are stored locally as plain text and have not yet been cleaned or imported into a database.

Because some Web APIs are no longer available, developers may need to refer to the web version of WeChat for equivalent functionality.

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.

PythonautomationAPIChatbotWeChatBotwxpy
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

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.