找回密码
 立即注册
首页 业界区 业界 基于Python实现微信通知和预警

基于Python实现微信通知和预警

盒礁泅 昨天 18:05
一、功能定义:

  • 当系统正常时给某微信群发送“成功“消息
  • 当系统异常时给某值班人员打微信电话,提醒其登陆系统排查解决问题
二、技术方案:

  • 核心模块:pyautogui,模拟人工鼠标和键盘操作
  • 重要功能:locateCenterOnScreen,利用该图像识别方法可以匹配界面中某个区域,比如需要点击微信聊天框中的“电话”图标。
三、代码实现:
1.gif
2.gif
  1. import pyperclip
  2. import pyautogui
  3. import time
  4. import show_mouse
  5. def locate_tpl(tpl_path, confidence=0.8):
  6.     """模板匹配 -> 返回屏幕绝对坐标"""
  7.     pos = pyautogui.locateCenterOnScreen(tpl_path, confidence=confidence)
  8.     # pos = pyautogui.locateCenterOnScreen(tpl_path)
  9.     if pos is None:
  10.         raise RuntimeError(f'未找到模板 {tpl_path},请确认微信已置顶并重新截图')
  11.     return pos
  12. def open_app_by_search(app_name: str):
  13.     """查找软件并打开"""
  14.     # 1. 按 Win 键呼出开始菜单
  15.     pyautogui.press('win')
  16.     time.sleep(0.5)
  17.     # 2. 输入软件名
  18.     pyautogui.write(app_name, interval=0.1)
  19.     time.sleep(0.1)
  20.     # 3. 回车打开(默认第一项就是)
  21.     pyautogui.press('enter')
  22.     time.sleep(1)          # 给软件启动留时间
  23. def record_active_window():
  24.     """返回当前活动窗口的坐标+尺寸 dict"""
  25.     win = pyautogui.getActiveWindow()     # 当前获得焦点的窗口
  26.     return {
  27.         'title': win.title,
  28.         'left': win.left,
  29.         'top': win.top,
  30.         'width': win.width,
  31.         'height': win.height,
  32.         'box': (win.left, win.top, win.width, win.height)  # region 直接可用
  33.     }
  34. def search_friend_and_open_chatbox(sw_posi_info, friend):
  35.     """搜索某个联系人或者群并打开聊天窗口"""
  36.     # 1. 获取微信左上角输入框位置
  37.     search_input_x = sw_posi_info['left'] + 150
  38.     search_input_y = sw_posi_info['top'] + 32
  39.     # 2. 点击输入框
  40.     pyautogui.click(search_input_x, search_input_y)
  41.     show_mouse.click_with_ring(search_input_x, search_input_y)
  42.     # 3. 输入好友昵称并回车搜索(用 pyperclip 避免中文输入乱码)
  43.     pyperclip.copy(friend)
  44.     pyautogui.hotkey("ctrl", "v")
  45.     time.sleep(1)
  46.     pyautogui.press("enter")
  47.     time.sleep(2)
  48.    
  49. def send_wechat_msg(friend, send_msg):
  50.     """给某个微信好友或者微信群发消息"""
  51.     # 1. 打开微信
  52.     open_app_by_search('weixin')
  53.     pyautogui.press("enter")
  54.     time.sleep(2)
  55.     # 2. 获取软件的坐标
  56.     sw_posi_info = record_active_window()
  57.     #print(sw_posi_info)
  58.     # 3. 搜索联系人并打开聊天框
  59.     search_friend_and_open_chatbox(sw_posi_info, friend)
  60.     # 4. 找到消息输入框,并点击获取焦点
  61.     send_button_x = sw_posi_info['left'] + sw_posi_info['width'] - 100
  62.     send_button_y = sw_posi_info['top'] + sw_posi_info['height'] - 100
  63.     pyautogui.click(send_button_x, send_button_y)
  64.     show_mouse.click_with_ring(send_button_x, send_button_y)
  65.     # 5. 粘贴要发送的消息
  66.     pyperclip.copy(send_msg)
  67.     pyautogui.hotkey("ctrl", "v")
  68.     time.sleep(0.3)
  69.     # 6. 点击发送按钮
  70.     send_button_x = sw_posi_info['left'] + sw_posi_info['width'] - 100
  71.     send_button_y = sw_posi_info['top'] + sw_posi_info['height'] - 32
  72.     pyautogui.click(send_button_x, send_button_y)
  73.     show_mouse.click_with_ring(send_button_x, send_button_y)
  74. def send_wechat_call(friend):
  75.     """给某个微信好友打微信电话"""
  76.     # 1. 打开微信
  77.     open_app_by_search('weixin')
  78.     pyautogui.press("enter")
  79.     time.sleep(2)
  80.     # 2. 获取软件的坐标
  81.     sw_posi_info = record_active_window()
  82.     #print(sw_posi_info)
  83.     # 3. 搜索联系人并打开聊天框
  84.     search_friend_and_open_chatbox(sw_posi_info, friend)
  85.     # 4. 搜索打电话图标的位置
  86.     call_pic = 'wechat_call_pic.png'  # 提前截图准备好的打电话图标
  87.     call_pic_x, call_pic_y = locate_tpl(call_pic)
  88.     #print(call_pic_x)
  89.     #print(call_pic_y)
  90.     # 5. 点击图标并拨打电话
  91.     pyautogui.click(call_pic_x, call_pic_y)
  92.     show_mouse.click_with_ring(call_pic_x, call_pic_y)
复制代码
call_wechat
3.gif
4.gif
[code]import tkinter as tkdef 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

相关推荐

半小时前

举报

您需要登录后才可以回帖 登录 | 立即注册