mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-15 23:10:59 +00:00
fix: image couldn't be sent in lark (#1348)
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import lark_oapi
|
import lark_oapi
|
||||||
|
from lark_oapi.api.im.v1 import CreateImageRequest, CreateImageRequestBody, CreateImageResponse
|
||||||
|
import traceback
|
||||||
import typing
|
import typing
|
||||||
import asyncio
|
import asyncio
|
||||||
import traceback
|
import traceback
|
||||||
@@ -60,12 +61,22 @@ class LarkMessageConverter(adapter.MessageConverter):
|
|||||||
message_chain: platform_message.MessageChain, api_client: lark_oapi.Client
|
message_chain: platform_message.MessageChain, api_client: lark_oapi.Client
|
||||||
) -> typing.Tuple[list]:
|
) -> typing.Tuple[list]:
|
||||||
message_elements = []
|
message_elements = []
|
||||||
|
|
||||||
pending_paragraph = []
|
pending_paragraph = []
|
||||||
|
|
||||||
for msg in message_chain:
|
for msg in message_chain:
|
||||||
if isinstance(msg, platform_message.Plain):
|
if isinstance(msg, platform_message.Plain):
|
||||||
pending_paragraph.append({"tag": "md", "text": msg.text})
|
# Ensure text is valid UTF-8
|
||||||
|
try:
|
||||||
|
text = msg.text.encode('utf-8').decode('utf-8')
|
||||||
|
pending_paragraph.append({"tag": "md", "text": text})
|
||||||
|
except UnicodeError:
|
||||||
|
# If text is not valid UTF-8, try to decode with other encodings
|
||||||
|
try:
|
||||||
|
text = msg.text.encode('latin1').decode('utf-8')
|
||||||
|
pending_paragraph.append({"tag": "md", "text": text})
|
||||||
|
except UnicodeError:
|
||||||
|
# If still fails, replace invalid characters
|
||||||
|
text = msg.text.encode('utf-8', errors='replace').decode('utf-8')
|
||||||
|
pending_paragraph.append({"tag": "md", "text": text})
|
||||||
elif isinstance(msg, platform_message.At):
|
elif isinstance(msg, platform_message.At):
|
||||||
pending_paragraph.append(
|
pending_paragraph.append(
|
||||||
{"tag": "at", "user_id": msg.target, "style": []}
|
{"tag": "at", "user_id": msg.target, "style": []}
|
||||||
@@ -73,51 +84,84 @@ class LarkMessageConverter(adapter.MessageConverter):
|
|||||||
elif isinstance(msg, platform_message.AtAll):
|
elif isinstance(msg, platform_message.AtAll):
|
||||||
pending_paragraph.append({"tag": "at", "user_id": "all", "style": []})
|
pending_paragraph.append({"tag": "at", "user_id": "all", "style": []})
|
||||||
elif isinstance(msg, platform_message.Image):
|
elif isinstance(msg, platform_message.Image):
|
||||||
|
|
||||||
image_bytes = None
|
image_bytes = None
|
||||||
|
|
||||||
if msg.base64:
|
if msg.base64:
|
||||||
image_bytes = base64.b64decode(msg.base64)
|
try:
|
||||||
|
# Remove data URL prefix if present
|
||||||
|
if msg.base64.startswith('data:'):
|
||||||
|
msg.base64 = msg.base64.split(',', 1)[1]
|
||||||
|
image_bytes = base64.b64decode(msg.base64)
|
||||||
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
|
continue
|
||||||
elif msg.url:
|
elif msg.url:
|
||||||
async with aiohttp.ClientSession() as session:
|
try:
|
||||||
async with session.get(msg.url) as response:
|
async with aiohttp.ClientSession() as session:
|
||||||
image_bytes = await response.read()
|
async with session.get(msg.url) as response:
|
||||||
|
if response.status == 200:
|
||||||
|
image_bytes = await response.read()
|
||||||
|
else:
|
||||||
|
traceback.print_exc()
|
||||||
|
continue
|
||||||
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
|
continue
|
||||||
elif msg.path:
|
elif msg.path:
|
||||||
with open(msg.path, "rb") as f:
|
try:
|
||||||
image_bytes = f.read()
|
with open(msg.path, "rb") as f:
|
||||||
|
image_bytes = f.read()
|
||||||
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
|
continue
|
||||||
|
|
||||||
request: CreateImageRequest = (
|
if image_bytes is None:
|
||||||
CreateImageRequest.builder()
|
continue
|
||||||
.request_body(
|
|
||||||
CreateImageRequestBody.builder()
|
|
||||||
.image_type("message")
|
|
||||||
.image(image_bytes)
|
|
||||||
.build()
|
|
||||||
)
|
|
||||||
.build()
|
|
||||||
)
|
|
||||||
|
|
||||||
response: CreateImageResponse = await api_client.im.v1.image.acreate(
|
try:
|
||||||
request
|
# Create a temporary file to store the image bytes
|
||||||
)
|
import tempfile
|
||||||
|
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
||||||
|
temp_file.write(image_bytes)
|
||||||
|
temp_file.flush()
|
||||||
|
|
||||||
|
# Create image request using the temporary file
|
||||||
|
request = CreateImageRequest.builder()\
|
||||||
|
.request_body(
|
||||||
|
CreateImageRequestBody.builder()
|
||||||
|
.image_type("message")
|
||||||
|
.image(open(temp_file.name, "rb"))
|
||||||
|
.build()
|
||||||
|
)\
|
||||||
|
.build()
|
||||||
|
|
||||||
|
response = await api_client.im.v1.image.acreate(request)
|
||||||
|
|
||||||
|
if not response.success():
|
||||||
|
raise Exception(
|
||||||
|
f"client.im.v1.image.create failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}, resp: \n{json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
image_key = response.data.image_key
|
||||||
|
|
||||||
if not response.success():
|
message_elements.append(pending_paragraph)
|
||||||
raise Exception(
|
message_elements.append(
|
||||||
f"client.im.v1.image.create failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}, resp: \n{json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}"
|
[
|
||||||
)
|
{
|
||||||
|
"tag": "img",
|
||||||
image_key = response.data.image_key
|
"image_key": image_key,
|
||||||
|
}
|
||||||
message_elements.append(pending_paragraph)
|
]
|
||||||
message_elements.append(
|
)
|
||||||
[
|
pending_paragraph = []
|
||||||
{
|
except Exception as e:
|
||||||
"tag": "img",
|
traceback.print_exc()
|
||||||
"image_key": image_key,
|
continue
|
||||||
}
|
finally:
|
||||||
]
|
# Clean up the temporary file
|
||||||
)
|
import os
|
||||||
pending_paragraph = []
|
if 'temp_file' in locals():
|
||||||
|
os.unlink(temp_file.name)
|
||||||
elif isinstance(msg, platform_message.Forward):
|
elif isinstance(msg, platform_message.Forward):
|
||||||
for node in msg.node_list:
|
for node in msg.node_list:
|
||||||
message_elements.extend(await LarkMessageConverter.yiri2target(node.message_chain, api_client))
|
message_elements.extend(await LarkMessageConverter.yiri2target(node.message_chain, api_client))
|
||||||
|
|||||||
Reference in New Issue
Block a user