2025-07-25 08:19:17 +00:00
|
|
|
|
"""
|
|
|
|
|
|
微信群发功能
|
|
|
|
|
|
建议使用企业微信
|
|
|
|
|
|
但需要管理员提供企业id以及secret信息
|
|
|
|
|
|
通过wechatpy库实现
|
|
|
|
|
|
"""
|
2025-08-15 03:37:06 +00:00
|
|
|
|
import core.logger as logging
|
2025-08-04 13:07:44 +00:00
|
|
|
|
import requests
|
|
|
|
|
|
from config import WECHAT_CONFIG
|
2025-07-25 08:19:17 +00:00
|
|
|
|
|
2025-08-15 03:37:06 +00:00
|
|
|
|
logger = logging.logger
|
2025-08-04 13:07:44 +00:00
|
|
|
|
|
|
|
|
|
|
class Wechat:
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
# 虽然config在根目录,但是取决于调用代码在哪
|
|
|
|
|
|
# 只要启动代码文件在根目录,config就能找到
|
|
|
|
|
|
self.key = WECHAT_CONFIG["key"]
|
|
|
|
|
|
self.url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={self.key}"
|
|
|
|
|
|
|
|
|
|
|
|
def send_text(self, text: str):
|
|
|
|
|
|
"""
|
|
|
|
|
|
发送文本消息
|
|
|
|
|
|
"""
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"msgtype": "text",
|
|
|
|
|
|
"text": {"content": text}
|
|
|
|
|
|
}
|
|
|
|
|
|
response = requests.post(self.url, json=data)
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
def send_markdown(self, text: str):
|
|
|
|
|
|
"""
|
|
|
|
|
|
发送markdown消息
|
|
|
|
|
|
"""
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"msgtype": "markdown_v2",
|
|
|
|
|
|
"markdown_v2": {"content": text}
|
|
|
|
|
|
}
|
|
|
|
|
|
response = requests.post(self.url, json=data)
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
def send_image(self, image_url: str):
|
|
|
|
|
|
"""
|
|
|
|
|
|
发送图片消息
|
|
|
|
|
|
"""
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"msgtype": "image",
|
|
|
|
|
|
"image": {"url": image_url}
|
|
|
|
|
|
}
|
|
|
|
|
|
response = requests.post(self.url, json=data)
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
def send_file(self, file_url: str):
|
|
|
|
|
|
"""
|
|
|
|
|
|
发送文件消息
|
|
|
|
|
|
"""
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"msgtype": "file",
|
|
|
|
|
|
"file": {"url": file_url}
|
|
|
|
|
|
}
|
|
|
|
|
|
response = requests.post(self.url, json=data)
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
def send_news(self, news: list):
|
|
|
|
|
|
"""
|
|
|
|
|
|
发送图文消息
|
|
|
|
|
|
"""
|
|
|
|
|
|
data = {
|
|
|
|
|
|
"msgtype": "news",
|
|
|
|
|
|
"news": {"articles": news}
|
|
|
|
|
|
}
|
|
|
|
|
|
response = requests.post(self.url, json=data)
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|