Turn Your WeChat Account into an Auto‑Reply Bot with Itchat and Turing AI

This article explains how to build a simple WeChat auto‑reply robot in Python using the itchat library, integrate it with the Turing chatbot API, and handle message sending, receiving, and login persistence with concise code examples.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Turn Your WeChat Account into an Auto‑Reply Bot with Itchat and Turing AI

Introduction

Recently I wrote about itchat and now present a simple WeChat auto‑reply robot implemented with about thirty lines of Python code.

Program Overview

The program uses itchat to capture WeChat messages, forwards them to the Turing robot API, receives the reply, and sends it back to the original sender, turning a personal WeChat account into a chatbot.

Installation

pip install itchat

Login

Use itchat.auto_login() to scan a QR code; adding hotReload=True keeps the login state by storing itchat.pkl.

Sending Messages

Use itchat.send('Message Content', 'toUserName') where the second argument is the recipient's UserName, for example sending to the filehelper.

Receiving Messages

Register message handlers with @itchat.msg_register(TEXT) (or other content types). A basic handler can reply with the received text.

@itchat.msg_register(TEXT)
def simple_reply(msg):
    itchat.send_msg('Received: %s' % msg['Text'], toUserName=msg['FromUserName'])
    return "T received: %s" % msg['Text']

Message Types

Picture: itchat.content.PICTURE Recording: itchat.content.RECORDING Card:

itchat.content.CARD

Integrating Turing Bot

Configure the Turing API key and send a POST request to http://www.tuling123.com/openapi/api. The helper function returns the bot's reply.

def get_response(msg):
    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {'key': KEY, 'info': msg, 'userid': 'wechat-robot'}
    try:
        r = requests.post(apiUrl, data=data).json()
        return r.get('text')
    except:
        return

The message handler uses this function and falls back to a default reply when the API fails.

Full Example

import requests
import itchat

KEY = '8edce3ce905a4c1dbb965e6b35c3834d'

def get_response(msg):
    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {'key': KEY, 'info': msg, 'userid': 'wechat-robot'}
    try:
        r = requests.post(apiUrl, data=data).json()
        return r.get('text')
    except:
        return

@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
    defaultReply = 'I received: ' + msg['Text']
    reply = get_response(msg['Text'])
    return reply or defaultReply

itchat.auto_login(hotReload=True)
itchat.run()

Notes

Avoid frequent operations, otherwise Web WeChat may block the login.

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.

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