refactor(feishu): 将send_text改为send_markdown的别名
所有消息现在都通过markdown卡片发送,简化了消息发送逻辑
This commit is contained in:
parent
f4249e5b0d
commit
3ca492634d
@ -57,55 +57,8 @@ def _split_message(text: str) -> list[str]:
|
|||||||
|
|
||||||
|
|
||||||
async def send_text(receive_id: str, receive_id_type: str, text: str) -> None:
|
async def send_text(receive_id: str, receive_id_type: str, text: str) -> None:
|
||||||
"""
|
"""Alias for send_markdown. All messages are sent as markdown cards."""
|
||||||
Send a plain-text message to a Feishu chat or user.
|
await send_markdown(receive_id, receive_id_type, text)
|
||||||
Automatically splits long messages into multiple parts with [1/N] headers.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
receive_id: chat_id or open_id depending on receive_id_type.
|
|
||||||
receive_id_type: "chat_id" | "open_id" | "user_id" | "union_id".
|
|
||||||
text: message content.
|
|
||||||
"""
|
|
||||||
parts = _split_message(text)
|
|
||||||
loop = asyncio.get_running_loop()
|
|
||||||
|
|
||||||
for i, part in enumerate(parts):
|
|
||||||
logger.debug(
|
|
||||||
"[feishu] send_text to=%s type=%s part=%d/%d len=%d",
|
|
||||||
receive_id, receive_id_type, i + 1, len(parts), len(part),
|
|
||||||
)
|
|
||||||
content = json.dumps({"text": part}, ensure_ascii=False)
|
|
||||||
|
|
||||||
request = (
|
|
||||||
CreateMessageRequest.builder()
|
|
||||||
.receive_id_type(receive_id_type)
|
|
||||||
.request_body(
|
|
||||||
CreateMessageRequestBody.builder()
|
|
||||||
.receive_id(receive_id)
|
|
||||||
.msg_type("text")
|
|
||||||
.content(content)
|
|
||||||
.build()
|
|
||||||
)
|
|
||||||
.build()
|
|
||||||
)
|
|
||||||
|
|
||||||
response = await loop.run_in_executor(
|
|
||||||
None,
|
|
||||||
lambda: _client.im.v1.message.create(request),
|
|
||||||
)
|
|
||||||
|
|
||||||
if not response.success():
|
|
||||||
logger.error(
|
|
||||||
"Feishu send_text failed: code=%s msg=%s",
|
|
||||||
response.code,
|
|
||||||
response.msg,
|
|
||||||
)
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
logger.debug("Sent message part %d/%d to %s (%s)", i + 1, len(parts), receive_id, receive_id_type)
|
|
||||||
|
|
||||||
if len(parts) > 1 and i < len(parts) - 1:
|
|
||||||
await asyncio.sleep(0.3)
|
|
||||||
|
|
||||||
|
|
||||||
async def send_markdown(receive_id: str, receive_id_type: str, content: str) -> None:
|
async def send_markdown(receive_id: str, receive_id_type: str, content: str) -> None:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user