SoFunction
Updated on 2024-12-19

Python to realize the WeChat auto-reply method

As we all know, you can set up auto-replies on QQ, but not on WeChat. Recently in the study of Python, found that the scope of application of Python is really very wide, here the use of itchat component to realize the auto-reply of WeChat!

1: Install itchat

pip install itchat

2: Simple examples:

(1): Sending information

import itchat
itchat.auto_login()
name = itchat.search_friends(name=u'XX') #XX indicates a nickname or username
userName = name[0]["UserName"]
print(userName )
itchat.send_msg('。。。', toUserName=userName)

(2):Reply to text messages sent to yourself

import itchat
@itchat.msg_register()
def text_reply(msg):
  return 
itchat.auto_login()
()

3: realize wechat auto-reply

Turing robots are used here.http:///

Register an account to add a bot and then use the interface according to the api documentation to get the bot's return value.

# Getting Turing bot replies to messages
def get_msg(msg):
apiUrl = 'http://openapi./openapi/api/v2'
data = {
  "perception": {
    "inputText": {
      "text": msg
    },
  },
  "userInfo": {
    "apiKey": "cfada3289203426f842746afdc5c0806",
    "userId": "demo"
  }
}
data = (data)
try:
r = (apiUrl,data = data).json()
return r['results'][0]['values']['text']
except:
return ''
#Normal message autoresponder
@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
print()
#Set Default Response
defaultmsg = 'Hello'
# Getting replies from Turing bots
reply = get_msg(msg['Text'])
# If the Turing bot replies to a message with an error then the default reply is used
replymsg = reply or defaultmsg
return replymsg
#Audio, picture autoresponder
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
  ()
  typeSymbol = {
    PICTURE: 'img',
    VIDEO: 'vid', }.get(, 'fil')
  return '@%s@%s' % (typeSymbol, )
#Friend Requests, Auto Add and Greeting
@itchat.msg_register(FRIENDS)
def add_friend(msg):
  ()
  ('Nice to meet you!')
#Group Message Auto-Responder
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
#Set Default Response
defaultmsg = 'Hello'
# Getting replies from Turing bots
reply = get_msg(msg['Text'])
# If the Turing bot replies to a message with an error then the default reply is used
replymsg = reply or defaultmsg
return replymsg
itchat.auto_login(hotReload=True)
(True)

Above is Python to achieve WeChat auto-reply method in detail, more information about python WeChat auto-reply please pay attention to my other related articles!