! [](https://p1.pstatp.com/origin/pgc-image/584f4b4c9bd442ac8f86c50c11e07b40)

Because to micro channel as a learning website login way, with small program and there are some extra costs, so want to micro channel chat window to do some articles, on the Internet to find a lot of micro channel robot methods, most are used old and has been ineffective WXPY, no way, give up?

Overall idea: use automated test library to achieve desired results.

Run environment: PyCharm Python

Code word process

  • Get the wechat PID
  • Connect to wechat
  • Some basic functions of wechat
  • Obtain messages from users Obtain chat information Users find a user on the chat screen search for a user send text messages to a user delete a user
  • Some extra features of wechat
  • Modify Remarks Add A designated friend Add all applied friends Send pictures to a designated user Receive super user commands and handle related tasks Receive common user commands and reply related content Delete all friends Drop out Notify super user drop out Automatically re-log in to other interfaces Complete related tasks Favorites information automatically collect money determine the amount and notify the super user to connect to the database, determine whether the user is within the period of using this function one-click notification message to friends specify friends message Do not Disturb group kick members forward information Send favorites to users Send local files to users
! [](https://p1.pstatp.com/origin/pgc-image/4a1eef1dd7ca4dd0b02f01e6efe8fa11)

Get the wechat PID

Here we use the psutil library, which basically means to get all the processes and iterate through the process ID of wechat

#! /usr/bin/env python # -*- coding:utf-8 -*- # Author:smart_num_1 # Blog:https://blog.csdn.net/smart_num_1 from psutil import process_iter def get_pid(): PID = process_iter() name = '' pid_num = 0 for pid_temp in PID: pid_dic = pid_temp.as_dict(attrs = ['pid','name']) if pid_dic ['name'] == 'WeChat.exe': name = pid_dic ['name'] pid_num = pid_dic ['pid'] break if name =='WeChat.exe': return pid_num else : return FalseCopy the code

Connect to wechat

Pywinauto backend has two types: Win32 and UIA. The default is Win32. You can use the spy++ and Inspect tools to determine which backend is suitable for writing. Backend = 'uia' self. App = Application(backend = 'uia') Self.app.connect (process = PID) self.win = app[u' wechat ']Copy the code

Some basic functions of wechat

Get messages from users

def get_text(self): 
    data = '' 
    try: 
        data = self.win.Edit2.get_value() 
        with open('./temporary_text','w',encoding = 'utf-8') as f: 
            f.write(data) 
    except: 
        pass 
    return data #line:4 
Copy the code

Get chat messages from users

def get_users(self): user_lis = [] try: # Locate the information list, Conunacation = self.win.child_window(title = "session ",control_type = "List" Conunacation. Rectangle () mouse. Click (button = 'left',coords = (position. Left + 100,position. Users = conunacation. Children () for users in users: user_lis.append(user.window_text()) except: pass return user_lisCopy the code

Find a user in the chat screen

def find_user (self ,user = ''): User = self.win.child_window(title = user, control_type = 'Text') Position = user.rectangle () # Mouse. Click (button = 'left',coords = (position.left,position.top)) sleep(0.3)Copy the code

Search for a user

def search_ueser (self ,user_name = ''): The method to find the control is described in the previous function. Search = self.win.child_window(title = "search ", control_type = "Edit") position = search.rectangle() mouse.click(button = 'left', Coords = (position.left + 100,position.top + 10)) sleep(0.1) mouse. Click (button = 'left', Coords = (position.left + 100,position.top + 10) Self.win.type_keys (user_name) sleep(0.6) self.win.type_keys('{ENTER}')Copy the code

Sends a text message to a user

Def send_message (self,texts = "):#line:64 for text in texts. Split ('\n'):#line:65 If text.isalnum(): copy(text.strip()) hotkey (' CTRL ', 'v') else: Self.win. type_keys(text) sleep(0.1) hotkey (' CTRL ', 'Enter ')#line:67 hotkey (' Enter ')#line:68Copy the code

Deleting a User

def delete_user(self ,user =''): user = self.win.child_window(title = user, control_type = 'Text') position = user.rectangle() mouse.click(button = 'right', Click self.app.menu [' delete chat '].click_input('left')Copy the code

Some extra features of wechat

Due to the use of this program, some sensitive contents of the project are involved, so it is not convenient to describe all these functions in detail, but just write about what has been implemented. If someone needs related ideas, please contact wechat :Be_a_luck_dog

  • Modify the remark
  • Add a specified application friend
  • Add all friends in one click
  • Sends the image to the specified user
  • Receive superuser instructions to handle related tasks
  • Receive instructions from ordinary users and reply relevant content
  • Delete all friends in one click
  • Notify the superuser of the drop
  • Offline automatically re-log in
  • Interconnect with other ports to complete related tasks
  • Collect the information
  • Automatic collection determines the amount and notifies the superuser
  • Interconnect with the database to check whether the user is within the validity period of using the function
  • One-click notification to friends
  • Specify DND for friend messages
  • Group kicks out members
  • The forwarding information
  • Send the contents of your favorites to the user
  • Sends a local file to the user

PS: If you need Python learning materials, please click on the link below to obtain them

Free Python learning materials and group communication solutions click to join