方法一,操控企业微信发送消息,条件:需要登录企业微信并置顶群聊
方法二,通过企业微信机器人发送消息,可以不用登录企业微信(推荐)
方法一代码如下:
import os
import time
import win32gui
import win32con
import win32api
import win32clipboard
import pyautogui as pag
from win32gui import IsWindow, IsWindowEnabled, IsWindowVisible, GetWindowText, EnumWindows
class PaySend:
"""
付款申请,企业微信群发送消息
"""
_we_are_one = {}
is_run = False
def __new__(cls, *args, **kwargs):
""" 共享模式一 """
self = object.__new__(cls, *args, **kwargs)
self.__dict__ = cls._we_are_one
return self
def 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 titles
def 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+v
win32api.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 = 1
while self.is_run: # 如果正在执行,等待
time.sleep(4)
c += 1
if c > 100:
break
self.is_run = True
try:
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 += 1
if c > 100:
break
# 如果title窗体正在作业,等待
while win == win2: # 当然当前正在企业微信会话工作,则等待
time.sleep(3)
win2 = win32gui.GetForegroundWindow() # 获取当前窗口句柄
c += 1
if c > 100:
break
win32gui.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 = False
if __name__ == '__main__':
sname = '群成员名称'
txt = f"@{sname} 您有新的消息"
pay = PaySend()
pay.send_mess(txt)
方法二代码如下:
import requests
def 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> [您有新的消息](http://www.xxx.com/info)",
}
}
res = requests.post(url=send_url, headers=headers, json=send_data)
print(res.text)