test: 更新测试脚本以独立测试飞书卡片发送功能
- 将 test.py 改为同步请求方式,移除对 mcp 模块的依赖 - 新增 test_send_card.py 用于测试卡片模板的加载与渲染 - 更新资产确认卡片的参数格式,支持更灵活的资产列表结构 - 改进环境变量读取逻辑,兼容新旧配置键名
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import types
|
||||
from pathlib import Path
|
||||
import requests
|
||||
|
||||
|
||||
def main() -> None:
|
||||
@@ -9,33 +11,61 @@ def main() -> None:
|
||||
config_path = Path(__file__).with_name("mcp-server.json")
|
||||
if config_path.exists():
|
||||
config_data = json.loads(config_path.read_text(encoding="utf-8"))
|
||||
servers = config_data.get("mcpServers", {})
|
||||
env_data = (
|
||||
config_data.get("mcpServers", {})
|
||||
.get("lzwcai-mcpskills-lark-mcp", {})
|
||||
.get("env", {})
|
||||
servers.get("lzwcai-lark-mcp", {}).get("env", {})
|
||||
or servers.get("lzwcai-mcpskills-lark-mcp", {}).get("env", {})
|
||||
)
|
||||
if env_data.get("app_id") and env_data.get("app_secret"):
|
||||
os.environ["app_id"] = env_data["app_id"]
|
||||
os.environ["app_secret"] = env_data["app_secret"]
|
||||
if not os.getenv("app_id") or not os.getenv("app_secret"):
|
||||
raise RuntimeError("missing app_id or app_secret")
|
||||
from lzwcai_lark_mcp.main import LarkMcpServer
|
||||
if "mcp" not in sys.modules:
|
||||
mcp_module = types.ModuleType("mcp")
|
||||
types_module = types.ModuleType("mcp.types")
|
||||
class Tool:
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
class TextContent:
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
types_module.Tool = Tool
|
||||
types_module.TextContent = TextContent
|
||||
mcp_module.types = types_module
|
||||
sys.modules["mcp"] = mcp_module
|
||||
sys.modules["mcp.types"] = types_module
|
||||
from lzwcai_lark_mcp.tools import send_asset_confirmation_card
|
||||
|
||||
async def _run() -> None:
|
||||
server = LarkMcpServer()
|
||||
await server.ensure_token()
|
||||
user_id = "843ga2gb"
|
||||
result = send_asset_confirmation_card(
|
||||
server.tenant_access_token or "",
|
||||
user_id,
|
||||
"2026-02-13 10:30:00",
|
||||
["华为i手机"],
|
||||
["红米手机"]
|
||||
)
|
||||
print(result)
|
||||
|
||||
asyncio.run(_run())
|
||||
app_id = os.getenv("app_id", "")
|
||||
app_secret = os.getenv("app_secret", "")
|
||||
auth_url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
|
||||
response = requests.post(
|
||||
auth_url,
|
||||
json={"app_id": app_id, "app_secret": app_secret},
|
||||
headers={"Content-Type": "application/json"},
|
||||
timeout=10
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("code") not in (0, None):
|
||||
raise RuntimeError(f"lark auth failed: {data}")
|
||||
token = data.get("tenant_access_token", "")
|
||||
if not token:
|
||||
raise RuntimeError(f"lark auth response missing token: {data}")
|
||||
user_id = "gegg1d78"
|
||||
result = send_asset_confirmation_card(
|
||||
token,
|
||||
user_id,
|
||||
"CONF-20260301-001",
|
||||
"2026-03-01 10:30:00",
|
||||
[
|
||||
{"华为手机": "huawei_phone"},
|
||||
{"红米手机": "redmi_phone"},
|
||||
"MacBook Pro"
|
||||
],
|
||||
"如有误报请点击反馈"
|
||||
)
|
||||
print(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user