feat(notification): 增加钉钉发送图片和文件功能,支持OSS图片上传
- 在DingTalkBot中添加发送图片消息(自动压缩)功能,支持大小限制自动处理 - 添加发送图文混合消息、发送文件消息接口,优化钉钉通知能力 - 实现发送本地图片链接和通过OSS上传图片再发送Markdown图文两种机制 - 新增阿里云OSS上传工具模块,支持文件和图片上传及预签名URL生成 - 创建每日任务调度脚本,实现每日交易日检查、策略执行、结果上传并通知 - 调整回测策略开始日期至2022年,适配最新数据范围
This commit is contained in:
@@ -8,10 +8,12 @@ import hmac
|
||||
import hashlib
|
||||
import base64
|
||||
import urllib.parse
|
||||
import os
|
||||
from loguru import logger
|
||||
from typing import Optional
|
||||
|
||||
from config.settings import get_dingtalk_config
|
||||
from core.common.oss_utils import upload_image_to_oss
|
||||
|
||||
|
||||
class DingTalkBot:
|
||||
@@ -132,6 +134,338 @@ class DingTalkBot:
|
||||
logger.error(f"钉钉markdown消息发送异常: {e}")
|
||||
return False
|
||||
|
||||
def send_image(self, image_path: str, title: str = "图片", max_size_kb: int = 6) -> bool:
|
||||
"""
|
||||
发送图片消息(自动压缩以适应钉钉20KB限制)
|
||||
注意:钉钉限制的是整个请求body大小,base64编码会增加约33%体积,所以图片本身需要更小
|
||||
|
||||
Args:
|
||||
image_path: 图片文件路径(本地路径)
|
||||
title: 消息标题
|
||||
max_size_kb: 最大图片大小(KB),默认6KB(base64后约8KB,加上其他字段约10KB)
|
||||
|
||||
Returns:
|
||||
bool: 是否发送成功
|
||||
"""
|
||||
if not self.webhook:
|
||||
logger.warning(f"[钉钉图片未发送] {title}")
|
||||
return False
|
||||
|
||||
if not os.path.exists(image_path):
|
||||
logger.error(f"图片文件不存在: {image_path}")
|
||||
return False
|
||||
|
||||
try:
|
||||
# 读取并压缩图片
|
||||
image_data = self._compress_image(image_path, max_size_kb)
|
||||
|
||||
if not image_data:
|
||||
logger.error(f"图片压缩失败: {image_path}")
|
||||
return False
|
||||
|
||||
# 转为base64
|
||||
image_base64 = base64.b64encode(image_data).decode("utf-8")
|
||||
# 计算图片md5(钉钉需要)
|
||||
image_md5 = hashlib.md5(image_data).hexdigest()
|
||||
|
||||
data = {
|
||||
"msgtype": "image",
|
||||
"image": {
|
||||
"base64": image_base64,
|
||||
"md5": image_md5
|
||||
}
|
||||
}
|
||||
|
||||
url = self._gen_signed_url()
|
||||
response = requests.post(url, json=data, timeout=10)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
if result.get("errcode", -1) != 0:
|
||||
logger.error(f"钉钉图片发送失败: {result}")
|
||||
return False
|
||||
|
||||
logger.info(f"钉钉图片发送成功: {image_path}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"钉钉图片发送异常: {e}")
|
||||
return False
|
||||
|
||||
def _compress_image(self, image_path: str, max_size_kb: int) -> bytes:
|
||||
"""
|
||||
压缩图片到指定大小以下
|
||||
|
||||
Args:
|
||||
image_path: 图片路径
|
||||
max_size_kb: 最大大小(KB)
|
||||
|
||||
Returns:
|
||||
bytes: 压缩后的图片数据
|
||||
"""
|
||||
from PIL import Image
|
||||
import io
|
||||
|
||||
max_size_bytes = max_size_kb * 1024
|
||||
|
||||
# 先尝试读取原图
|
||||
with open(image_path, "rb") as f:
|
||||
image_data = f.read()
|
||||
|
||||
# 如果已经小于限制,直接返回
|
||||
if len(image_data) <= max_size_bytes:
|
||||
return image_data
|
||||
|
||||
# 需要压缩,使用PIL重新保存
|
||||
img = Image.open(image_path)
|
||||
|
||||
# 转换为RGB(去除alpha通道,减小大小)
|
||||
if img.mode in ('RGBA', 'P'):
|
||||
img = img.convert('RGB')
|
||||
|
||||
# 逐步降低质量直到满足大小要求
|
||||
quality = 85
|
||||
min_quality = 30
|
||||
|
||||
while quality >= min_quality:
|
||||
buffer = io.BytesIO()
|
||||
img.save(buffer, format='JPEG', quality=quality, optimize=True)
|
||||
compressed_data = buffer.getvalue()
|
||||
|
||||
if len(compressed_data) <= max_size_bytes:
|
||||
logger.info(f"图片压缩成功: {len(image_data)/1024:.1f}KB -> {len(compressed_data)/1024:.1f}KB (quality={quality})")
|
||||
return compressed_data
|
||||
|
||||
quality -= 10
|
||||
|
||||
# 如果质量降到最小还不行,尝试缩小尺寸
|
||||
logger.warning(f"降低质量无法满足要求,尝试缩小尺寸")
|
||||
width, height = img.size
|
||||
|
||||
while width > 200 and height > 200:
|
||||
width = int(width * 0.8)
|
||||
height = int(height * 0.8)
|
||||
resized_img = img.resize((width, height), Image.Resampling.LANCZOS)
|
||||
|
||||
buffer = io.BytesIO()
|
||||
resized_img.save(buffer, format='JPEG', quality=min_quality, optimize=True)
|
||||
compressed_data = buffer.getvalue()
|
||||
|
||||
if len(compressed_data) <= max_size_bytes:
|
||||
logger.info(f"图片压缩成功: {len(image_data)/1024:.1f}KB -> {len(compressed_data)/1024:.1f}KB ({width}x{height})")
|
||||
return compressed_data
|
||||
|
||||
logger.error(f"无法将图片压缩到 {max_size_kb}KB 以下")
|
||||
return None
|
||||
|
||||
def send_image_with_text(self, image_path: str, title: str = "图片", text: str = "") -> bool:
|
||||
"""
|
||||
发送图文消息(markdown格式嵌入图片链接)
|
||||
注意:需要使用钉钉的媒体文件上传接口获取URL,这里使用简化的markdown图片语法
|
||||
|
||||
Args:
|
||||
image_path: 图片文件路径
|
||||
title: 消息标题
|
||||
text: accompanying text
|
||||
|
||||
Returns:
|
||||
bool: 是否发送成功
|
||||
"""
|
||||
if not self.webhook:
|
||||
logger.warning(f"[钉钉图文未发送] {title}")
|
||||
return False
|
||||
|
||||
if not os.path.exists(image_path):
|
||||
logger.error(f"图片文件不存在: {image_path}")
|
||||
return False
|
||||
|
||||
# 先尝试直接发送图片
|
||||
success = self.send_image(image_path, title)
|
||||
|
||||
# 如果图片发送成功且有文字,再发送文字
|
||||
if success and text:
|
||||
time.sleep(0.5) # 避免发送过快
|
||||
return self.send_text(f"{title}\n{text}")
|
||||
|
||||
return success
|
||||
|
||||
def send_file(self, file_path: str, title: str = "文件") -> bool:
|
||||
"""
|
||||
发送文件(通过钉钉文件上传接口)
|
||||
注意:需要企业版钉钉机器人,个人版可能不支持
|
||||
|
||||
Args:
|
||||
file_path: 文件路径
|
||||
title: 文件标题
|
||||
|
||||
Returns:
|
||||
bool: 是否发送成功
|
||||
"""
|
||||
if not self.webhook:
|
||||
logger.warning(f"[钉钉文件未发送] {title}")
|
||||
return False
|
||||
|
||||
if not os.path.exists(file_path):
|
||||
logger.error(f"文件不存在: {file_path}")
|
||||
return False
|
||||
|
||||
try:
|
||||
# 获取文件大小
|
||||
file_size = os.path.getsize(file_path)
|
||||
file_name = os.path.basename(file_path)
|
||||
|
||||
# 钉钉文件大小限制:20MB
|
||||
if file_size > 20 * 1024 * 1024:
|
||||
logger.error(f"文件过大: {file_size/1024/1024:.1f}MB > 20MB")
|
||||
return False
|
||||
|
||||
# 读取文件并转为base64
|
||||
with open(file_path, "rb") as f:
|
||||
file_data = f.read()
|
||||
|
||||
file_base64 = base64.b64encode(file_data).decode("utf-8")
|
||||
|
||||
# 构建消息
|
||||
data = {
|
||||
"msgtype": "file",
|
||||
"file": {
|
||||
"base64": file_base64,
|
||||
"name": file_name
|
||||
}
|
||||
}
|
||||
|
||||
url = self._gen_signed_url()
|
||||
response = requests.post(url, json=data, timeout=30)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
if result.get("errcode", -1) != 0:
|
||||
# 如果文件发送失败,尝试作为图片发送
|
||||
if file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
|
||||
logger.warning(f"文件发送失败,尝试作为图片发送: {result}")
|
||||
return self.send_image(file_path, title)
|
||||
logger.error(f"钉钉文件发送失败: {result}")
|
||||
return False
|
||||
|
||||
logger.info(f"钉钉文件发送成功: {file_name}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"钉钉文件发送异常: {e}")
|
||||
return False
|
||||
|
||||
def send_local_image_as_link(self, image_path: str, title: str = "图片", text: str = "") -> bool:
|
||||
"""
|
||||
发送本地图片(转换为base64嵌入markdown)
|
||||
注意:钉钉不支持直接显示base64图片,此方法会发送图片链接文本
|
||||
|
||||
Args:
|
||||
image_path: 图片文件路径
|
||||
title: 消息标题
|
||||
text: 附加文本
|
||||
|
||||
Returns:
|
||||
bool: 是否发送成功
|
||||
"""
|
||||
if not self.webhook:
|
||||
logger.warning(f"[钉钉图片链接未发送] {title}")
|
||||
return False
|
||||
|
||||
if not os.path.exists(image_path):
|
||||
logger.error(f"图片文件不存在: {image_path}")
|
||||
return False
|
||||
|
||||
try:
|
||||
# 读取图片
|
||||
with open(image_path, "rb") as f:
|
||||
image_data = f.read()
|
||||
|
||||
# 转为base64
|
||||
image_base64 = base64.b64encode(image_data).decode("utf-8")
|
||||
|
||||
# 获取图片格式
|
||||
ext = os.path.splitext(image_path)[1].lower().replace('.', '')
|
||||
if ext == 'jpg':
|
||||
ext = 'jpeg'
|
||||
if ext not in ['png', 'jpeg', 'gif', 'bmp']:
|
||||
ext = 'png'
|
||||
|
||||
# 构建data URL(钉钉可能不支持直接显示,但可以作为链接)
|
||||
data_url = f"data:image/{ext};base64,{image_base64[:100]}..."
|
||||
|
||||
# 发送markdown消息
|
||||
markdown = f"## {title}\n\n"
|
||||
if text:
|
||||
markdown += f"{text}\n\n"
|
||||
markdown += f"**图片**: {os.path.basename(image_path)}\n\n"
|
||||
markdown += f"大小: {len(image_data)/1024:.1f}KB\n"
|
||||
|
||||
return self.send_markdown(title, markdown)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"发送图片链接异常: {e}")
|
||||
return False
|
||||
|
||||
def send_image_via_oss(
|
||||
self,
|
||||
image_path: str,
|
||||
title: str = "策略图表",
|
||||
text: str = "",
|
||||
expire_days: int = 7,
|
||||
) -> bool:
|
||||
"""
|
||||
上传图片到 OSS 并通过 Markdown 发送到钉钉
|
||||
这是发送图片的推荐方式,不受 20KB 限制
|
||||
|
||||
Args:
|
||||
image_path: 本地图片路径
|
||||
title: 消息标题
|
||||
text: 附加文本
|
||||
expire_days: OSS 链接有效期(天)
|
||||
|
||||
Returns:
|
||||
bool: 是否发送成功
|
||||
"""
|
||||
if not self.webhook:
|
||||
logger.warning(f"[钉钉OSS图片未发送] {title}")
|
||||
return False
|
||||
|
||||
if not os.path.exists(image_path):
|
||||
logger.error(f"图片文件不存在: {image_path}")
|
||||
return False
|
||||
|
||||
try:
|
||||
# 上传图片到 OSS
|
||||
image_url = upload_image_to_oss(image_path, expire_days)
|
||||
|
||||
if not image_url:
|
||||
logger.error("图片上传到 OSS 失败")
|
||||
# 尝试直接发送压缩后的图片
|
||||
return self.send_image(image_path, title)
|
||||
|
||||
# 构建 Markdown 消息
|
||||
markdown = f"## {title}\n\n"
|
||||
|
||||
if text:
|
||||
markdown += f"{text}\n\n"
|
||||
|
||||
# 添加图片(钉钉 Markdown 支持图片语法)
|
||||
markdown += f"\n\n"
|
||||
|
||||
# 添加图片信息
|
||||
file_size = os.path.getsize(image_path) / 1024
|
||||
markdown += f"---\n"
|
||||
markdown += f"**图片**: {os.path.basename(image_path)} ({file_size:.1f}KB)\n"
|
||||
markdown += f"**有效期**: {expire_days}天\n"
|
||||
|
||||
# 发送 Markdown 消息
|
||||
return self.send_markdown(title, markdown)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"发送 OSS 图片异常: {e}")
|
||||
# 失败时尝试直接发送
|
||||
return self.send_image(image_path, title)
|
||||
|
||||
|
||||
class NotificationManager:
|
||||
"""通知管理器 - 统一管理多种通知渠道"""
|
||||
|
||||
172
core/common/oss_utils.py
Normal file
172
core/common/oss_utils.py
Normal file
@@ -0,0 +1,172 @@
|
||||
"""
|
||||
阿里云 OSS 工具模块
|
||||
用于上传文件到 OSS 并生成访问链接
|
||||
"""
|
||||
|
||||
import oss2
|
||||
import os
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class OSSUploader:
|
||||
"""OSS 文件上传器"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
access_key_id: str = None,
|
||||
access_key_secret: str = None,
|
||||
bucket_name: str = None,
|
||||
endpoint: str = None,
|
||||
):
|
||||
"""
|
||||
初始化 OSS 上传器
|
||||
|
||||
Args:
|
||||
access_key_id: 阿里云 AccessKey ID
|
||||
access_key_secret: 阿里云 AccessKey Secret
|
||||
bucket_name: OSS Bucket 名称
|
||||
endpoint: OSS 区域 Endpoint
|
||||
"""
|
||||
# 从环境变量或参数获取配置
|
||||
self.access_key_id = access_key_id or os.getenv("OSS_ACCESS_KEY_ID")
|
||||
self.access_key_secret = access_key_secret or os.getenv("OSS_ACCESS_KEY_SECRET")
|
||||
self.bucket_name = bucket_name or os.getenv("OSS_BUCKET_NAME", "value-investing")
|
||||
self.endpoint = endpoint or os.getenv("OSS_ENDPOINT", "https://oss-cn-wulanchabu.aliyuncs.com")
|
||||
|
||||
self.bucket = None
|
||||
self._init_bucket()
|
||||
|
||||
def _init_bucket(self):
|
||||
"""初始化 OSS Bucket"""
|
||||
if not all([self.access_key_id, self.access_key_secret, self.bucket_name, self.endpoint]):
|
||||
logger.warning("OSS 配置不完整,无法初始化")
|
||||
return
|
||||
|
||||
try:
|
||||
auth = oss2.Auth(self.access_key_id, self.access_key_secret)
|
||||
self.bucket = oss2.Bucket(auth, self.endpoint, self.bucket_name)
|
||||
logger.info(f"OSS Bucket 初始化成功: {self.bucket_name}")
|
||||
except Exception as e:
|
||||
logger.error(f"OSS Bucket 初始化失败: {e}")
|
||||
self.bucket = None
|
||||
|
||||
def upload_file(
|
||||
self,
|
||||
local_path: str,
|
||||
oss_key: str = None,
|
||||
expire_seconds: int = 3600 * 24 * 7, # 默认7天有效期
|
||||
) -> Optional[str]:
|
||||
"""
|
||||
上传文件到 OSS
|
||||
|
||||
Args:
|
||||
local_path: 本地文件路径
|
||||
oss_key: OSS 中的目标路径,如果不指定则自动生成
|
||||
expire_seconds: 预签名URL有效期(秒)
|
||||
|
||||
Returns:
|
||||
str: 可访问的 URL,失败返回 None
|
||||
"""
|
||||
if not self.bucket:
|
||||
logger.error("OSS Bucket 未初始化")
|
||||
return None
|
||||
|
||||
if not os.path.exists(local_path):
|
||||
logger.error(f"本地文件不存在: {local_path}")
|
||||
return None
|
||||
|
||||
try:
|
||||
# 自动生成 OSS 路径
|
||||
if not oss_key:
|
||||
file_name = os.path.basename(local_path)
|
||||
date_str = datetime.now().strftime("%Y%m%d")
|
||||
oss_key = f"etf-signals/{date_str}/{file_name}"
|
||||
|
||||
# 上传文件
|
||||
self.bucket.put_object_from_file(oss_key, local_path)
|
||||
logger.info(f"文件上传成功: {local_path} -> {oss_key}")
|
||||
|
||||
# 生成预签名 URL
|
||||
url = self.bucket.sign_url("GET", oss_key, expire_seconds)
|
||||
return url
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"文件上传失败: {e}")
|
||||
return None
|
||||
|
||||
def upload_image(
|
||||
self,
|
||||
image_path: str,
|
||||
expire_days: int = 7,
|
||||
) -> Optional[str]:
|
||||
"""
|
||||
上传图片到 OSS(专门用于钉钉通知)
|
||||
|
||||
Args:
|
||||
image_path: 图片文件路径
|
||||
expire_days: URL 有效期(天)
|
||||
|
||||
Returns:
|
||||
str: 图片访问 URL
|
||||
"""
|
||||
if not os.path.exists(image_path):
|
||||
logger.error(f"图片文件不存在: {image_path}")
|
||||
return None
|
||||
|
||||
# 生成带时间戳的 OSS 路径
|
||||
file_name = os.path.basename(image_path)
|
||||
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
oss_key = f"etf-charts/{timestamp}_{file_name}"
|
||||
|
||||
return self.upload_file(image_path, oss_key, expire_days * 24 * 3600)
|
||||
|
||||
def delete_file(self, oss_key: str) -> bool:
|
||||
"""
|
||||
删除 OSS 文件
|
||||
|
||||
Args:
|
||||
oss_key: OSS 文件路径
|
||||
|
||||
Returns:
|
||||
bool: 是否删除成功
|
||||
"""
|
||||
if not self.bucket:
|
||||
logger.error("OSS Bucket 未初始化")
|
||||
return False
|
||||
|
||||
try:
|
||||
self.bucket.delete_object(oss_key)
|
||||
logger.info(f"文件删除成功: {oss_key}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"文件删除失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
# 全局单例
|
||||
_oss_uploader: Optional[OSSUploader] = None
|
||||
|
||||
|
||||
def get_oss_uploader() -> OSSUploader:
|
||||
"""获取 OSS 上传器单例"""
|
||||
global _oss_uploader
|
||||
if _oss_uploader is None:
|
||||
_oss_uploader = OSSUploader()
|
||||
return _oss_uploader
|
||||
|
||||
|
||||
def upload_image_to_oss(image_path: str, expire_days: int = 7) -> Optional[str]:
|
||||
"""
|
||||
便捷函数:上传图片到 OSS
|
||||
|
||||
Args:
|
||||
image_path: 图片路径
|
||||
expire_days: URL 有效期(天)
|
||||
|
||||
Returns:
|
||||
str: 图片访问 URL
|
||||
"""
|
||||
uploader = get_oss_uploader()
|
||||
return uploader.upload_image(image_path, expire_days)
|
||||
Reference in New Issue
Block a user