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.
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 itchatLogin
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.CARDIntegrating 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:
returnThe 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
