新增飞书机器人适配器全套功能,包括: - 基础适配器入口与工具导出 - 消息格式化、卡片渲染、回复调度逻辑 - 会话ID生成、模型覆盖策略 - 消息发送缓存、顺序队列管理 - 飞书签名验证、加解密webhook请求 - 审批权限校验、机器人菜单事件处理 - 文档评论、钉消息、语音转码处理 - 静态/动态目录管理、子代理生命周期管理 - 各类工具集:聊天、云盘、文档、知识库API封装
123 lines
4.1 KiB
Python
123 lines
4.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
try:
|
|
import lark_oapi
|
|
|
|
HAS_LARK_SDK = True
|
|
except ImportError:
|
|
HAS_LARK_SDK = False
|
|
lark_oapi = None # type: ignore
|
|
|
|
|
|
async def create_doc(client: Any, title: str, folder_token: str = "") -> dict[str, Any]:
|
|
if not HAS_LARK_SDK or not client:
|
|
raise RuntimeError("SDK 不可用")
|
|
|
|
try:
|
|
request_body = lark_oapi.api.docx.v1.CreateDocumentRequestBody.builder().title(title).build()
|
|
if folder_token:
|
|
request_body.folder_token = folder_token
|
|
|
|
request = lark_oapi.api.docx.v1.CreateDocumentRequest.builder().request_body(request_body).build()
|
|
resp = await client.docx.v1.document.create(request)
|
|
if not resp.success():
|
|
raise RuntimeError(f"创建文档失败: {resp.msg}")
|
|
|
|
data = resp.data if hasattr(resp, "data") else {}
|
|
doc = data.get("document", {})
|
|
return {
|
|
"document_id": doc.get("document_id", ""),
|
|
"title": doc.get("title", title),
|
|
"url": doc.get("url", ""),
|
|
}
|
|
except Exception as e:
|
|
raise RuntimeError(f"创建文档失败: {e}") from e
|
|
|
|
|
|
async def get_doc_content(client: Any, document_id: str) -> dict[str, Any]:
|
|
if not HAS_LARK_SDK or not client:
|
|
raise RuntimeError("SDK 不可用")
|
|
|
|
try:
|
|
request = lark_oapi.api.docx.v1.GetDocumentRequest.builder().document_id(document_id).build()
|
|
resp = await client.docx.v1.document.get(request)
|
|
if not resp.success():
|
|
raise RuntimeError(f"获取文档失败: {resp.msg}")
|
|
|
|
data = resp.data if hasattr(resp, "data") else {}
|
|
doc = data.get("document", {})
|
|
blocks = doc.get("blocks", [])
|
|
return {
|
|
"document_id": doc.get("document_id", document_id),
|
|
"title": doc.get("title", ""),
|
|
"block_count": len(blocks),
|
|
"blocks": _extract_doc_blocks(blocks),
|
|
}
|
|
except Exception as e:
|
|
raise RuntimeError(f"获取文档失败: {e}") from e
|
|
|
|
|
|
async def list_docs(client: Any, folder_token: str = "", page_size: int = 50) -> list[dict[str, Any]]:
|
|
if not HAS_LARK_SDK or not client:
|
|
return []
|
|
|
|
docs: list[dict[str, Any]] = []
|
|
page_token = ""
|
|
try:
|
|
while True:
|
|
request = (
|
|
lark_oapi.api.docx.v1.ListDocumentRequest.builder()
|
|
.page_size(page_size)
|
|
.page_token(page_token)
|
|
.folder_token(folder_token)
|
|
.build()
|
|
)
|
|
resp = await client.docx.v1.document.list(request)
|
|
if not resp.success():
|
|
break
|
|
|
|
data = resp.data if hasattr(resp, "data") else {}
|
|
items = data.get("items", [])
|
|
for item in items:
|
|
docs.append(
|
|
{
|
|
"document_id": item.get("document_id", ""),
|
|
"title": item.get("title", ""),
|
|
"url": item.get("url", ""),
|
|
"create_time": item.get("create_time", ""),
|
|
"edit_time": item.get("edit_time", ""),
|
|
}
|
|
)
|
|
|
|
page_token = data.get("page_token", "")
|
|
if not page_token or not items:
|
|
break
|
|
except Exception as e:
|
|
raise RuntimeError(f"获取文档列表失败: {e}") from e
|
|
|
|
return docs
|
|
|
|
|
|
def _extract_doc_blocks(blocks: list) -> list[dict[str, Any]]:
|
|
result: list[dict[str, Any]] = []
|
|
for block in blocks:
|
|
block_type = block.get("block_type", block.get("blockType", 0))
|
|
extracted = {"block_type": block_type}
|
|
|
|
text_content = ""
|
|
for elem_type in ("text", "heading1", "heading2", "heading3", "heading4", "heading5", "bullet", "ordered"):
|
|
elem = block.get(elem_type, {})
|
|
if elem:
|
|
elements = elem.get("elements", [])
|
|
for e in elements:
|
|
text_run = e.get("text_run", {})
|
|
text_content += text_run.get("content", "")
|
|
|
|
if text_content:
|
|
extracted["content"] = text_content
|
|
result.append(extracted)
|
|
|
|
return result
|