Everyone uses WeChat every day. Have you ever thought of using python to control our WeChat? Not much to say, just send dry goods! This is a package made on itchat http://itchat.readthedocs.io/zh/latest/
Install the module
pip3 install wxpy
pip install wxpy -i "https://pypi.doubanio.com/simple/"
# Doubanyuan
bot =Bot() #Initializing an object is equivalent to getting the person's WeChat, and some subsequent operations must be completed with it
bot =Bot(cache_path=True) #Cache login status
friends = bot.friends() #Get friends chats= bot.chats() #Get chat partner
groups = bot.groups() #Get group chat
maps = bot.maps() #Get the official account
# What you get is a list. If you want to get the object, add a corner mark[0]
But this is troublesome
Recommended method, write like this
ensure_one(bot.groups().search('Full-stack development phase 11 off production'))
friend = bot.friends().search('Yuan Yong')[0]
# Send text
my_friend.send('Hello, WeChat!')
# send pictures
my_friend.send_image('my_picture.png')
# Send video
my_friend.send_video('my_video.mov')
# Send File
my_friend.send_file('my_file.zip')
# Send pictures in a dynamic way
my_friend.send('@img@my_picture.png')
bot.friends().stats_text()
from wxpy import*
bot =Bot()
# Target company group
company_group =ensure_one(bot.groups().search('Company WeChat Group'))
# Locate the boss
boss =ensure_one(company_group.search('Boss name'))
# Forward the boss's message to the file transfer assistant
@ bot.register(company_group)
def forward_boss_message(msg):if msg.member == boss:
msg.forward(bot.file_helper, prefix='Boss speaking')
# Block thread
embed()
from wxpy import*import wxpy
from wxpy import*
bot =Bot() #Initializing an object is equivalent to getting the person's WeChat, and some subsequent operations must be completed with it
# me =ensure_one(bot.search('Yuan Yong'))
# me.send('Haha')
all_friends = bot.friends() #Find all my friends
tuling =Tuling(api_key='0f329eba0af742cfb34daa64f9edef8b') #Connect to Turing Robot
for friend in all_friends :
@ bot.register(friend)
def reply_me_friend(msg):
tuling.do_reply(msg)embed()
bot =Bot()
# Set the maximum number of saved historical messages to 10,000
bot.messages.max_history =10000
# Search all self-sent, the text contains'wxpy'News
bot.messages.search('wxpy', sender=bot.self)
wxpy.get_wechat_logger(receiver=None, name=None, level=30)
Obtain a Logger that can send logs to the specified WeChat chat partner
parameter:
receiver –
Treat as None,True or string, the value will be used as cache_The path parameter starts a new robot and sends it to the "file transfer assistant" of the robot
When it is a robot, it will be sent to the "File Transfer Assistant" of the robot
When it is a chat partner, it will be sent to the chat partner
name-Logger name
level-Logger level, the default is logging.WARNING
return:
Logger
from wxpy import*
# Initialize the robot
bot =Bot()
# Find the group that needs to receive logs--`ensure_one()`Used to ensure that the result found is unique and avoid sending the wrong place
group_receiver =ensure_one(bot.groups().search('XX business-Alert notification'))
# Designate this group as the recipient
logger =get_wechat_logger(group_receiver)
logger.error('Excuse me, but this is an important error log...') #The default log level is set to WARNING (log level CRITICAL> ERROR > WARNING > INFO > DEBUG)
from wxpy import get_wechat_logger
# Obtain a dedicated Logger
# When not set`receiver`When the time, the log will be sent to the WeChat account"File transfer assistant"
logger =get_wechat_logger()
# Specify recipients
group_reciver =ensure_one(bot.groups().search('Full-stack development phase 11 off production'))
# Send warning
logger.warning('This is a WARNING log, have you received it?')
# Receive caught exception
try:1/0
except Exception as e
logger.exception(e)
Recommended Posts