基于Python实现微信通知和预警
一、功能定义:
- 当系统正常时给某微信群发送“成功“消息
- 当系统异常时给某值班人员打微信电话,提醒其登陆系统排查解决问题
二、技术方案:
- 核心模块:pyautogui,模拟人工鼠标和键盘操作
- 重要功能:locateCenterOnScreen,利用该图像识别方法可以匹配界面中某个区域,比如需要点击微信聊天框中的“电话”图标。
三、代码实现:
import pyperclip import pyautogui import time import show_mouse def locate_tpl(tpl_path, confidence=0.8): """模板匹配 -> 返回屏幕绝对坐标""" pos = pyautogui.locateCenterOnScreen(tpl_path, confidence=confidence) # pos = pyautogui.locateCenterOnScreen(tpl_path) if pos is None: raise RuntimeError(f'未找到模板 {tpl_path},请确认微信已置顶并重新截图') return pos def open_app_by_search(app_name: str): """查找软件并打开""" # 1. 按 Win 键呼出开始菜单 pyautogui.press('win') time.sleep(0.5) # 2. 输入软件名 pyautogui.write(app_name, interval=0.1) time.sleep(0.1) # 3. 回车打开(默认第一项就是) pyautogui.press('enter') time.sleep(1) # 给软件启动留时间 def record_active_window(): """返回当前活动窗口的坐标+尺寸 dict""" win = pyautogui.getActiveWindow() # 当前获得焦点的窗口 return { 'title': win.title, 'left': win.left, 'top': win.top, 'width': win.width, 'height': win.height, 'box': (win.left, win.top, win.width, win.height) # region 直接可用 } def search_friend_and_open_chatbox(sw_posi_info, friend): """搜索某个联系人或者群并打开聊天窗口""" # 1. 获取微信左上角输入框位置 search_input_x = sw_posi_info['left'] + 150 search_input_y = sw_posi_info['top'] + 32 # 2. 点击输入框 pyautogui.click(search_input_x, search_input_y) show_mouse.click_with_ring(search_input_x, search_input_y) # 3. 输入好友昵称并回车搜索(用 pyperclip 避免中文输入乱码) pyperclip.copy(friend) pyautogui.hotkey("ctrl", "v") time.sleep(1) pyautogui.press("enter") time.sleep(2) def send_wechat_msg(friend, send_msg): """给某个微信好友或者微信群发消息""" # 1. 打开微信 open_app_by_search('weixin') pyautogui.press("enter") time.sleep(2) # 2. 获取软件的坐标 sw_posi_info = record_active_window() #print(sw_posi_info) # 3. 搜索联系人并打开聊天框 search_friend_and_open_chatbox(sw_posi_info, friend) # 4. 找到消息输入框,并点击获取焦点 send_button_x = sw_posi_info['left'] + sw_posi_info['width'] - 100 send_button_y = sw_posi_info['top'] + sw_posi_info['height'] - 100 pyautogui.click(send_button_x, send_button_y) show_mouse.click_with_ring(send_button_x, send_button_y) # 5. 粘贴要发送的消息 pyperclip.copy(send_msg) pyautogui.hotkey("ctrl", "v") time.sleep(0.3) # 6. 点击发送按钮 send_button_x = sw_posi_info['left'] + sw_posi_info['width'] - 100 send_button_y = sw_posi_info['top'] + sw_posi_info['height'] - 32 pyautogui.click(send_button_x, send_button_y) show_mouse.click_with_ring(send_button_x, send_button_y) def send_wechat_call(friend): """给某个微信好友打微信电话""" # 1. 打开微信 open_app_by_search('weixin') pyautogui.press("enter") time.sleep(2) # 2. 获取软件的坐标 sw_posi_info = record_active_window() #print(sw_posi_info) # 3. 搜索联系人并打开聊天框 search_friend_and_open_chatbox(sw_posi_info, friend) # 4. 搜索打电话图标的位置 call_pic = 'wechat_call_pic.png' # 提前截图准备好的打电话图标 call_pic_x, call_pic_y = locate_tpl(call_pic) #print(call_pic_x) #print(call_pic_y) # 5. 点击图标并拨打电话 pyautogui.click(call_pic_x, call_pic_y) show_mouse.click_with_ring(call_pic_x, call_pic_y)
import tkinter as tk def click_with_ring(x, y, r=30, ms=400): """在 (x,y) 画一个逐渐消失的圆环,模拟点击特效""" root = tk.Tk() root.overrideredirect(True) # 无边框 root.attributes('-topmost', True) # 置顶 root.attributes('-transparentcolor', 'white') # 白色全透明 root.geometry(f'{r*2}x{r*2}+{x-r}+{y-r}') c = tk.Canvas(root, bg='white', highlightthickness=0) c.pack(fill='both', expand=True) ring = c.create_oval(2, 2, r*2-2, r*2-2, outline='red', width=3) def fade(alpha=255): if alpha <= 0: root.destroy() return c.itemconfig(ring, fill='#%02x0000' % alpha) root.after(int(ms/15), fade, alpha-17) fade() root.mainloop()
四、注意点&Tips:
- 搜索微信群名时,用拼音即可直接定位群名,而用中文反而无法直接定位群(中文搜索可以直接定位好友,群需要再手动选择,多了一步操作)。
- pyautogui运行时,Python脚本后台进程会与前台操作争抢输入(鼠标及键盘),因此代码中要适时的加入一些等待,避免Python后台进程干扰前台操作。
- 该uiautomation/PyOfficeRobot等框架方案相比,pyautogui是模拟人来操作,实际运行一段时间后,在低频操作场景下,发现微信被系统踢出的概率较小。
浙公网安备 33010602011771号