|
@@ -26,16 +26,15 @@ from enum import Enum
|
|
|
from typing import Any, Callable, Dict, List, Optional, Union
|
|
from typing import Any, Callable, Dict, List, Optional, Union
|
|
|
|
|
|
|
|
import lark_oapi as lark
|
|
import lark_oapi as lark
|
|
|
-from lark_oapi.api.contact.v3 import GetUserRequest, GetUserResponse
|
|
|
|
|
|
|
+from lark_oapi.api.contact.v3 import GetUserRequest
|
|
|
from lark_oapi.api.im.v1 import (
|
|
from lark_oapi.api.im.v1 import (
|
|
|
CreateMessageRequest, CreateMessageRequestBody,
|
|
CreateMessageRequest, CreateMessageRequestBody,
|
|
|
ReplyMessageRequest, ReplyMessageRequestBody,
|
|
ReplyMessageRequest, ReplyMessageRequestBody,
|
|
|
- GetMessageRequest, GetMessageResponse,
|
|
|
|
|
- PatchMessageRequest, PatchMessageRequestBody,
|
|
|
|
|
|
|
+ GetMessageRequest,
|
|
|
CreateImageRequest, CreateImageRequestBody,
|
|
CreateImageRequest, CreateImageRequestBody,
|
|
|
GetImageRequest,
|
|
GetImageRequest,
|
|
|
CreateFileRequest, CreateFileRequestBody,
|
|
CreateFileRequest, CreateFileRequestBody,
|
|
|
- GetMessageResourceRequest, GetMessageResourceResponse,
|
|
|
|
|
|
|
+ GetMessageResourceRequest,
|
|
|
ListMessageRequest, ListMessageResponse
|
|
ListMessageRequest, ListMessageResponse
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -116,6 +115,8 @@ class FeishuClient:
|
|
|
encrypt_key: 事件加密密钥 (可选)
|
|
encrypt_key: 事件加密密钥 (可选)
|
|
|
verification_token: 事件验证令牌 (可选)
|
|
verification_token: 事件验证令牌 (可选)
|
|
|
"""
|
|
"""
|
|
|
|
|
+ if not app_id or not app_secret:
|
|
|
|
|
+ raise RuntimeError("FEISHU_APP_ID and FEISHU_APP_SECRET are required")
|
|
|
self.app_id = app_id
|
|
self.app_id = app_id
|
|
|
self.app_secret = app_secret
|
|
self.app_secret = app_secret
|
|
|
self.domain = domain
|
|
self.domain = domain
|
|
@@ -557,7 +558,7 @@ class FeishuClient:
|
|
|
parsed = json.loads(content)
|
|
parsed = json.loads(content)
|
|
|
if item.msg_type == "text" and "text" in parsed:
|
|
if item.msg_type == "text" and "text" in parsed:
|
|
|
content = parsed["text"]
|
|
content = parsed["text"]
|
|
|
- except:
|
|
|
|
|
|
|
+ except (json.JSONDecodeError, TypeError):
|
|
|
pass
|
|
pass
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
@@ -618,7 +619,7 @@ class FeishuClient:
|
|
|
parsed = json.loads(content)
|
|
parsed = json.loads(content)
|
|
|
if item.msg_type == "text" and "text" in parsed:
|
|
if item.msg_type == "text" and "text" in parsed:
|
|
|
content = parsed["text"]
|
|
content = parsed["text"]
|
|
|
- except:
|
|
|
|
|
|
|
+ except (json.JSONDecodeError, TypeError):
|
|
|
pass
|
|
pass
|
|
|
|
|
|
|
|
messages.append({
|
|
messages.append({
|
|
@@ -813,7 +814,7 @@ class FeishuClient:
|
|
|
elif message_type == "post":
|
|
elif message_type == "post":
|
|
|
return self._parse_post_content(parsed)
|
|
return self._parse_post_content(parsed)
|
|
|
return content
|
|
return content
|
|
|
- except:
|
|
|
|
|
|
|
+ except (json.JSONDecodeError, TypeError, AttributeError):
|
|
|
return content
|
|
return content
|
|
|
|
|
|
|
|
def _parse_post_content(self, parsed: Dict) -> str:
|
|
def _parse_post_content(self, parsed: Dict) -> str:
|
|
@@ -876,8 +877,8 @@ class FeishuClient:
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|
|
|
# 从环境变量获取配置
|
|
# 从环境变量获取配置
|
|
|
- APP_ID = os.getenv("FEISHU_APP_ID", "cli_a90fe317987a9cc9")
|
|
|
|
|
- APP_SECRET = os.getenv("FEISHU_APP_SECRET", "nn2dWuXTiRA2N6xodbm4g0qz1AfM2ayi")
|
|
|
|
|
|
|
+ APP_ID = os.getenv("FEISHU_APP_ID")
|
|
|
|
|
+ APP_SECRET = os.getenv("FEISHU_APP_SECRET")
|
|
|
|
|
|
|
|
if not APP_ID or not APP_SECRET:
|
|
if not APP_ID or not APP_SECRET:
|
|
|
print("请设置环境变量 FEISHU_APP_ID 和 FEISHU_APP_SECRET")
|
|
print("请设置环境变量 FEISHU_APP_ID 和 FEISHU_APP_SECRET")
|
|
@@ -892,7 +893,7 @@ if __name__ == "__main__":
|
|
|
|
|
|
|
|
# 消息处理回调
|
|
# 消息处理回调
|
|
|
def handle_message(event: FeishuMessageEvent):
|
|
def handle_message(event: FeishuMessageEvent):
|
|
|
- print(f"\n收到消息:")
|
|
|
|
|
|
|
+ print("\n收到消息:")
|
|
|
print(f" 发送者: {event.sender_name or event.sender_open_id}")
|
|
print(f" 发送者: {event.sender_name or event.sender_open_id}")
|
|
|
print(f" 类型: {event.chat_type.value}")
|
|
print(f" 类型: {event.chat_type.value}")
|
|
|
print(f" 内容: {event.content}")
|
|
print(f" 内容: {event.content}")
|
|
@@ -902,10 +903,6 @@ if __name__ == "__main__":
|
|
|
if event.chat_type == ChatType.P2P or event.mentioned_bot:
|
|
if event.chat_type == ChatType.P2P or event.mentioned_bot:
|
|
|
# 先回复文字
|
|
# 先回复文字
|
|
|
reply_text = f"收到你的消息: {event.content}"
|
|
reply_text = f"收到你的消息: {event.content}"
|
|
|
- chat_id = event.chat_id
|
|
|
|
|
- content = event.content
|
|
|
|
|
- content_type = event.content_type # image、text等
|
|
|
|
|
- open_id = event.sender_open_id
|
|
|
|
|
client.send_message(
|
|
client.send_message(
|
|
|
to=event.chat_id,
|
|
to=event.chat_id,
|
|
|
text=reply_text,
|
|
text=reply_text,
|