feat(telegram): 优化Telegram文件下载与base64转换逻辑,重构消息发送相关代码

- 新增 TelegramModule.download_file_to_base64 方法,统一文件下载与base64编码
- Telegram 客户端新增 download_file 方法,简化文件下载流程
- 消息图片下载逻辑调整为通过模块方法调用,移除冗余静态方法
- 修复部分参数格式与空格风格,提升代码一致性
- 优化长消息发送异常处理与代码结构
This commit is contained in:
jxxghp
2026-03-30 10:28:40 +08:00
parent 70109635c7
commit 9c51f73a72
4 changed files with 131 additions and 122 deletions
+4 -43
View File
@@ -1204,9 +1204,8 @@ class MessageChain(ChainBase):
f"AI智能体处理失败: {str(e)}", role="system", title="MoviePilot助手"
)
@staticmethod
def _download_images_to_base64(
images: List[str], channel: MessageChannel, source: str
self, images: List[str], channel: MessageChannel, source: str
) -> List[str]:
"""
下载图片并转为base64
@@ -1218,7 +1217,9 @@ class MessageChain(ChainBase):
try:
if img.startswith("tg://file_id/"):
file_id = img.replace("tg://file_id/", "")
base64_data = MessageChain._download_telegram_file(file_id, source)
base64_data = self.run_module(
"download_file_to_base64", file_id=file_id, source=source
)
if base64_data:
base64_images.append(f"data:image/jpeg;base64,{base64_data}")
elif img.startswith("http"):
@@ -1232,43 +1233,3 @@ class MessageChain(ChainBase):
except Exception as e:
logger.error(f"下载图片失败: {img}, error: {e}")
return base64_images if base64_images else None
@staticmethod
def _download_telegram_file(file_id: str, source: str) -> Optional[str]:
"""
下载Telegram文件并转为base64
"""
from app.modules.telegram import TelegramModule
module = TelegramModule()
config = module.get_config(source)
if not config:
return None
client = module.get_instance(config.name)
if not client or not client._bot:
return None
try:
file_info = client._bot.get_file(file_id)
file_url = f"https://api.telegram.org/file/bot{client._telegram_token}/{file_info.file_path}"
resp = RequestUtils(timeout=30).get_res(file_url)
if resp and resp.content:
import base64
return base64.b64encode(resp.content).decode()
except Exception as e:
logger.error(f"下载Telegram文件失败: {e}")
return None
client = module.get_instance(config.name)
if not client:
return None
try:
file_info = client.bot.get_file(file_id)
file_url = f"https://api.telegram.org/file/bot{client._telegram_token}/{file_info.file_path}"
resp = RequestUtils(timeout=30).get_res(file_url)
if resp and resp.content:
import base64
return base64.b64encode(resp.content).decode()
except Exception as e:
logger.error(f"下载Telegram文件失败: {e}")
return None