Python playing wechat

A lot of people are using wechat. Do you want to use Python to control our wechat? Well, let's go straight to the dry goods. We can directly encapsulate it on itchat
http://itchat.readthedocs.io/zh/latest/

Installation module

1 pip install wxpy
2 
3 
4 pip install wxpy -i "https://pypi.doubanio.com/simple " #Source of watercress

1. Generate wechat object

 

1 bot = Bot() #Initializing an object is equivalent to getting this person's wechat. It will do some subsequent operations.

 

 

2. Find friends, chat objects, friends, etc. of wechat objects respectively

 

1 friends = bot.friends() #Get friends
2 chats = bot.chats() #Get chat objects
3 groups = bot.groups() #Get group number
4 maps = bot.maps() #Access to public address
5 
6 #What you get is a list. If you get an object and add a corner marker,[0]
7 #It's very troublesome. I recommend it.
8 ensure_one(bot.groups().search('Family'))

 

3. Find a friend

 

1 friends = bot.friends().search('Fang Hao')[0]

 

4. Send messages to friends

 1 #Send text
 2 my_friend.send('Hello,')
 3 
 4 #Send pictures
 5 my_friend.send_image('01.jpg')
 6 
 7 #Send video
 8 my_friend.send_video('video.mov')
 9 
10 #send files
11 my_friend.send_file('my_file.zip')
12 
13 #Send pictures dynamically
14 my_friend.send('@img@my_prictue.png')

 

5. Count the information of wechat friends, such as the proportion of men and women, etc.

bot.friends().stats_text()

 

6. Monitor someone's information in the group

 1 from wxpy import *
 2 
 3 bot  = Bot()
 4 
 5 #Positioning family groups
 6 company_group = ensure_one(bot.group().search('Family wechat group'))
 7 
 8 #Location boy
 9 di = ensure_one(company_group.search('Little brother'))
10 
11 #Forward my brother's information to wechat assistant
12 @bot.register(company_group)
13 def forward_boss_message(msg):
14         if msg.member == boss
15               msg.forward(bot.file_helper, prefix='Little brother speaks')
16 
17 #Thread jam
18 ember() #banner parameter-Set welcome content and display after entering the command.

7. Connect the Turing robot and let the robot reply to friends' messages

 1 from wxpy import *
 2 import wxpy
 3 from wxpy import *
 4 bot = Bot()   #Initializing an object is equivalent to getting this person's wechat. It is used for subsequent operations.
 5 # me = ensure_one(bot.search('Yuan Yong'))
 6 # me.send('Ha-ha')
 7 all_friends = bot.friends()  # Find all my friends
 8 tuling = Tuling(api_key='0f329eba0af742cfb34daa64f9edef8b') # Access to Turing robot
 9 for friend in all_friends :
10     @bot.register(friend)
11     def reply_me_friend(msg):
12         tuling.do_reply(msg)
13 embed()

8. Set the maximum number of saved information and search

1 bot = Bot()
2 # Set the maximum number of saved historical messages to 10000
3 bot.messages.max_history = 10000
4 
5 # Search all self sent, text contains 'wxpy' News
6 bot.messages.search('wxpy', sender=bot.self)

9. Use wechat to monitor your program

1. Get a special logger

wxpy.get_wechat_logger(receiver=None, name=None, level=30)
Get a Logger that can send logs to the specified wechat chat object

Parameters:    
receiver –
When it is None, True or string, this value will be used as the cache path parameter to start a new robot and send 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 object, it will be sent to that chat object
Name – Logger name
Level – Logger level, the default is logging.WARNING
Return:    
Logger

2. Specify a group as the message receiver

 1 from wxpy import *
 2  
 3 # Initialize robot
 4 bot = Bot()
 5 # Find groups that need to receive logs -- `ensure_one()` Used to ensure that the results found are unique and avoid misplacement
 6 group_receiver = ensure_one(bot.groups().search('XX business-Alarm notification'))
 7   
 8 # Specify this group as the recipient
 9 logger = get_wechat_logger(group_receiver)
10  
11 logger.error('Excuse me, but this is an important error log...')   #The default log level is set to WARNING(Log level level CRITICAL > ERROR > WARNING > INFO > DEBUG)

3. Send the exception message to the specified object

 1 from wxpy import get_wechat_logger
 2 
 3 # Get a dedicated Logger
 4 # When not set `receiver` The log will be sent to the wechat that will scan and log in later."File transfer assistant"
 5 logger = get_wechat_logger()
 6 
 7 #Specify objects to accept
 8 group_reciver = ensure_one(bot.groups().search('Full stack development phase 11'))
 9 
10 # Send a warning
11 logger.warning('This is a piece. WARNING Grade log, have you received it?')
12 
13 # Receive caught exception
14 try:
15     1 / 0
16 except Exception as e 
17     logger.exception(e)

 

 

 

This article is loaded in https://www.cnblogs.com/yyyyyyyyyy/p/9398649.html

Keywords: PHP pip Python

Added by staples27 on Thu, 31 Oct 2019 08:54:04 +0200