2026-02-04 14:07:45 +08:00
|
|
|
|
"""Feishu/Lark channel implementation using lark-oapi SDK with WebSocket long connection."""
|
|
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
import json
|
2026-02-19 17:33:08 +00:00
|
|
|
|
import os
|
2026-02-07 09:46:53 +00:00
|
|
|
|
import re
|
2026-02-04 14:07:45 +08:00
|
|
|
|
import threading
|
2026-02-05 06:01:02 +00:00
|
|
|
|
from collections import OrderedDict
|
2026-02-21 06:30:26 +00:00
|
|
|
|
from pathlib import Path
|
2026-02-04 14:07:45 +08:00
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
|
|
|
|
from nanobot.bus.events import OutboundMessage
|
|
|
|
|
|
from nanobot.bus.queue import MessageBus
|
|
|
|
|
|
from nanobot.channels.base import BaseChannel
|
|
|
|
|
|
from nanobot.config.schema import FeishuConfig
|
|
|
|
|
|
|
2026-03-04 19:31:39 +01:00
|
|
|
|
import importlib.util
|
|
|
|
|
|
|
|
|
|
|
|
FEISHU_AVAILABLE = importlib.util.find_spec("lark_oapi") is not None
|
2026-02-05 06:01:02 +00:00
|
|
|
|
|
|
|
|
|
|
# Message type display mapping
|
|
|
|
|
|
MSG_TYPE_MAP = {
|
|
|
|
|
|
"image": "[image]",
|
|
|
|
|
|
"audio": "[audio]",
|
|
|
|
|
|
"file": "[file]",
|
|
|
|
|
|
"sticker": "[sticker]",
|
|
|
|
|
|
}
|
2026-02-04 14:07:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
def _extract_share_card_content(content_json: dict, msg_type: str) -> str:
|
2026-02-21 06:30:26 +00:00
|
|
|
|
"""Extract text representation from share cards and interactive messages."""
|
2026-02-21 14:08:25 +08:00
|
|
|
|
parts = []
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
if msg_type == "share_chat":
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append(f"[shared chat: {content_json.get('chat_id', '')}]")
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif msg_type == "share_user":
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append(f"[shared user: {content_json.get('user_id', '')}]")
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif msg_type == "interactive":
|
|
|
|
|
|
parts.extend(_extract_interactive_content(content_json))
|
|
|
|
|
|
elif msg_type == "share_calendar_event":
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append(f"[shared calendar event: {content_json.get('event_key', '')}]")
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif msg_type == "system":
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append("[system message]")
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif msg_type == "merge_forward":
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append("[merged forward messages]")
|
|
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
return "\n".join(parts) if parts else f"[{msg_type}]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_interactive_content(content: dict) -> list[str]:
|
|
|
|
|
|
"""Recursively extract text and links from interactive card content."""
|
|
|
|
|
|
parts = []
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
if isinstance(content, str):
|
|
|
|
|
|
try:
|
|
|
|
|
|
content = json.loads(content)
|
|
|
|
|
|
except (json.JSONDecodeError, TypeError):
|
|
|
|
|
|
return [content] if content.strip() else []
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
if not isinstance(content, dict):
|
|
|
|
|
|
return parts
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
if "title" in content:
|
|
|
|
|
|
title = content["title"]
|
|
|
|
|
|
if isinstance(title, dict):
|
|
|
|
|
|
title_content = title.get("content", "") or title.get("text", "")
|
|
|
|
|
|
if title_content:
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append(f"title: {title_content}")
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif isinstance(title, str):
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append(f"title: {title}")
|
|
|
|
|
|
|
2026-02-28 15:10:35 +08:00
|
|
|
|
for elements in content.get("elements", []) if isinstance(content.get("elements"), list) else []:
|
|
|
|
|
|
for element in elements:
|
|
|
|
|
|
parts.extend(_extract_element_content(element))
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
card = content.get("card", {})
|
|
|
|
|
|
if card:
|
|
|
|
|
|
parts.extend(_extract_interactive_content(card))
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
header = content.get("header", {})
|
|
|
|
|
|
if header:
|
|
|
|
|
|
header_title = header.get("title", {})
|
|
|
|
|
|
if isinstance(header_title, dict):
|
|
|
|
|
|
header_text = header_title.get("content", "") or header_title.get("text", "")
|
|
|
|
|
|
if header_text:
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append(f"title: {header_text}")
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
return parts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_element_content(element: dict) -> list[str]:
|
|
|
|
|
|
"""Extract content from a single card element."""
|
|
|
|
|
|
parts = []
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
if not isinstance(element, dict):
|
|
|
|
|
|
return parts
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
tag = element.get("tag", "")
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-21 06:30:26 +00:00
|
|
|
|
if tag in ("markdown", "lark_md"):
|
2026-02-21 14:08:25 +08:00
|
|
|
|
content = element.get("content", "")
|
|
|
|
|
|
if content:
|
|
|
|
|
|
parts.append(content)
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif tag == "div":
|
|
|
|
|
|
text = element.get("text", {})
|
|
|
|
|
|
if isinstance(text, dict):
|
|
|
|
|
|
text_content = text.get("content", "") or text.get("text", "")
|
|
|
|
|
|
if text_content:
|
|
|
|
|
|
parts.append(text_content)
|
|
|
|
|
|
elif isinstance(text, str):
|
|
|
|
|
|
parts.append(text)
|
2026-02-21 06:30:26 +00:00
|
|
|
|
for field in element.get("fields", []):
|
2026-02-21 14:08:25 +08:00
|
|
|
|
if isinstance(field, dict):
|
|
|
|
|
|
field_text = field.get("text", {})
|
|
|
|
|
|
if isinstance(field_text, dict):
|
2026-02-21 06:30:26 +00:00
|
|
|
|
c = field_text.get("content", "")
|
|
|
|
|
|
if c:
|
|
|
|
|
|
parts.append(c)
|
|
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif tag == "a":
|
|
|
|
|
|
href = element.get("href", "")
|
|
|
|
|
|
text = element.get("text", "")
|
|
|
|
|
|
if href:
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append(f"link: {href}")
|
2026-02-21 14:08:25 +08:00
|
|
|
|
if text:
|
|
|
|
|
|
parts.append(text)
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif tag == "button":
|
|
|
|
|
|
text = element.get("text", {})
|
|
|
|
|
|
if isinstance(text, dict):
|
2026-02-21 06:30:26 +00:00
|
|
|
|
c = text.get("content", "")
|
|
|
|
|
|
if c:
|
|
|
|
|
|
parts.append(c)
|
2026-02-21 14:08:25 +08:00
|
|
|
|
url = element.get("url", "") or element.get("multi_url", {}).get("url", "")
|
|
|
|
|
|
if url:
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append(f"link: {url}")
|
|
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif tag == "img":
|
|
|
|
|
|
alt = element.get("alt", {})
|
2026-02-21 06:30:26 +00:00
|
|
|
|
parts.append(alt.get("content", "[image]") if isinstance(alt, dict) else "[image]")
|
|
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif tag == "note":
|
2026-02-21 06:30:26 +00:00
|
|
|
|
for ne in element.get("elements", []):
|
2026-02-21 14:08:25 +08:00
|
|
|
|
parts.extend(_extract_element_content(ne))
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif tag == "column_set":
|
2026-02-21 06:30:26 +00:00
|
|
|
|
for col in element.get("columns", []):
|
|
|
|
|
|
for ce in col.get("elements", []):
|
2026-02-21 14:08:25 +08:00
|
|
|
|
parts.extend(_extract_element_content(ce))
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif tag == "plain_text":
|
|
|
|
|
|
content = element.get("content", "")
|
|
|
|
|
|
if content:
|
|
|
|
|
|
parts.append(content)
|
2026-02-21 06:30:26 +00:00
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
for ne in element.get("elements", []):
|
2026-02-21 14:08:25 +08:00
|
|
|
|
parts.extend(_extract_element_content(ne))
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
return parts
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-02-24 13:42:07 +08:00
|
|
|
|
def _extract_post_content(content_json: dict) -> tuple[str, list[str]]:
|
2026-03-01 06:36:29 +00:00
|
|
|
|
"""Extract text and image keys from Feishu post (rich text) message.
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-03-01 06:36:29 +00:00
|
|
|
|
Handles three payload shapes:
|
|
|
|
|
|
- Direct: {"title": "...", "content": [[...]]}
|
|
|
|
|
|
- Localized: {"zh_cn": {"title": "...", "content": [...]}}
|
|
|
|
|
|
- Wrapped: {"post": {"zh_cn": {"title": "...", "content": [...]}}}
|
2026-02-14 14:37:23 +08:00
|
|
|
|
"""
|
2026-03-01 06:36:29 +00:00
|
|
|
|
|
|
|
|
|
|
def _parse_block(block: dict) -> tuple[str | None, list[str]]:
|
|
|
|
|
|
if not isinstance(block, dict) or not isinstance(block.get("content"), list):
|
2026-02-24 13:42:07 +08:00
|
|
|
|
return None, []
|
2026-03-01 06:36:29 +00:00
|
|
|
|
texts, images = [], []
|
|
|
|
|
|
if title := block.get("title"):
|
|
|
|
|
|
texts.append(title)
|
|
|
|
|
|
for row in block["content"]:
|
|
|
|
|
|
if not isinstance(row, list):
|
2026-02-14 12:14:31 +08:00
|
|
|
|
continue
|
2026-03-01 06:36:29 +00:00
|
|
|
|
for el in row:
|
|
|
|
|
|
if not isinstance(el, dict):
|
|
|
|
|
|
continue
|
|
|
|
|
|
tag = el.get("tag")
|
|
|
|
|
|
if tag in ("text", "a"):
|
|
|
|
|
|
texts.append(el.get("text", ""))
|
|
|
|
|
|
elif tag == "at":
|
|
|
|
|
|
texts.append(f"@{el.get('user_name', 'user')}")
|
|
|
|
|
|
elif tag == "img" and (key := el.get("image_key")):
|
|
|
|
|
|
images.append(key)
|
|
|
|
|
|
return (" ".join(texts).strip() or None), images
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-03-01 06:36:29 +00:00
|
|
|
|
# Unwrap optional {"post": ...} envelope
|
|
|
|
|
|
root = content_json
|
|
|
|
|
|
if isinstance(root, dict) and isinstance(root.get("post"), dict):
|
|
|
|
|
|
root = root["post"]
|
|
|
|
|
|
if not isinstance(root, dict):
|
|
|
|
|
|
return "", []
|
2026-03-01 02:17:10 +08:00
|
|
|
|
|
2026-03-01 06:36:29 +00:00
|
|
|
|
# Direct format
|
|
|
|
|
|
if "content" in root:
|
|
|
|
|
|
text, imgs = _parse_block(root)
|
|
|
|
|
|
if text or imgs:
|
|
|
|
|
|
return text or "", imgs
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-03-01 06:36:29 +00:00
|
|
|
|
# Localized: prefer known locales, then fall back to any dict child
|
|
|
|
|
|
for key in ("zh_cn", "en_us", "ja_jp"):
|
|
|
|
|
|
if key in root:
|
|
|
|
|
|
text, imgs = _parse_block(root[key])
|
|
|
|
|
|
if text or imgs:
|
|
|
|
|
|
return text or "", imgs
|
|
|
|
|
|
for val in root.values():
|
|
|
|
|
|
if isinstance(val, dict):
|
|
|
|
|
|
text, imgs = _parse_block(val)
|
|
|
|
|
|
if text or imgs:
|
|
|
|
|
|
return text or "", imgs
|
2026-03-01 06:30:10 +00:00
|
|
|
|
|
2026-02-24 13:42:07 +08:00
|
|
|
|
return "", []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_post_text(content_json: dict) -> str:
|
|
|
|
|
|
"""Extract plain text from Feishu post (rich text) message content.
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-24 13:42:07 +08:00
|
|
|
|
Legacy wrapper for _extract_post_content, returns only text.
|
|
|
|
|
|
"""
|
|
|
|
|
|
text, _ = _extract_post_content(content_json)
|
|
|
|
|
|
return text
|
2026-02-14 12:14:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
class FeishuChannel(BaseChannel):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Feishu/Lark channel using WebSocket long connection.
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
Uses WebSocket to receive events - no public IP or webhook required.
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
Requires:
|
|
|
|
|
|
- App ID and App Secret from Feishu Open Platform
|
|
|
|
|
|
- Bot capability enabled
|
|
|
|
|
|
- Event subscription enabled (im.message.receive_v1)
|
|
|
|
|
|
"""
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
name = "feishu"
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
def __init__(self, config: FeishuConfig, bus: MessageBus):
|
|
|
|
|
|
super().__init__(config, bus)
|
|
|
|
|
|
self.config: FeishuConfig = config
|
|
|
|
|
|
self._client: Any = None
|
|
|
|
|
|
self._ws_client: Any = None
|
|
|
|
|
|
self._ws_thread: threading.Thread | None = None
|
2026-02-05 06:01:02 +00:00
|
|
|
|
self._processed_message_ids: OrderedDict[str, None] = OrderedDict() # Ordered dedup cache
|
2026-02-04 14:07:45 +08:00
|
|
|
|
self._loop: asyncio.AbstractEventLoop | None = None
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
async def start(self) -> None:
|
|
|
|
|
|
"""Start the Feishu bot with WebSocket long connection."""
|
|
|
|
|
|
if not FEISHU_AVAILABLE:
|
|
|
|
|
|
logger.error("Feishu SDK not installed. Run: pip install lark-oapi")
|
|
|
|
|
|
return
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
if not self.config.app_id or not self.config.app_secret:
|
|
|
|
|
|
logger.error("Feishu app_id and app_secret not configured")
|
|
|
|
|
|
return
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-03-04 19:31:39 +01:00
|
|
|
|
import lark_oapi as lark
|
2026-02-04 14:07:45 +08:00
|
|
|
|
self._running = True
|
2026-02-05 06:01:02 +00:00
|
|
|
|
self._loop = asyncio.get_running_loop()
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
# Create Lark client for sending messages
|
|
|
|
|
|
self._client = lark.Client.builder() \
|
|
|
|
|
|
.app_id(self.config.app_id) \
|
|
|
|
|
|
.app_secret(self.config.app_secret) \
|
|
|
|
|
|
.log_level(lark.LogLevel.INFO) \
|
|
|
|
|
|
.build()
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
# Create event handler (only register message receive, ignore other events)
|
|
|
|
|
|
event_handler = lark.EventDispatcherHandler.builder(
|
|
|
|
|
|
self.config.encrypt_key or "",
|
|
|
|
|
|
self.config.verification_token or "",
|
|
|
|
|
|
).register_p2_im_message_receive_v1(
|
|
|
|
|
|
self._on_message_sync
|
|
|
|
|
|
).build()
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
# Create WebSocket client for long connection
|
|
|
|
|
|
self._ws_client = lark.ws.Client(
|
|
|
|
|
|
self.config.app_id,
|
|
|
|
|
|
self.config.app_secret,
|
|
|
|
|
|
event_handler=event_handler,
|
|
|
|
|
|
log_level=lark.LogLevel.INFO
|
|
|
|
|
|
)
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-03-05 17:27:17 +08:00
|
|
|
|
# Start WebSocket client in a separate thread with reconnect loop.
|
|
|
|
|
|
# A dedicated event loop is created for this thread so that lark_oapi's
|
|
|
|
|
|
# module-level `loop = asyncio.get_event_loop()` picks up an idle loop
|
|
|
|
|
|
# instead of the already-running main asyncio loop, which would cause
|
|
|
|
|
|
# "This event loop is already running" errors.
|
2026-02-04 14:07:45 +08:00
|
|
|
|
def run_ws():
|
2026-03-05 17:27:17 +08:00
|
|
|
|
import time
|
|
|
|
|
|
import lark_oapi.ws.client as _lark_ws_client
|
|
|
|
|
|
ws_loop = asyncio.new_event_loop()
|
|
|
|
|
|
asyncio.set_event_loop(ws_loop)
|
|
|
|
|
|
# Patch the module-level loop used by lark's ws Client.start()
|
|
|
|
|
|
_lark_ws_client.loop = ws_loop
|
|
|
|
|
|
try:
|
|
|
|
|
|
while self._running:
|
|
|
|
|
|
try:
|
|
|
|
|
|
self._ws_client.start()
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.warning("Feishu WebSocket error: {}", e)
|
|
|
|
|
|
if self._running:
|
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
finally:
|
|
|
|
|
|
ws_loop.close()
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
self._ws_thread = threading.Thread(target=run_ws, daemon=True)
|
|
|
|
|
|
self._ws_thread.start()
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
logger.info("Feishu bot started with WebSocket long connection")
|
|
|
|
|
|
logger.info("No public IP required - using WebSocket to receive events")
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
# Keep running until stopped
|
|
|
|
|
|
while self._running:
|
|
|
|
|
|
await asyncio.sleep(1)
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
async def stop(self) -> None:
|
2026-03-01 00:30:03 +08:00
|
|
|
|
"""
|
|
|
|
|
|
Stop the Feishu bot.
|
|
|
|
|
|
|
|
|
|
|
|
Notice: lark.ws.Client does not expose stop method, simply exiting the program will close the client.
|
|
|
|
|
|
|
|
|
|
|
|
Reference: https://github.com/larksuite/oapi-sdk-python/blob/v2_main/lark_oapi/ws/client.py#L86
|
|
|
|
|
|
"""
|
2026-02-04 14:07:45 +08:00
|
|
|
|
self._running = False
|
|
|
|
|
|
logger.info("Feishu bot stopped")
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-05 06:01:02 +00:00
|
|
|
|
def _add_reaction_sync(self, message_id: str, emoji_type: str) -> None:
|
|
|
|
|
|
"""Sync helper for adding reaction (runs in thread pool)."""
|
2026-03-04 19:31:39 +01:00
|
|
|
|
from lark_oapi.api.im.v1 import CreateMessageReactionRequest, CreateMessageReactionRequestBody, Emoji
|
2026-02-04 14:07:45 +08:00
|
|
|
|
try:
|
|
|
|
|
|
request = CreateMessageReactionRequest.builder() \
|
|
|
|
|
|
.message_id(message_id) \
|
|
|
|
|
|
.request_body(
|
|
|
|
|
|
CreateMessageReactionRequestBody.builder()
|
|
|
|
|
|
.reaction_type(Emoji.builder().emoji_type(emoji_type).build())
|
|
|
|
|
|
.build()
|
|
|
|
|
|
).build()
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
response = self._client.im.v1.message_reaction.create(request)
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
if not response.success():
|
2026-02-19 17:19:36 -03:00
|
|
|
|
logger.warning("Failed to add reaction: code={}, msg={}", response.code, response.msg)
|
2026-02-04 14:07:45 +08:00
|
|
|
|
else:
|
2026-02-20 07:55:34 +00:00
|
|
|
|
logger.debug("Added {} reaction to message {}", emoji_type, message_id)
|
2026-02-04 14:07:45 +08:00
|
|
|
|
except Exception as e:
|
2026-02-19 17:19:36 -03:00
|
|
|
|
logger.warning("Error adding reaction: {}", e)
|
2026-02-05 06:01:02 +00:00
|
|
|
|
|
|
|
|
|
|
async def _add_reaction(self, message_id: str, emoji_type: str = "THUMBSUP") -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Add a reaction emoji to a message (non-blocking).
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-05 06:01:02 +00:00
|
|
|
|
Common emoji types: THUMBSUP, OK, EYES, DONE, OnIt, HEART
|
|
|
|
|
|
"""
|
2026-03-04 19:31:39 +01:00
|
|
|
|
if not self._client:
|
2026-02-05 06:01:02 +00:00
|
|
|
|
return
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-05 06:01:02 +00:00
|
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
|
|
await loop.run_in_executor(None, self._add_reaction_sync, message_id, emoji_type)
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-07 09:46:53 +00:00
|
|
|
|
# Regex to match markdown tables (header + separator + data rows)
|
|
|
|
|
|
_TABLE_RE = re.compile(
|
|
|
|
|
|
r"((?:^[ \t]*\|.+\|[ \t]*\n)(?:^[ \t]*\|[-:\s|]+\|[ \t]*\n)(?:^[ \t]*\|.+\|[ \t]*\n?)+)",
|
|
|
|
|
|
re.MULTILINE,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-13 15:31:30 +08:00
|
|
|
|
_HEADING_RE = re.compile(r"^(#{1,6})\s+(.+)$", re.MULTILINE)
|
|
|
|
|
|
|
|
|
|
|
|
_CODE_BLOCK_RE = re.compile(r"(```[\s\S]*?```)", re.MULTILINE)
|
|
|
|
|
|
|
2026-02-07 09:46:53 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _parse_md_table(table_text: str) -> dict | None:
|
|
|
|
|
|
"""Parse a markdown table into a Feishu table element."""
|
2026-02-28 20:55:43 +08:00
|
|
|
|
lines = [_line.strip() for _line in table_text.strip().split("\n") if _line.strip()]
|
2026-02-07 09:46:53 +00:00
|
|
|
|
if len(lines) < 3:
|
|
|
|
|
|
return None
|
2026-02-28 20:55:43 +08:00
|
|
|
|
def split(_line: str) -> list[str]:
|
|
|
|
|
|
return [c.strip() for c in _line.strip("|").split("|")]
|
2026-02-07 09:46:53 +00:00
|
|
|
|
headers = split(lines[0])
|
2026-02-28 20:55:43 +08:00
|
|
|
|
rows = [split(_line) for _line in lines[2:]]
|
2026-02-07 09:46:53 +00:00
|
|
|
|
columns = [{"tag": "column", "name": f"c{i}", "display_name": h, "width": "auto"}
|
|
|
|
|
|
for i, h in enumerate(headers)]
|
|
|
|
|
|
return {
|
|
|
|
|
|
"tag": "table",
|
|
|
|
|
|
"page_size": len(rows) + 1,
|
|
|
|
|
|
"columns": columns,
|
|
|
|
|
|
"rows": [{f"c{i}": r[i] if i < len(r) else "" for i in range(len(headers))} for r in rows],
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _build_card_elements(self, content: str) -> list[dict]:
|
2026-02-13 15:31:30 +08:00
|
|
|
|
"""Split content into div/markdown + table elements for Feishu card."""
|
2026-02-07 09:46:53 +00:00
|
|
|
|
elements, last_end = [], 0
|
|
|
|
|
|
for m in self._TABLE_RE.finditer(content):
|
2026-02-13 15:31:30 +08:00
|
|
|
|
before = content[last_end:m.start()]
|
|
|
|
|
|
if before.strip():
|
|
|
|
|
|
elements.extend(self._split_headings(before))
|
2026-02-07 09:46:53 +00:00
|
|
|
|
elements.append(self._parse_md_table(m.group(1)) or {"tag": "markdown", "content": m.group(1)})
|
|
|
|
|
|
last_end = m.end()
|
2026-02-13 15:31:30 +08:00
|
|
|
|
remaining = content[last_end:]
|
|
|
|
|
|
if remaining.strip():
|
|
|
|
|
|
elements.extend(self._split_headings(remaining))
|
|
|
|
|
|
return elements or [{"tag": "markdown", "content": content}]
|
|
|
|
|
|
|
2026-03-01 15:13:44 +01:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _split_elements_by_table_limit(elements: list[dict], max_tables: int = 1) -> list[list[dict]]:
|
|
|
|
|
|
"""Split card elements into groups with at most *max_tables* table elements each.
|
|
|
|
|
|
|
|
|
|
|
|
Feishu cards have a hard limit of one table per card (API error 11310).
|
|
|
|
|
|
When the rendered content contains multiple markdown tables each table is
|
|
|
|
|
|
placed in a separate card message so every table reaches the user.
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not elements:
|
|
|
|
|
|
return [[]]
|
|
|
|
|
|
groups: list[list[dict]] = []
|
|
|
|
|
|
current: list[dict] = []
|
|
|
|
|
|
table_count = 0
|
|
|
|
|
|
for el in elements:
|
|
|
|
|
|
if el.get("tag") == "table":
|
|
|
|
|
|
if table_count >= max_tables:
|
|
|
|
|
|
if current:
|
|
|
|
|
|
groups.append(current)
|
|
|
|
|
|
current = []
|
|
|
|
|
|
table_count = 0
|
|
|
|
|
|
current.append(el)
|
|
|
|
|
|
table_count += 1
|
|
|
|
|
|
else:
|
|
|
|
|
|
current.append(el)
|
|
|
|
|
|
if current:
|
|
|
|
|
|
groups.append(current)
|
|
|
|
|
|
return groups or [[]]
|
|
|
|
|
|
|
2026-02-13 15:31:30 +08:00
|
|
|
|
def _split_headings(self, content: str) -> list[dict]:
|
|
|
|
|
|
"""Split content by headings, converting headings to div elements."""
|
|
|
|
|
|
protected = content
|
|
|
|
|
|
code_blocks = []
|
|
|
|
|
|
for m in self._CODE_BLOCK_RE.finditer(content):
|
|
|
|
|
|
code_blocks.append(m.group(1))
|
|
|
|
|
|
protected = protected.replace(m.group(1), f"\x00CODE{len(code_blocks)-1}\x00", 1)
|
|
|
|
|
|
|
|
|
|
|
|
elements = []
|
|
|
|
|
|
last_end = 0
|
|
|
|
|
|
for m in self._HEADING_RE.finditer(protected):
|
|
|
|
|
|
before = protected[last_end:m.start()].strip()
|
|
|
|
|
|
if before:
|
|
|
|
|
|
elements.append({"tag": "markdown", "content": before})
|
|
|
|
|
|
text = m.group(2).strip()
|
|
|
|
|
|
elements.append({
|
|
|
|
|
|
"tag": "div",
|
|
|
|
|
|
"text": {
|
|
|
|
|
|
"tag": "lark_md",
|
|
|
|
|
|
"content": f"**{text}**",
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
last_end = m.end()
|
|
|
|
|
|
remaining = protected[last_end:].strip()
|
2026-02-07 09:46:53 +00:00
|
|
|
|
if remaining:
|
|
|
|
|
|
elements.append({"tag": "markdown", "content": remaining})
|
2026-02-13 15:31:30 +08:00
|
|
|
|
|
|
|
|
|
|
for i, cb in enumerate(code_blocks):
|
|
|
|
|
|
for el in elements:
|
|
|
|
|
|
if el.get("tag") == "markdown":
|
|
|
|
|
|
el["content"] = el["content"].replace(f"\x00CODE{i}\x00", cb)
|
|
|
|
|
|
|
2026-02-07 09:46:53 +00:00
|
|
|
|
return elements or [{"tag": "markdown", "content": content}]
|
|
|
|
|
|
|
2026-03-06 10:11:53 +08:00
|
|
|
|
# ── Smart format detection ──────────────────────────────────────────
|
|
|
|
|
|
# Patterns that indicate "complex" markdown needing card rendering
|
|
|
|
|
|
_COMPLEX_MD_RE = re.compile(
|
|
|
|
|
|
r"```" # fenced code block
|
|
|
|
|
|
r"|^\|.+\|.*\n\s*\|[-:\s|]+\|" # markdown table (header + separator)
|
|
|
|
|
|
r"|^#{1,6}\s+" # headings
|
|
|
|
|
|
, re.MULTILINE,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Simple markdown patterns (bold, italic, strikethrough)
|
|
|
|
|
|
_SIMPLE_MD_RE = re.compile(
|
|
|
|
|
|
r"\*\*.+?\*\*" # **bold**
|
|
|
|
|
|
r"|__.+?__" # __bold__
|
|
|
|
|
|
r"|(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)" # *italic* (single *)
|
|
|
|
|
|
r"|~~.+?~~" # ~~strikethrough~~
|
|
|
|
|
|
, re.DOTALL,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Markdown link: [text](url)
|
|
|
|
|
|
_MD_LINK_RE = re.compile(r"\[([^\]]+)\]\((https?://[^\)]+)\)")
|
|
|
|
|
|
|
|
|
|
|
|
# Unordered list items
|
|
|
|
|
|
_LIST_RE = re.compile(r"^[\s]*[-*+]\s+", re.MULTILINE)
|
|
|
|
|
|
|
|
|
|
|
|
# Ordered list items
|
|
|
|
|
|
_OLIST_RE = re.compile(r"^[\s]*\d+\.\s+", re.MULTILINE)
|
|
|
|
|
|
|
|
|
|
|
|
# Max length for plain text format
|
|
|
|
|
|
_TEXT_MAX_LEN = 200
|
|
|
|
|
|
|
|
|
|
|
|
# Max length for post (rich text) format; beyond this, use card
|
|
|
|
|
|
_POST_MAX_LEN = 2000
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def _detect_msg_format(cls, content: str) -> str:
|
|
|
|
|
|
"""Determine the optimal Feishu message format for *content*.
|
|
|
|
|
|
|
|
|
|
|
|
Returns one of:
|
|
|
|
|
|
- ``"text"`` – plain text, short and no markdown
|
|
|
|
|
|
- ``"post"`` – rich text (links only, moderate length)
|
|
|
|
|
|
- ``"interactive"`` – card with full markdown rendering
|
|
|
|
|
|
"""
|
|
|
|
|
|
stripped = content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
# Complex markdown (code blocks, tables, headings) → always card
|
|
|
|
|
|
if cls._COMPLEX_MD_RE.search(stripped):
|
|
|
|
|
|
return "interactive"
|
|
|
|
|
|
|
|
|
|
|
|
# Long content → card (better readability with card layout)
|
|
|
|
|
|
if len(stripped) > cls._POST_MAX_LEN:
|
|
|
|
|
|
return "interactive"
|
|
|
|
|
|
|
|
|
|
|
|
# Has bold/italic/strikethrough → card (post format can't render these)
|
|
|
|
|
|
if cls._SIMPLE_MD_RE.search(stripped):
|
|
|
|
|
|
return "interactive"
|
|
|
|
|
|
|
|
|
|
|
|
# Has list items → card (post format can't render list bullets well)
|
|
|
|
|
|
if cls._LIST_RE.search(stripped) or cls._OLIST_RE.search(stripped):
|
|
|
|
|
|
return "interactive"
|
|
|
|
|
|
|
|
|
|
|
|
# Has links → post format (supports <a> tags)
|
|
|
|
|
|
if cls._MD_LINK_RE.search(stripped):
|
|
|
|
|
|
return "post"
|
|
|
|
|
|
|
|
|
|
|
|
# Short plain text → text format
|
|
|
|
|
|
if len(stripped) <= cls._TEXT_MAX_LEN:
|
|
|
|
|
|
return "text"
|
|
|
|
|
|
|
|
|
|
|
|
# Medium plain text without any formatting → post format
|
|
|
|
|
|
return "post"
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def _markdown_to_post(cls, content: str) -> str:
|
|
|
|
|
|
"""Convert markdown content to Feishu post message JSON.
|
|
|
|
|
|
|
|
|
|
|
|
Handles links ``[text](url)`` as ``a`` tags; everything else as ``text`` tags.
|
|
|
|
|
|
Each line becomes a paragraph (row) in the post body.
|
|
|
|
|
|
"""
|
|
|
|
|
|
lines = content.strip().split("\n")
|
|
|
|
|
|
paragraphs: list[list[dict]] = []
|
|
|
|
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
|
|
elements: list[dict] = []
|
|
|
|
|
|
last_end = 0
|
|
|
|
|
|
|
|
|
|
|
|
for m in cls._MD_LINK_RE.finditer(line):
|
|
|
|
|
|
# Text before this link
|
|
|
|
|
|
before = line[last_end:m.start()]
|
|
|
|
|
|
if before:
|
|
|
|
|
|
elements.append({"tag": "text", "text": before})
|
|
|
|
|
|
elements.append({
|
|
|
|
|
|
"tag": "a",
|
|
|
|
|
|
"text": m.group(1),
|
|
|
|
|
|
"href": m.group(2),
|
|
|
|
|
|
})
|
|
|
|
|
|
last_end = m.end()
|
|
|
|
|
|
|
|
|
|
|
|
# Remaining text after last link
|
|
|
|
|
|
remaining = line[last_end:]
|
|
|
|
|
|
if remaining:
|
|
|
|
|
|
elements.append({"tag": "text", "text": remaining})
|
|
|
|
|
|
|
|
|
|
|
|
# Empty line → empty paragraph for spacing
|
|
|
|
|
|
if not elements:
|
|
|
|
|
|
elements.append({"tag": "text", "text": ""})
|
|
|
|
|
|
|
|
|
|
|
|
paragraphs.append(elements)
|
|
|
|
|
|
|
|
|
|
|
|
post_body = {
|
|
|
|
|
|
"zh_cn": {
|
|
|
|
|
|
"content": paragraphs,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return json.dumps(post_body, ensure_ascii=False)
|
|
|
|
|
|
|
2026-02-19 16:31:00 +08:00
|
|
|
|
_IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".ico", ".tiff", ".tif"}
|
|
|
|
|
|
_AUDIO_EXTS = {".opus"}
|
2026-03-06 01:54:00 +08:00
|
|
|
|
_VIDEO_EXTS = {".mp4", ".mov", ".avi"}
|
2026-02-19 16:31:00 +08:00
|
|
|
|
_FILE_TYPE_MAP = {
|
|
|
|
|
|
".opus": "opus", ".mp4": "mp4", ".pdf": "pdf", ".doc": "doc", ".docx": "doc",
|
|
|
|
|
|
".xls": "xls", ".xlsx": "xls", ".ppt": "ppt", ".pptx": "ppt",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _upload_image_sync(self, file_path: str) -> str | None:
|
|
|
|
|
|
"""Upload an image to Feishu and return the image_key."""
|
2026-03-04 19:31:39 +01:00
|
|
|
|
from lark_oapi.api.im.v1 import CreateImageRequest, CreateImageRequestBody
|
2026-02-19 16:31:00 +08:00
|
|
|
|
try:
|
|
|
|
|
|
with open(file_path, "rb") as f:
|
|
|
|
|
|
request = CreateImageRequest.builder() \
|
|
|
|
|
|
.request_body(
|
|
|
|
|
|
CreateImageRequestBody.builder()
|
|
|
|
|
|
.image_type("message")
|
|
|
|
|
|
.image(f)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
).build()
|
|
|
|
|
|
response = self._client.im.v1.image.create(request)
|
|
|
|
|
|
if response.success():
|
|
|
|
|
|
image_key = response.data.image_key
|
2026-02-20 07:55:34 +00:00
|
|
|
|
logger.debug("Uploaded image {}: {}", os.path.basename(file_path), image_key)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
return image_key
|
|
|
|
|
|
else:
|
2026-02-19 17:19:36 -03:00
|
|
|
|
logger.error("Failed to upload image: code={}, msg={}", response.code, response.msg)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
return None
|
|
|
|
|
|
except Exception as e:
|
2026-02-19 17:19:36 -03:00
|
|
|
|
logger.error("Error uploading image {}: {}", file_path, e)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _upload_file_sync(self, file_path: str) -> str | None:
|
|
|
|
|
|
"""Upload a file to Feishu and return the file_key."""
|
2026-03-04 19:31:39 +01:00
|
|
|
|
from lark_oapi.api.im.v1 import CreateFileRequest, CreateFileRequestBody
|
2026-02-19 16:31:00 +08:00
|
|
|
|
ext = os.path.splitext(file_path)[1].lower()
|
|
|
|
|
|
file_type = self._FILE_TYPE_MAP.get(ext, "stream")
|
|
|
|
|
|
file_name = os.path.basename(file_path)
|
|
|
|
|
|
try:
|
|
|
|
|
|
with open(file_path, "rb") as f:
|
|
|
|
|
|
request = CreateFileRequest.builder() \
|
|
|
|
|
|
.request_body(
|
|
|
|
|
|
CreateFileRequestBody.builder()
|
|
|
|
|
|
.file_type(file_type)
|
|
|
|
|
|
.file_name(file_name)
|
|
|
|
|
|
.file(f)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
).build()
|
|
|
|
|
|
response = self._client.im.v1.file.create(request)
|
|
|
|
|
|
if response.success():
|
|
|
|
|
|
file_key = response.data.file_key
|
2026-02-20 07:55:34 +00:00
|
|
|
|
logger.debug("Uploaded file {}: {}", file_name, file_key)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
return file_key
|
|
|
|
|
|
else:
|
2026-02-19 17:19:36 -03:00
|
|
|
|
logger.error("Failed to upload file: code={}, msg={}", response.code, response.msg)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
return None
|
|
|
|
|
|
except Exception as e:
|
2026-02-19 17:19:36 -03:00
|
|
|
|
logger.error("Error uploading file {}: {}", file_path, e)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
return None
|
|
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
def _download_image_sync(self, message_id: str, image_key: str) -> tuple[bytes | None, str | None]:
|
|
|
|
|
|
"""Download an image from Feishu message by message_id and image_key."""
|
2026-03-04 19:31:39 +01:00
|
|
|
|
from lark_oapi.api.im.v1 import GetMessageResourceRequest
|
2026-02-21 12:56:57 +08:00
|
|
|
|
try:
|
2026-02-21 14:08:25 +08:00
|
|
|
|
request = GetMessageResourceRequest.builder() \
|
|
|
|
|
|
.message_id(message_id) \
|
|
|
|
|
|
.file_key(image_key) \
|
|
|
|
|
|
.type("image") \
|
|
|
|
|
|
.build()
|
|
|
|
|
|
response = self._client.im.v1.message_resource.get(request)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
if response.success():
|
2026-02-21 14:08:25 +08:00
|
|
|
|
file_data = response.file
|
|
|
|
|
|
# GetMessageResourceRequest returns BytesIO, need to read bytes
|
|
|
|
|
|
if hasattr(file_data, 'read'):
|
|
|
|
|
|
file_data = file_data.read()
|
|
|
|
|
|
return file_data, response.file_name
|
2026-02-21 12:56:57 +08:00
|
|
|
|
else:
|
|
|
|
|
|
logger.error("Failed to download image: code={}, msg={}", response.code, response.msg)
|
|
|
|
|
|
return None, None
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error("Error downloading image {}: {}", image_key, e)
|
|
|
|
|
|
return None, None
|
|
|
|
|
|
|
2026-02-22 18:16:45 +00:00
|
|
|
|
def _download_file_sync(
|
|
|
|
|
|
self, message_id: str, file_key: str, resource_type: str = "file"
|
|
|
|
|
|
) -> tuple[bytes | None, str | None]:
|
|
|
|
|
|
"""Download a file/audio/media from a Feishu message by message_id and file_key."""
|
2026-03-04 19:31:39 +01:00
|
|
|
|
from lark_oapi.api.im.v1 import GetMessageResourceRequest
|
2026-03-04 20:04:00 +01:00
|
|
|
|
|
|
|
|
|
|
# Feishu API only accepts 'image' or 'file' as type parameter
|
|
|
|
|
|
# Convert 'audio' to 'file' for API compatibility
|
|
|
|
|
|
if resource_type == "audio":
|
|
|
|
|
|
resource_type = "file"
|
|
|
|
|
|
|
2026-02-21 12:56:57 +08:00
|
|
|
|
try:
|
2026-02-22 18:16:45 +00:00
|
|
|
|
request = (
|
|
|
|
|
|
GetMessageResourceRequest.builder()
|
|
|
|
|
|
.message_id(message_id)
|
|
|
|
|
|
.file_key(file_key)
|
|
|
|
|
|
.type(resource_type)
|
2026-02-22 17:15:00 +08:00
|
|
|
|
.build()
|
2026-02-22 18:16:45 +00:00
|
|
|
|
)
|
2026-02-22 17:15:00 +08:00
|
|
|
|
response = self._client.im.v1.message_resource.get(request)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
if response.success():
|
2026-02-22 17:15:00 +08:00
|
|
|
|
file_data = response.file
|
2026-02-22 18:16:45 +00:00
|
|
|
|
if hasattr(file_data, "read"):
|
2026-02-22 17:15:00 +08:00
|
|
|
|
file_data = file_data.read()
|
|
|
|
|
|
return file_data, response.file_name
|
2026-02-21 12:56:57 +08:00
|
|
|
|
else:
|
2026-02-22 17:15:00 +08:00
|
|
|
|
logger.error("Failed to download {}: code={}, msg={}", resource_type, response.code, response.msg)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
return None, None
|
2026-02-22 18:16:45 +00:00
|
|
|
|
except Exception:
|
|
|
|
|
|
logger.exception("Error downloading {} {}", resource_type, file_key)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
async def _download_and_save_media(
|
|
|
|
|
|
self,
|
|
|
|
|
|
msg_type: str,
|
2026-02-21 14:08:25 +08:00
|
|
|
|
content_json: dict,
|
|
|
|
|
|
message_id: str | None = None
|
2026-02-21 12:56:57 +08:00
|
|
|
|
) -> tuple[str | None, str]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Download media from Feishu and save to local disk.
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
(file_path, content_text) - file_path is None if download failed
|
|
|
|
|
|
"""
|
|
|
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
|
|
media_dir = Path.home() / ".nanobot" / "media"
|
|
|
|
|
|
media_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
data, filename = None, None
|
|
|
|
|
|
|
|
|
|
|
|
if msg_type == "image":
|
|
|
|
|
|
image_key = content_json.get("image_key")
|
2026-02-21 14:08:25 +08:00
|
|
|
|
if image_key and message_id:
|
2026-02-21 12:56:57 +08:00
|
|
|
|
data, filename = await loop.run_in_executor(
|
2026-02-21 14:08:25 +08:00
|
|
|
|
None, self._download_image_sync, message_id, image_key
|
2026-02-21 12:56:57 +08:00
|
|
|
|
)
|
|
|
|
|
|
if not filename:
|
|
|
|
|
|
filename = f"{image_key[:16]}.jpg"
|
|
|
|
|
|
|
2026-02-22 17:37:33 +08:00
|
|
|
|
elif msg_type in ("audio", "file", "media"):
|
2026-02-21 12:56:57 +08:00
|
|
|
|
file_key = content_json.get("file_key")
|
2026-02-22 17:15:00 +08:00
|
|
|
|
if file_key and message_id:
|
2026-02-21 12:56:57 +08:00
|
|
|
|
data, filename = await loop.run_in_executor(
|
2026-02-22 18:16:45 +00:00
|
|
|
|
None, self._download_file_sync, message_id, file_key, msg_type
|
2026-02-21 12:56:57 +08:00
|
|
|
|
)
|
|
|
|
|
|
if not filename:
|
2026-02-22 18:16:45 +00:00
|
|
|
|
ext = {"audio": ".opus", "media": ".mp4"}.get(msg_type, "")
|
2026-02-21 12:56:57 +08:00
|
|
|
|
filename = f"{file_key[:16]}{ext}"
|
|
|
|
|
|
|
|
|
|
|
|
if data and filename:
|
|
|
|
|
|
file_path = media_dir / filename
|
|
|
|
|
|
file_path.write_bytes(data)
|
|
|
|
|
|
logger.debug("Downloaded {} to {}", msg_type, file_path)
|
|
|
|
|
|
return str(file_path), f"[{msg_type}: {filename}]"
|
|
|
|
|
|
|
|
|
|
|
|
return None, f"[{msg_type}: download failed]"
|
|
|
|
|
|
|
2026-02-19 16:31:00 +08:00
|
|
|
|
def _send_message_sync(self, receive_id_type: str, receive_id: str, msg_type: str, content: str) -> bool:
|
|
|
|
|
|
"""Send a single message (text/image/file/interactive) synchronously."""
|
2026-03-04 19:31:39 +01:00
|
|
|
|
from lark_oapi.api.im.v1 import CreateMessageRequest, CreateMessageRequestBody
|
2026-02-04 14:07:45 +08:00
|
|
|
|
try:
|
|
|
|
|
|
request = CreateMessageRequest.builder() \
|
|
|
|
|
|
.receive_id_type(receive_id_type) \
|
|
|
|
|
|
.request_body(
|
|
|
|
|
|
CreateMessageRequestBody.builder()
|
2026-02-19 16:31:00 +08:00
|
|
|
|
.receive_id(receive_id)
|
|
|
|
|
|
.msg_type(msg_type)
|
2026-02-04 14:07:45 +08:00
|
|
|
|
.content(content)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
).build()
|
|
|
|
|
|
response = self._client.im.v1.message.create(request)
|
|
|
|
|
|
if not response.success():
|
|
|
|
|
|
logger.error(
|
2026-02-19 17:19:36 -03:00
|
|
|
|
"Failed to send Feishu {} message: code={}, msg={}, log_id={}",
|
|
|
|
|
|
msg_type, response.code, response.msg, response.get_log_id()
|
2026-02-04 14:07:45 +08:00
|
|
|
|
)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
return False
|
2026-02-20 07:55:34 +00:00
|
|
|
|
logger.debug("Feishu {} message sent to {}", msg_type, receive_id)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
return True
|
2026-02-04 14:07:45 +08:00
|
|
|
|
except Exception as e:
|
2026-02-19 17:19:36 -03:00
|
|
|
|
logger.error("Error sending Feishu {} message: {}", msg_type, e)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
return False
|
|
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
async def send(self, msg: OutboundMessage) -> None:
|
2026-02-19 16:31:00 +08:00
|
|
|
|
"""Send a message through Feishu, including media (images/files) if present."""
|
2026-02-04 14:07:45 +08:00
|
|
|
|
if not self._client:
|
|
|
|
|
|
logger.warning("Feishu client not initialized")
|
|
|
|
|
|
return
|
2026-02-19 16:31:00 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
try:
|
2026-02-19 17:33:08 +00:00
|
|
|
|
receive_id_type = "chat_id" if msg.chat_id.startswith("oc_") else "open_id"
|
2026-02-19 16:31:00 +08:00
|
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
|
|
|
2026-02-19 17:33:08 +00:00
|
|
|
|
for file_path in msg.media:
|
|
|
|
|
|
if not os.path.isfile(file_path):
|
2026-02-20 07:55:34 +00:00
|
|
|
|
logger.warning("Media file not found: {}", file_path)
|
2026-02-19 17:33:08 +00:00
|
|
|
|
continue
|
|
|
|
|
|
ext = os.path.splitext(file_path)[1].lower()
|
|
|
|
|
|
if ext in self._IMAGE_EXTS:
|
|
|
|
|
|
key = await loop.run_in_executor(None, self._upload_image_sync, file_path)
|
|
|
|
|
|
if key:
|
|
|
|
|
|
await loop.run_in_executor(
|
|
|
|
|
|
None, self._send_message_sync,
|
2026-02-20 07:59:32 +00:00
|
|
|
|
receive_id_type, msg.chat_id, "image", json.dumps({"image_key": key}, ensure_ascii=False),
|
2026-02-19 17:33:08 +00:00
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
key = await loop.run_in_executor(None, self._upload_file_sync, file_path)
|
|
|
|
|
|
if key:
|
2026-03-06 01:54:00 +08:00
|
|
|
|
# Use msg_type "media" for audio/video so users can play inline;
|
|
|
|
|
|
# "file" for everything else (documents, archives, etc.)
|
|
|
|
|
|
if ext in self._AUDIO_EXTS or ext in self._VIDEO_EXTS:
|
|
|
|
|
|
media_type = "media"
|
|
|
|
|
|
else:
|
|
|
|
|
|
media_type = "file"
|
2026-02-19 17:33:08 +00:00
|
|
|
|
await loop.run_in_executor(
|
|
|
|
|
|
None, self._send_message_sync,
|
2026-02-20 07:59:32 +00:00
|
|
|
|
receive_id_type, msg.chat_id, media_type, json.dumps({"file_key": key}, ensure_ascii=False),
|
2026-02-19 17:33:08 +00:00
|
|
|
|
)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
|
|
|
|
|
|
if msg.content and msg.content.strip():
|
2026-03-06 10:11:53 +08:00
|
|
|
|
fmt = self._detect_msg_format(msg.content)
|
|
|
|
|
|
|
|
|
|
|
|
if fmt == "text":
|
|
|
|
|
|
# Short plain text – send as simple text message
|
|
|
|
|
|
text_body = json.dumps({"text": msg.content.strip()}, ensure_ascii=False)
|
2026-03-01 15:13:44 +01:00
|
|
|
|
await loop.run_in_executor(
|
|
|
|
|
|
None, self._send_message_sync,
|
2026-03-06 10:11:53 +08:00
|
|
|
|
receive_id_type, msg.chat_id, "text", text_body,
|
2026-03-01 15:13:44 +01:00
|
|
|
|
)
|
2026-02-19 16:31:00 +08:00
|
|
|
|
|
2026-03-06 10:11:53 +08:00
|
|
|
|
elif fmt == "post":
|
|
|
|
|
|
# Medium content with links – send as rich-text post
|
|
|
|
|
|
post_body = self._markdown_to_post(msg.content)
|
|
|
|
|
|
await loop.run_in_executor(
|
|
|
|
|
|
None, self._send_message_sync,
|
|
|
|
|
|
receive_id_type, msg.chat_id, "post", post_body,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
# Complex / long content – send as interactive card
|
|
|
|
|
|
elements = self._build_card_elements(msg.content)
|
|
|
|
|
|
for chunk in self._split_elements_by_table_limit(elements):
|
|
|
|
|
|
card = {"config": {"wide_screen_mode": True}, "elements": chunk}
|
|
|
|
|
|
await loop.run_in_executor(
|
|
|
|
|
|
None, self._send_message_sync,
|
|
|
|
|
|
receive_id_type, msg.chat_id, "interactive", json.dumps(card, ensure_ascii=False),
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
except Exception as e:
|
2026-02-19 17:19:36 -03:00
|
|
|
|
logger.error("Error sending Feishu message: {}", e)
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
def _on_message_sync(self, data: "P2ImMessageReceiveV1") -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Sync handler for incoming messages (called from WebSocket thread).
|
|
|
|
|
|
Schedules async handling in the main event loop.
|
|
|
|
|
|
"""
|
2026-02-05 06:01:02 +00:00
|
|
|
|
if self._loop and self._loop.is_running():
|
|
|
|
|
|
asyncio.run_coroutine_threadsafe(self._on_message(data), self._loop)
|
2026-02-28 20:55:43 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
async def _on_message(self, data: "P2ImMessageReceiveV1") -> None:
|
|
|
|
|
|
"""Handle incoming message from Feishu."""
|
|
|
|
|
|
try:
|
|
|
|
|
|
event = data.event
|
|
|
|
|
|
message = event.message
|
|
|
|
|
|
sender = event.sender
|
2026-02-21 12:56:57 +08:00
|
|
|
|
|
2026-02-05 06:01:02 +00:00
|
|
|
|
# Deduplication check
|
2026-02-04 14:07:45 +08:00
|
|
|
|
message_id = message.message_id
|
|
|
|
|
|
if message_id in self._processed_message_ids:
|
|
|
|
|
|
return
|
2026-02-05 06:01:02 +00:00
|
|
|
|
self._processed_message_ids[message_id] = None
|
2026-02-21 12:56:57 +08:00
|
|
|
|
|
|
|
|
|
|
# Trim cache
|
2026-02-05 06:01:02 +00:00
|
|
|
|
while len(self._processed_message_ids) > 1000:
|
|
|
|
|
|
self._processed_message_ids.popitem(last=False)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
# Skip bot messages
|
2026-02-21 12:56:57 +08:00
|
|
|
|
if sender.sender_type == "bot":
|
2026-02-04 14:07:45 +08:00
|
|
|
|
return
|
2026-02-21 12:56:57 +08:00
|
|
|
|
|
2026-02-05 06:01:02 +00:00
|
|
|
|
sender_id = sender.sender_id.open_id if sender.sender_id else "unknown"
|
2026-02-04 14:07:45 +08:00
|
|
|
|
chat_id = message.chat_id
|
2026-02-21 12:56:57 +08:00
|
|
|
|
chat_type = message.chat_type
|
2026-02-04 14:07:45 +08:00
|
|
|
|
msg_type = message.message_type
|
2026-02-21 12:56:57 +08:00
|
|
|
|
|
|
|
|
|
|
# Add reaction
|
2026-02-27 11:45:44 +08:00
|
|
|
|
await self._add_reaction(message_id, self.config.react_emoji)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
|
|
|
|
|
|
# Parse content
|
|
|
|
|
|
content_parts = []
|
|
|
|
|
|
media_paths = []
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
content_json = json.loads(message.content) if message.content else {}
|
|
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
|
|
content_json = {}
|
|
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
if msg_type == "text":
|
2026-02-21 12:56:57 +08:00
|
|
|
|
text = content_json.get("text", "")
|
|
|
|
|
|
if text:
|
|
|
|
|
|
content_parts.append(text)
|
|
|
|
|
|
|
2026-02-14 12:14:31 +08:00
|
|
|
|
elif msg_type == "post":
|
2026-02-24 13:42:07 +08:00
|
|
|
|
text, image_keys = _extract_post_content(content_json)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
if text:
|
|
|
|
|
|
content_parts.append(text)
|
2026-02-24 13:42:07 +08:00
|
|
|
|
# Download images embedded in post
|
|
|
|
|
|
for img_key in image_keys:
|
|
|
|
|
|
file_path, content_text = await self._download_and_save_media(
|
|
|
|
|
|
"image", {"image_key": img_key}, message_id
|
|
|
|
|
|
)
|
|
|
|
|
|
if file_path:
|
|
|
|
|
|
media_paths.append(file_path)
|
|
|
|
|
|
content_parts.append(content_text)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
|
2026-02-22 17:37:33 +08:00
|
|
|
|
elif msg_type in ("image", "audio", "file", "media"):
|
2026-02-21 14:08:25 +08:00
|
|
|
|
file_path, content_text = await self._download_and_save_media(msg_type, content_json, message_id)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
if file_path:
|
|
|
|
|
|
media_paths.append(file_path)
|
|
|
|
|
|
content_parts.append(content_text)
|
|
|
|
|
|
|
2026-02-21 14:08:25 +08:00
|
|
|
|
elif msg_type in ("share_chat", "share_user", "interactive", "share_calendar_event", "system", "merge_forward"):
|
|
|
|
|
|
# Handle share cards and interactive messages
|
|
|
|
|
|
text = _extract_share_card_content(content_json, msg_type)
|
|
|
|
|
|
if text:
|
|
|
|
|
|
content_parts.append(text)
|
|
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
else:
|
2026-02-21 12:56:57 +08:00
|
|
|
|
content_parts.append(MSG_TYPE_MAP.get(msg_type, f"[{msg_type}]"))
|
|
|
|
|
|
|
|
|
|
|
|
content = "\n".join(content_parts) if content_parts else ""
|
|
|
|
|
|
|
|
|
|
|
|
if not content and not media_paths:
|
2026-02-04 14:07:45 +08:00
|
|
|
|
return
|
2026-02-21 12:56:57 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
# Forward to message bus
|
|
|
|
|
|
reply_to = chat_id if chat_type == "group" else sender_id
|
|
|
|
|
|
await self._handle_message(
|
|
|
|
|
|
sender_id=sender_id,
|
|
|
|
|
|
chat_id=reply_to,
|
|
|
|
|
|
content=content,
|
2026-02-21 12:56:57 +08:00
|
|
|
|
media=media_paths,
|
2026-02-04 14:07:45 +08:00
|
|
|
|
metadata={
|
|
|
|
|
|
"message_id": message_id,
|
|
|
|
|
|
"chat_type": chat_type,
|
|
|
|
|
|
"msg_type": msg_type,
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2026-02-21 12:56:57 +08:00
|
|
|
|
|
2026-02-04 14:07:45 +08:00
|
|
|
|
except Exception as e:
|
2026-02-19 17:19:36 -03:00
|
|
|
|
logger.error("Error processing Feishu message: {}", e)
|