Add agent image support for Telegram and Slack

This commit is contained in:
jxxghp
2026-04-11 20:40:02 +08:00
parent a4f2c574b0
commit 6c5fae56d9
8 changed files with 244 additions and 17 deletions

View File

@@ -279,12 +279,40 @@ class SlackModule(_ModuleBase, _MessageBase[Slack]):
return None
images = []
for file in files:
if file.get("type") in ("image", "jpg", "jpeg", "png", "gif", "webp"):
file_type = str(file.get("type", "")).lower()
file_ext = str(file.get("filetype", "")).lower()
mime_type = str(file.get("mimetype", "")).lower()
if (
file_type == "image"
or file_ext in ("jpg", "jpeg", "png", "gif", "webp", "bmp")
or mime_type.startswith("image/")
):
url = file.get("url_private") or file.get("url_private_download")
if url:
images.append(url)
return images if images else None
def download_file_to_data_url(self, file_url: str, source: str) -> Optional[str]:
"""
下载Slack文件并转为data URL
:param file_url: Slack私有文件URL
:param source: 来源名称
:return: data URL
"""
config = self.get_config(source)
if not config:
return None
client = self.get_instance(config.name)
if not client:
return None
file_data = client.download_file(file_url)
if file_data:
import base64
content, mime_type = file_data
return f"data:{mime_type};base64,{base64.b64encode(content).decode()}"
return None
def post_message(self, message: Notification, **kwargs) -> None:
"""
发送消息

View File

@@ -1,6 +1,6 @@
import re
from threading import Lock
from typing import List, Optional
from typing import List, Optional, Tuple
from urllib.parse import quote
import requests
@@ -12,6 +12,7 @@ from app.core.config import settings
from app.core.context import MediaInfo, Context
from app.core.metainfo import MetaInfo
from app.log import logger
from app.utils.http import RequestUtils
from app.utils.string import StringUtils
lock = Lock()
@@ -22,6 +23,7 @@ class Slack:
_service: SocketModeHandler = None
_ds_url = f"http://127.0.0.1:{settings.PORT}/api/v1/message?token={settings.API_TOKEN}"
_channel = ""
_oauth_token = ""
def __init__(self, SLACK_OAUTH_TOKEN: Optional[str] = None, SLACK_APP_TOKEN: Optional[str] = None,
SLACK_CHANNEL: Optional[str] = None, **kwargs):
@@ -40,6 +42,7 @@ class Slack:
self._client = slack_app.client
self._channel = SLACK_CHANNEL
self._oauth_token = SLACK_OAUTH_TOKEN
# 标记消息来源
if kwargs.get("name"):
@@ -102,6 +105,28 @@ class Slack:
"""
return True if self._client else False
def download_file(self, file_url: str) -> Optional[Tuple[bytes, str]]:
"""
下载Slack私有文件
:param file_url: Slack文件URL
:return: (文件内容, MIME类型)
"""
if not self._client or not self._oauth_token or not file_url:
return None
try:
headers = {
"Authorization": f"Bearer {self._oauth_token}",
"User-Agent": settings.USER_AGENT,
"Accept": "*/*",
}
resp = RequestUtils(headers=headers, timeout=30).get_res(file_url)
if resp and resp.content:
mime_type = resp.headers.get("Content-Type", "image/jpeg")
return resp.content, mime_type.split(";")[0]
except Exception as e:
logger.error(f"下载Slack文件失败: {e}")
return None
def send_msg(self, title: str, text: Optional[str] = None,
image: Optional[str] = None, link: Optional[str] = None,
userid: Optional[str] = None, buttons: Optional[List[List[dict]]] = None,

View File

@@ -267,14 +267,14 @@ class TelegramModule(_ModuleBase, _MessageBase[Telegram]):
largest_photo = photo[-1]
file_id = largest_photo.get("file_id")
if file_id:
images.append(file_id)
images.append(f"tg://file_id/{file_id}")
document = msg.get("document")
if document:
file_id = document.get("file_id")
mime_type = document.get("mime_type", "")
if file_id and mime_type.startswith("image/"):
images.append(file_id)
images.append(f"tg://file_id/{file_id}")
return images if images else None