第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > Python 企业微信群推送消息

Python 企业微信群推送消息

时间:2024-02-25 04:24:07

相关推荐

Python 企业微信群推送消息

方法一,操控企业微信发送消息,条件:需要登录企业微信并置顶群聊

方法二,通过企业微信机器人发送消息,可以不用登录企业微信(推荐)

方法一代码如下:

import osimport timeimport win32guiimport win32conimport win32apiimport win32clipboardimport pyautogui as pagfrom win32gui import IsWindow, IsWindowEnabled, IsWindowVisible, GetWindowText, EnumWindowsclass PaySend:"""付款申请,企业微信群发送消息"""_we_are_one = {}is_run = Falsedef __new__(cls, *args, **kwargs):""" 共享模式一 """self = object.__new__(cls, *args, **kwargs)self.__dict__ = cls._we_are_onereturn selfdef get_ct(self):""" 获取所有Windows打开的窗体 """titles = set()def foo(hwnd, mouse):# 去掉下面这句就能获取所有,但是我不需要那么多if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):titles.add(GetWindowText(hwnd))EnumWindows(foo, 0)return titlesdef sbdj(self, x, y, enter=False, txt=''):""" 操控剪贴板粘贴信息并发送 """win32api.SetCursorPos([x, y]) # 为鼠标焦点设定一个位置time.sleep(0.02)win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)time.sleep(0.02)win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)# win32api.keybd_event(0,0,win32con.KEYEVENTF_KEYUP,0)if txt:# 复制到剪切板win32clipboard.OpenClipboard()win32clipboard.EmptyClipboard()win32clipboard.SetClipboardText(txt)win32clipboard.CloseClipboard()# 按下ctrl+vwin32api.keybd_event(0x11, 0, 0, 0)win32api.keybd_event(0x56, 0, 0, 0)win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)if enter:# 按下回车键time.sleep(0.05)# win32api.keybd_event(32, 0, 0, 0)# time.sleep(1)# win32api.keybd_event(32, 0, win32con.KEYEVENTF_KEYUP, 0)win32api.keybd_event(13, 0, 0, 0)win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)def send_mess(self, txt):""" 发送消息 """c = 1while self.is_run: # 如果正在执行,等待time.sleep(4)c += 1if c > 100:breakself.is_run = Truetry:title = '企业微信'# if title not in self.get_ct():#os.startfile(r'D:\Program Files (x86)\WXWork\WXWork.exe')#time.sleep(5)x, y = pag.position() # 原来鼠标坐标win = win32gui.FindWindow(None, title) # 获取标题名称为title的句柄win2 = win32gui.GetForegroundWindow() # 获取当前窗口句柄while win == 0: # 如果没有启动企业微信,启动os.startfile(r'D:\Program Files (x86)\WXWork\WXWork.exe')time.sleep(10)win = win32gui.FindWindow(None, title)c += 1if c > 100:break# 如果title窗体正在作业,等待while win == win2: # 当然当前正在企业微信会话工作,则等待time.sleep(3)win2 = win32gui.GetForegroundWindow() # 获取当前窗口句柄c += 1if c > 100:breakwin32gui.SetForegroundWindow(win) # 前台显示win32gui.ShowWindow(win, win32con.SW_MAXIMIZE) # 最大化self.sbdj(150, 82, enter=False, txt='')time.sleep(0.02)self.sbdj(468, 966, enter=True, txt=txt) win32gui.CloseWindow(win) # 最小化# 为鼠标还原到原来的坐标win32api.SetCursorPos([x, y])win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)# win32gui.CloseWindow(win) # 最小化except Exception as e:print('执行错误')finally:self.is_run = Falseif __name__ == '__main__':sname = '群成员名称'txt = f"@{sname} 您有新的消息"pay = PaySend()pay.send_mess(txt)

方法二代码如下:

import requestsdef send_txt():""" 文本消息 """headers = {"Content-Type": "text/plain"}send_url = "机器人的webhook地址"send_data = {"msgtype": "text", # 消息类型"text": {"content": "您有新的消息", # 文本内容,最长不超过2048个字节,必须是utf8编码# "mentioned_list": ["@all"],# userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list"mentioned_mobile_list": ["13812891678"] # 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人}}res = requests.post(url=send_url, headers=headers, json=send_data)print(res.text)def send_markdown():""" markdown类型消息 """headers = {"Content-Type": "text/plain"}send_url = "机器人的webhook地址"send_data = {"msgtype": "markdown", # 消息类型,此时固定为markdown"markdown": {"content": "<@userid> [您有新的消息](/info)",}}res = requests.post(url=send_url, headers=headers, json=send_data)print(res.text)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。