|
@@ -119,22 +119,41 @@ class FeishuWebhookClient:
|
|
|
link_url: str = "",
|
|
link_url: str = "",
|
|
|
signing_secret: str = "",
|
|
signing_secret: str = "",
|
|
|
) -> None:
|
|
) -> None:
|
|
|
|
|
+ elements: list[dict[str, Any]] = [
|
|
|
|
|
+ {
|
|
|
|
|
+ "tag": "div",
|
|
|
|
|
+ "text": {"tag": "plain_text", "content": str(line)[:1000]},
|
|
|
|
|
+ }
|
|
|
|
|
+ for line in lines
|
|
|
|
|
+ ]
|
|
|
|
|
+ if link_url:
|
|
|
|
|
+ elements.extend(
|
|
|
|
|
+ [
|
|
|
|
|
+ {"tag": "hr"},
|
|
|
|
|
+ {
|
|
|
|
|
+ "tag": "action",
|
|
|
|
|
+ "actions": [
|
|
|
|
|
+ {
|
|
|
|
|
+ "tag": "button",
|
|
|
|
|
+ "text": {
|
|
|
|
|
+ "tag": "plain_text",
|
|
|
|
|
+ "content": "打开 Telegram 对话",
|
|
|
|
|
+ },
|
|
|
|
|
+ "type": "primary",
|
|
|
|
|
+ "url": link_url,
|
|
|
|
|
+ }
|
|
|
|
|
+ ],
|
|
|
|
|
+ },
|
|
|
|
|
+ ]
|
|
|
|
|
+ )
|
|
|
card: dict[str, Any] = {
|
|
card: dict[str, Any] = {
|
|
|
"config": {"wide_screen_mode": True},
|
|
"config": {"wide_screen_mode": True},
|
|
|
"header": {
|
|
"header": {
|
|
|
"template": "blue",
|
|
"template": "blue",
|
|
|
"title": {"tag": "plain_text", "content": str(title)[:100]},
|
|
"title": {"tag": "plain_text", "content": str(title)[:100]},
|
|
|
},
|
|
},
|
|
|
- "elements": [
|
|
|
|
|
- {
|
|
|
|
|
- "tag": "div",
|
|
|
|
|
- "text": {"tag": "plain_text", "content": str(line)[:1000]},
|
|
|
|
|
- }
|
|
|
|
|
- for line in lines
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ "elements": elements,
|
|
|
}
|
|
}
|
|
|
- if link_url:
|
|
|
|
|
- card["card_link"] = {"url": link_url}
|
|
|
|
|
payload: dict[str, Any] = {
|
|
payload: dict[str, Any] = {
|
|
|
"msg_type": "interactive",
|
|
"msg_type": "interactive",
|
|
|
"card": card,
|
|
"card": card,
|
|
@@ -1010,12 +1029,10 @@ class BusinessAssistantRuntime:
|
|
|
metadata={"content_type": "text" if text else "unsupported"},
|
|
metadata={"content_type": "text" if text else "unsupported"},
|
|
|
)
|
|
)
|
|
|
await self._notify_feishu_customer_message(
|
|
await self._notify_feishu_customer_message(
|
|
|
- connection=connection,
|
|
|
|
|
conversation=conversation,
|
|
conversation=conversation,
|
|
|
message=message,
|
|
message=message,
|
|
|
sender=sender,
|
|
sender=sender,
|
|
|
settings=settings,
|
|
settings=settings,
|
|
|
- text=text,
|
|
|
|
|
)
|
|
)
|
|
|
if not connection.get("is_enabled") or not settings.get("assistant_enabled"):
|
|
if not connection.get("is_enabled") or not settings.get("assistant_enabled"):
|
|
|
return
|
|
return
|
|
@@ -1124,12 +1141,10 @@ class BusinessAssistantRuntime:
|
|
|
async def _notify_feishu_customer_message(
|
|
async def _notify_feishu_customer_message(
|
|
|
self,
|
|
self,
|
|
|
*,
|
|
*,
|
|
|
- connection: dict[str, Any],
|
|
|
|
|
conversation: dict[str, Any],
|
|
conversation: dict[str, Any],
|
|
|
message: dict[str, Any],
|
|
message: dict[str, Any],
|
|
|
sender: dict[str, Any],
|
|
sender: dict[str, Any],
|
|
|
settings: dict[str, Any],
|
|
settings: dict[str, Any],
|
|
|
- text: str,
|
|
|
|
|
) -> None:
|
|
) -> None:
|
|
|
webhook_url = str(settings.get("feishu_webhook_url") or "")
|
|
webhook_url = str(settings.get("feishu_webhook_url") or "")
|
|
|
if not settings.get("feishu_webhook_enabled") or not webhook_url:
|
|
if not settings.get("feishu_webhook_enabled") or not webhook_url:
|
|
@@ -1149,44 +1164,31 @@ class BusinessAssistantRuntime:
|
|
|
if sender.get("username")
|
|
if sender.get("username")
|
|
|
else str((message.get("chat") or {}).get("id") or "未知")
|
|
else str((message.get("chat") or {}).get("id") or "未知")
|
|
|
)
|
|
)
|
|
|
- account = connection.get("user") or {}
|
|
|
|
|
- account_name = " ".join(
|
|
|
|
|
- str(item).strip()
|
|
|
|
|
- for item in (account.get("first_name"), account.get("last_name"))
|
|
|
|
|
- if str(item or "").strip()
|
|
|
|
|
- ) or (f"@{account.get('username')}" if account.get("username") else "Business 账号")
|
|
|
|
|
- message_type = "文本" if text else "非文本"
|
|
|
|
|
preview_text = str(
|
|
preview_text = str(
|
|
|
message.get("text") or message.get("caption") or ""
|
|
message.get("text") or message.get("caption") or ""
|
|
|
).strip()
|
|
).strip()
|
|
|
- lines = [
|
|
|
|
|
- f"账号:{account_name}",
|
|
|
|
|
- f"用户:{user_name}",
|
|
|
|
|
- f"类型:{message_type}",
|
|
|
|
|
- ]
|
|
|
|
|
|
|
+ if settings.get("feishu_message_preview_enabled"):
|
|
|
|
|
+ message_content = preview_text[:500] if preview_text else "[非文本消息]"
|
|
|
|
|
+ else:
|
|
|
|
|
+ message_content = "消息已隐藏"
|
|
|
|
|
+ lines = [message_content]
|
|
|
telegram_username = str(sender.get("username") or "").strip().lstrip("@")
|
|
telegram_username = str(sender.get("username") or "").strip().lstrip("@")
|
|
|
telegram_chat_url = (
|
|
telegram_chat_url = (
|
|
|
f"https://t.me/{telegram_username}"
|
|
f"https://t.me/{telegram_username}"
|
|
|
if re.fullmatch(r"[A-Za-z0-9_]{1,64}", telegram_username)
|
|
if re.fullmatch(r"[A-Za-z0-9_]{1,64}", telegram_username)
|
|
|
else ""
|
|
else ""
|
|
|
)
|
|
)
|
|
|
- if settings.get("feishu_message_preview_enabled"):
|
|
|
|
|
- lines.append(
|
|
|
|
|
- f"内容:{preview_text[:500] if preview_text else '[非文本消息]'}"
|
|
|
|
|
- )
|
|
|
|
|
try:
|
|
try:
|
|
|
message_time = datetime.fromtimestamp(
|
|
message_time = datetime.fromtimestamp(
|
|
|
int(message.get("date") or 0), UTC
|
|
int(message.get("date") or 0), UTC
|
|
|
).astimezone(BEIJING_TIMEZONE).strftime("%Y-%m-%d %H:%M:%S")
|
|
).astimezone(BEIJING_TIMEZONE).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
except (OSError, OverflowError, TypeError, ValueError):
|
|
except (OSError, OverflowError, TypeError, ValueError):
|
|
|
message_time = "未知"
|
|
message_time = "未知"
|
|
|
- lines.append(f"时间(北京时间):{message_time}")
|
|
|
|
|
- if telegram_chat_url:
|
|
|
|
|
- lines.append("点击卡片打开 Telegram 对话")
|
|
|
|
|
|
|
+ lines.append(message_time)
|
|
|
try:
|
|
try:
|
|
|
await self.feishu.send_card(
|
|
await self.feishu.send_card(
|
|
|
webhook_url,
|
|
webhook_url,
|
|
|
- title="消息提醒",
|
|
|
|
|
|
|
+ title=user_name,
|
|
|
lines=lines,
|
|
lines=lines,
|
|
|
link_url=telegram_chat_url,
|
|
link_url=telegram_chat_url,
|
|
|
signing_secret=str(settings.get("feishu_webhook_signing_secret") or ""),
|
|
signing_secret=str(settings.get("feishu_webhook_signing_secret") or ""),
|
|
@@ -1229,8 +1231,8 @@ class BusinessAssistantRuntime:
|
|
|
) or (f"@{account.get('username')}" if account.get("username") else "Business 账号")
|
|
) or (f"@{account.get('username')}" if account.get("username") else "Business 账号")
|
|
|
await self.feishu.send_card(
|
|
await self.feishu.send_card(
|
|
|
webhook_url,
|
|
webhook_url,
|
|
|
- title="消息提醒",
|
|
|
|
|
- lines=["飞书群机器人连接测试成功。", f"账号:{account_name}"],
|
|
|
|
|
|
|
+ title="测试消息",
|
|
|
|
|
+ lines=["飞书群机器人连接测试成功。"],
|
|
|
signing_secret=str(settings.get("feishu_webhook_signing_secret") or ""),
|
|
signing_secret=str(settings.get("feishu_webhook_signing_secret") or ""),
|
|
|
)
|
|
)
|
|
|
return {"ok": True, "account": account_name}
|
|
return {"ok": True, "account": account_name}
|