mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-10-22 00:15:20 +08:00
fix(slack): transfer message attachments through file APIs
This commit is contained in:
@@ -167,8 +167,8 @@ class SlackClient:
|
||||
try:
|
||||
response = await self.client.chat_postMessage(channel=channel_id, text=text)
|
||||
if self.bot_user_id is None and response.get('ok'):
|
||||
self.bot_user_id = response['message']['bot_id']
|
||||
return
|
||||
self.bot_user_id = response.get('message', {}).get('bot_id')
|
||||
return response.data if hasattr(response, 'data') else response
|
||||
except Exception as e:
|
||||
await self.logger.error(f'Error in send_message: {e}')
|
||||
raise e
|
||||
@@ -177,9 +177,9 @@ class SlackClient:
|
||||
try:
|
||||
response = await self.client.chat_postMessage(channel='@' + user_id, text=text)
|
||||
if self.bot_user_id is None and response.get('ok'):
|
||||
self.bot_user_id = response['message']['bot_id']
|
||||
self.bot_user_id = response.get('message', {}).get('bot_id')
|
||||
|
||||
return
|
||||
return response.data if hasattr(response, 'data') else response
|
||||
except Exception as e:
|
||||
await self.logger.error(f'Error in send_message: {traceback.format_exc()}')
|
||||
raise e
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
"""Transfer attachment bytes rather than degrading attachments to text."""
|
||||
|
||||
import asyncio
|
||||
import mimetypes
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse, unquote
|
||||
|
||||
import aiohttp
|
||||
|
||||
from langbot.pkg.utils import httpclient, image
|
||||
from langbot_plugin.api.entities.builtin.platform import message as pm
|
||||
|
||||
MAX_MEDIA_BYTES = 20 * 1024 * 1024
|
||||
MEDIA_TYPES = (pm.Image, pm.File, pm.Voice)
|
||||
|
||||
|
||||
async def download_media(url: str, token: str = '') -> tuple[bytes, str]:
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in {'http', 'https'}:
|
||||
raise ValueError('Slack media requires an HTTP(S) URL')
|
||||
# Never disclose a bot token to arbitrary media hosts or redirects.
|
||||
private = parsed.scheme == 'https' and parsed.hostname == 'files.slack.com'
|
||||
headers = {'Authorization': f'Bearer {token}'} if private and token else {}
|
||||
async with httpclient.get_session().get(
|
||||
url, headers=headers, allow_redirects=not bool(headers), timeout=aiohttp.ClientTimeout(total=30)
|
||||
) as response:
|
||||
if response.status != 200:
|
||||
raise ValueError(f'Slack media download failed (HTTP {response.status})')
|
||||
data = await httpclient.read_limited(response, max_bytes=MAX_MEDIA_BYTES)
|
||||
return data, response.headers.get('Content-Type', 'application/octet-stream').split(';')[0]
|
||||
|
||||
|
||||
async def attachment_bytes(component, token: str = '') -> tuple[bytes, str]:
|
||||
name = getattr(component, 'name', '')
|
||||
mime = ''
|
||||
if component.base64:
|
||||
value = component.base64
|
||||
if value.startswith('data:'):
|
||||
header, value = value.split(',', 1)
|
||||
mime = header[5:].split(';')[0]
|
||||
data = await image.decode_base64_limited(value, max_bytes=MAX_MEDIA_BYTES)
|
||||
elif component.path:
|
||||
path = Path(component.path)
|
||||
|
||||
def read():
|
||||
with path.open('rb') as stream:
|
||||
return stream.read(MAX_MEDIA_BYTES + 1)
|
||||
|
||||
data = await asyncio.to_thread(read)
|
||||
name = name or path.name
|
||||
elif component.url:
|
||||
data, mime = await download_media(component.url, token)
|
||||
name = name or Path(unquote(urlparse(component.url).path)).name
|
||||
else:
|
||||
raise ValueError('Slack attachment has no base64, path or URL')
|
||||
if not data or len(data) > MAX_MEDIA_BYTES:
|
||||
raise ValueError('Slack attachment is empty or exceeds the 20 MiB limit')
|
||||
if not name:
|
||||
if isinstance(component, pm.Image) and not mime:
|
||||
mime = await asyncio.to_thread(_image_mime, data)
|
||||
name = 'attachment' + (mimetypes.guess_extension(mime) or '.bin')
|
||||
return data, name.replace('\\', '/').rsplit('/', 1)[-1]
|
||||
|
||||
|
||||
def _image_mime(data: bytes) -> str:
|
||||
import io
|
||||
from PIL import Image
|
||||
|
||||
with Image.open(io.BytesIO(data)) as picture:
|
||||
return Image.MIME.get(picture.format, 'application/octet-stream')
|
||||
|
||||
|
||||
async def send_chain(
|
||||
bot, target_type: str, target_id: str, chain: pm.MessageChain, thread_ts: str | None = None
|
||||
) -> dict:
|
||||
from langbot.pkg.platform.adapters.slack.message_converter import SlackMessageConverter
|
||||
|
||||
if target_type not in {'person', 'channel'}:
|
||||
raise ValueError(f'Unsupported Slack target type: {target_type}')
|
||||
responses = []
|
||||
pending = []
|
||||
channel = None
|
||||
|
||||
async def flush():
|
||||
if not pending:
|
||||
return
|
||||
text = await SlackMessageConverter.yiri2target(pm.MessageChain(list(pending)))
|
||||
pending.clear()
|
||||
if text:
|
||||
if thread_ts:
|
||||
response = await bot.client.chat_postMessage(channel=target_id, text=text, thread_ts=thread_ts)
|
||||
responses.append(response.data if hasattr(response, 'data') else response)
|
||||
else:
|
||||
method = bot.send_message_to_one if target_type == 'person' else bot.send_message_to_channel
|
||||
responses.append(await method(text, target_id))
|
||||
|
||||
for component in chain:
|
||||
if isinstance(component, MEDIA_TYPES):
|
||||
data, filename = await attachment_bytes(component, bot.bot_token)
|
||||
await flush()
|
||||
if channel is None:
|
||||
if target_type == 'person' and not target_id.startswith('D'):
|
||||
opened = await bot.client.conversations_open(users=[target_id])
|
||||
channel = opened['channel']['id']
|
||||
else:
|
||||
channel = target_id
|
||||
response = await bot.client.files_upload_v2(
|
||||
channel=channel,
|
||||
file=data,
|
||||
filename=filename,
|
||||
title=filename,
|
||||
**({'thread_ts': thread_ts} if thread_ts else {}),
|
||||
)
|
||||
if not response.get('ok'):
|
||||
raise RuntimeError(f'Slack file upload failed: {response.get("error", "unknown_error")}')
|
||||
responses.append(response.data if hasattr(response, 'data') else response)
|
||||
elif isinstance(component, pm.Forward):
|
||||
await flush()
|
||||
for node in component.node_list:
|
||||
if node.message_chain:
|
||||
responses.append(await send_chain(bot, target_type, target_id, node.message_chain, thread_ts))
|
||||
else:
|
||||
pending.append(component)
|
||||
await flush()
|
||||
return {'target_type': target_type, 'target_id': target_id, 'responses': responses}
|
||||
@@ -12,6 +12,8 @@ class SlackEvent(dict):
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
if self.get('event', {}).get('text') is not None:
|
||||
return self['event']['text']
|
||||
if self.get('event', {}).get('channel_type') == 'im':
|
||||
blocks = self.get('event', {}).get('blocks', [])
|
||||
if not blocks:
|
||||
|
||||
@@ -8,6 +8,7 @@ import pydantic
|
||||
|
||||
from langbot.libs.slack_api.api import SlackClient
|
||||
from langbot.libs.slack_api.slackevent import SlackEvent
|
||||
from langbot.libs.slack_api.media import send_chain
|
||||
from langbot.pkg.platform.adapters.slack.api_impl import SlackAPIMixin
|
||||
from langbot.pkg.platform.adapters.slack.errors import NotSupportedError
|
||||
from langbot.pkg.platform.adapters.slack.event_converter import SlackEventConverter
|
||||
@@ -96,8 +97,7 @@ class SlackAdapter(SlackAPIMixin, abstract_platform_adapter.AbstractPlatformAdap
|
||||
target_id: str,
|
||||
message: platform_message.MessageChain,
|
||||
) -> platform_events.MessageResult:
|
||||
content = await SlackMessageConverter.yiri2target(message)
|
||||
raw = await self._send_text(str(target_type), str(target_id), content)
|
||||
raw = await send_chain(self.bot, self._normalize_target_type(str(target_type)), str(target_id), message)
|
||||
return platform_events.MessageResult(raw=raw)
|
||||
|
||||
async def reply_message(
|
||||
@@ -111,7 +111,7 @@ class SlackAdapter(SlackAPIMixin, abstract_platform_adapter.AbstractPlatformAdap
|
||||
raise ValueError('Slack reply_message requires a SlackEvent source object')
|
||||
target_type = 'channel' if source.type == 'channel' else 'person'
|
||||
target_id = source.channel_id if source.type == 'channel' else source.user_id
|
||||
raw = await self._send_text(target_type, target_id, await SlackMessageConverter.yiri2target(message))
|
||||
raw = await send_chain(self.bot, target_type, target_id, message)
|
||||
return platform_events.MessageResult(message_id=source.message_id, raw=raw)
|
||||
|
||||
async def call_platform_api(self, action: str, params: dict = {}) -> dict:
|
||||
|
||||
@@ -4,6 +4,7 @@ import datetime
|
||||
|
||||
from langbot.libs.slack_api.slackevent import SlackEvent
|
||||
from langbot.pkg.utils import image
|
||||
from langbot.libs.slack_api.media import download_media
|
||||
import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter
|
||||
from langbot_plugin.api.entities.builtin.platform import message as platform_message
|
||||
|
||||
@@ -55,15 +56,24 @@ class SlackMessageConverter(abstract_platform_adapter.AbstractMessageConverter):
|
||||
if event.type == 'channel':
|
||||
components.append(platform_message.At(target='SlackBot'))
|
||||
|
||||
if event.pic_url:
|
||||
try:
|
||||
for file in event.get('event', {}).get('files', []):
|
||||
url = file.get('url_private_download') or file.get('url_private')
|
||||
if not url:
|
||||
raise ValueError('Slack attachment is missing its download URL')
|
||||
data, mime = await download_media(url, bot_token)
|
||||
mime = file.get('mimetype') or mime
|
||||
encoded = await image.encode_base64(data)
|
||||
if mime.startswith('image/') and mime != 'image/svg+xml':
|
||||
components.append(platform_message.Image(base64=f'data:{mime};base64,{encoded}'))
|
||||
else:
|
||||
components.append(
|
||||
platform_message.Image(
|
||||
url=event.pic_url, base64=await image.get_slack_image_to_base64(event.pic_url, bot_token)
|
||||
platform_message.File(
|
||||
id=file.get('id', ''),
|
||||
name=file.get('name') or file.get('title') or 'attachment',
|
||||
size=len(data),
|
||||
base64=encoded,
|
||||
)
|
||||
)
|
||||
except Exception:
|
||||
components.append(platform_message.Image(url=event.pic_url))
|
||||
|
||||
if event.text:
|
||||
components.append(platform_message.Plain(text=event.text))
|
||||
|
||||
@@ -6,6 +6,8 @@ import traceback
|
||||
import datetime
|
||||
|
||||
from langbot.libs.slack_api.api import SlackClient
|
||||
from langbot.libs.slack_api.media import send_chain
|
||||
from langbot.pkg.platform.adapters.slack.message_converter import SlackMessageConverter as OmniMessageConverter
|
||||
import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter
|
||||
from langbot.libs.slack_api.slackevent import SlackEvent
|
||||
import langbot_plugin.api.entities.builtin.platform.events as platform_events
|
||||
@@ -61,12 +63,9 @@ class SlackEventConverter(abstract_platform_adapter.AbstractEventConverter):
|
||||
|
||||
@staticmethod
|
||||
async def target2yiri(event: SlackEvent, bot: SlackClient):
|
||||
yiri_chain = await SlackMessageConverter.target2yiri(
|
||||
message=event.text, message_id=event.message_id, pic_url=event.pic_url, bot=bot
|
||||
)
|
||||
yiri_chain = await OmniMessageConverter.target2yiri(event, bot.bot_token)
|
||||
|
||||
if event.type == 'channel':
|
||||
yiri_chain.insert(0, platform_message.At(target='SlackBot'))
|
||||
|
||||
sender = platform_entities.GroupMember(
|
||||
id=event.user_id,
|
||||
@@ -127,27 +126,13 @@ class SlackAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
||||
):
|
||||
slack_event = await SlackEventConverter.yiri2target(message_source)
|
||||
|
||||
content_list = await SlackMessageConverter.yiri2target(message)
|
||||
|
||||
for content in content_list:
|
||||
# Both text and image (URL) are sent as text messages
|
||||
# Slack will auto-unfurl image URLs
|
||||
message_content = content['content']
|
||||
if slack_event.type == 'channel':
|
||||
await self.bot.send_message_to_channel(message_content, slack_event.channel_id)
|
||||
if slack_event.type == 'im':
|
||||
await self.bot.send_message_to_one(message_content, slack_event.user_id)
|
||||
target_type = 'channel' if slack_event.type == 'channel' else 'person'
|
||||
target_id = slack_event.channel_id if target_type == 'channel' else slack_event.user_id
|
||||
return await send_chain(self.bot, target_type, target_id, message)
|
||||
|
||||
async def send_message(self, target_type: str, target_id: str, message: platform_message.MessageChain):
|
||||
content_list = await SlackMessageConverter.yiri2target(message)
|
||||
for content in content_list:
|
||||
# Both text and image (URL) are sent as text messages
|
||||
# Slack will auto-unfurl image URLs
|
||||
message_content = content['content']
|
||||
if target_type == 'person':
|
||||
await self.bot.send_message_to_one(message_content, target_id)
|
||||
if target_type == 'group':
|
||||
await self.bot.send_message_to_channel(message_content, target_id)
|
||||
target_type = 'channel' if target_type in {'group', 'channel'} else target_type
|
||||
return await send_chain(self.bot, target_type, target_id, message)
|
||||
|
||||
def register_listener(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user