Play WeChat with Python

Python Fun WeChat

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

1. Generate WeChat object

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

2. Find friends, chat objects, friends, groups, and official accounts of WeChat objects separately

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'))

3. Find a friend

friend = bot.friends().search('Yuan Yong')[0]

** 4. Send a message to a friend**

# 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')

5. Statistics of WeChat friends' information, such as male to female ratio, geographical distribution, etc.

bot.friends().stats_text()

** 6. Listen to the news of someone in the group**

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()

7. Connect to the Turing robot and let the robot reply to friend information

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()

**8. Set the maximum number of saved information, and can search **

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)

** 9. Use WeChat to monitor your program**

  1. Get a dedicated logger
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
  1. Designate a group as the message recipient
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)
  1. Send the exception message to the specified object
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

Play WeChat with Python
python requests.get with header
Getting started with Python(9)
Getting started with Python(8)
Getting started with Python(4)
Web Scraping with Python
Getting started with Python (2)
How to play happily with Python3 on Ubuntu
Getting started with python-1
Getting started with Python(14)
Getting started with Python(7)
Getting started with Python(17)
Getting started with Python(15)
Python play pause music
Getting started with Python(10)
Getting started with Python(11)
Getting started with Python(6)
Getting started with Python(3)
Getting started with Python(12)
Getting started with Python(5)
Getting started with Python (18+)
Getting started with Python(13)
Getting started with Python(16)
Python implements WeChat airplane game
Implementing student management system with python
Getting started with Numpy in Python
Centos6.7 comes with python upgrade to
Played with stocks and learned Python
Gray-level co-occurrence matrix (with python code)
Speed up Python code with Cython
Getting Started with Python-4: Classes and Objects
How to make a globe with Python
Automatically generate data analysis report with Python
Getting started with python-2: functions and dictionaries
Create dynamic color QR code with Python
Python | Quickly test your Python code with Hypothesis
How to process excel table with python
04. Conditional Statements for Getting Started with Python
How to get started quickly with Python