python wechat robot making tutorial + source code

1, Environment configuration
Most people can't log in to the web version, so spare it to simulate computer login. Remember to install this module:

pip install itchat-uos
pip install itchat

2, Login

#Login to personal wechat account with code
import itchat
itchat.auto_login(hotReload=True)#hotReload= True, the login status can be temporarily saved, and the login can be restarted within a certain time after exiting without scanning the code again.

3, The first simple message sending monitoring
For example, send a message to the file transfer assistant and monitor it in real time. The code is:

# coding=gbk
"""
Author: Chuan Chuan
@time  : 2021/12/24 15:59
"""
import itchat
import time

@itchat.msg_register(itchat.content.TEXT)
def reply_msg(msg):
    print("Received a message:",msg.text)


if __name__ == '__main__':
    itchat.auto_login()
    time.sleep(5)
    itchat.send("Hello, little assistant", toUserName="filehelper")
    itchat.run()

The effects are as follows:


4, Specify a wechat friend to send a message
For example, to automatically send a message to Zhang San, the code is as follows:

#Login to personal wechat account with code
import itchat
itchat.auto_login(hotReload=True)#hotReload= True, the login status can be temporarily saved, and the login can be restarted within a certain time after exiting without scanning the code again.

#Send messages to specific friends
itchat.auto_login(hotReload=True)
friends_list =itchat.get_friends(update=True)
users =itchat.search_friends(name=u'Zhang San')
userName = users[0]["UserName"]
itchat.send('Testing wechat robot...' ,toUserName = userName)

effect:

5, All wechat group monitoring
The effect is as follows: (individual remarks I mosaic)

Test results:

Six. Official account monitoring
effect:
This is to monitor all official account numbers and assign them to my official account: Kawakawa Natori sends messages.

7, Send messages regularly
For example, remember to remind a lazy person to eat every day and execute it at 12 noon every day:

# coding=gbk
"""
Author: Chuan Chuan
@time  : 2021/12/24 16:26
 Group: 970353786
"""
import itchat
from apscheduler.schedulers.blocking import BlockingScheduler


# Send message
def send_msg():
    user_info = itchat.search_friends(name='Little bell')
    if len(user_info) > 0:
        user_name = user_info[0]['UserName']
        itchat.send_msg('Remember to eat, little lazy!', toUserName=user_name)


def after_login():
    # The current task will be executed at 12:00 every day of each month
    sched.add_job(send_msg, 'cron', month='*', day='*', hour='12')
    sched.start()


def after_logout():
    sched.shutdown()


if __name__ == '__main__':
    sched = BlockingScheduler()
    itchat.auto_login(loginCallback=after_login, exitCallback=after_login,hotReload=True)
    itchat.run()

I won't demonstrate it. The code is right.

8, Wechat intelligent chat robot
The effect is:


9, Complete source code

Official account: Kawakawa Natori
 Reply: wechat robot

reminder!
Please do not use this tutorial for illegal content, only for entertainment, otherwise you will not be responsible for all consequences. If you have any questions, you can comment on the district message or the left side of the article, plus WeChat, or official account.

10, Feedback supplement
After feedback from some partners, you still can't log in normally according to my installation, and an error is reported, so I packed all my environment (it may be a little big, but it doesn't matter). You just need to replace your installation package with my installation package.
Find your corresponding installation package path. For example, you can download a module at will:

Copy this path to the file directory and find:

After downloading the file, unzip it and overwrite the original installation package: (that is, overwrite yours as mine)

Installation package download:

Link: https://pan.baidu.com/s/1J-z_XDaeboJ_IAhvyX4xpw 
Extraction code: ibku 
--From Baidu online disk super member V3 Sharing

Original link: https://blog.csdn.net/weixin_...

Keywords: Python

Added by ecaandrew on Tue, 28 Dec 2021 14:36:04 +0200