feat(dingtalk): add support for group chat messages

This commit is contained in:
shawn_wxn
2026-03-06 19:08:44 +08:00
parent 3508909ae4
commit 26670d3e80

View File

@@ -70,6 +70,13 @@ class NanobotDingTalkHandler(CallbackHandler):
sender_id = chatbot_msg.sender_staff_id or chatbot_msg.sender_id sender_id = chatbot_msg.sender_staff_id or chatbot_msg.sender_id
sender_name = chatbot_msg.sender_nick or "Unknown" sender_name = chatbot_msg.sender_nick or "Unknown"
# Extract conversation info
conversation_type = message.data.get("conversationType")
conversation_id = message.data.get("conversationId") or message.data.get("openConversationId")
if conversation_type == "2" and conversation_id:
sender_id = f"group:{conversation_id}"
logger.info("Received DingTalk message from {} ({}): {}", sender_name, sender_id, content) logger.info("Received DingTalk message from {} ({}): {}", sender_name, sender_id, content)
# Forward to Nanobot via _on_message (non-blocking). # Forward to Nanobot via _on_message (non-blocking).
@@ -301,14 +308,25 @@ class DingTalkChannel(BaseChannel):
logger.warning("DingTalk HTTP client not initialized, cannot send") logger.warning("DingTalk HTTP client not initialized, cannot send")
return False return False
url = "https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend"
headers = {"x-acs-dingtalk-access-token": token} headers = {"x-acs-dingtalk-access-token": token}
payload = { if chat_id.startswith("group:"):
"robotCode": self.config.client_id, # Group chat
"userIds": [chat_id], url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send"
"msgKey": msg_key, payload = {
"msgParam": json.dumps(msg_param, ensure_ascii=False), "robotCode": self.config.client_id,
} "openConversationId": chat_id[6:], # Remove "group:" prefix,
"msgKey": "sampleMarkdown",
"msgParam": json.dumps(msg_param, ensure_ascii=False),
}
else:
# Private chat
url = "https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend"
payload = {
"robotCode": self.config.client_id,
"userIds": [chat_id],
"msgKey": msg_key,
"msgParam": json.dumps(msg_param, ensure_ascii=False),
}
try: try:
resp = await self._http.post(url, json=payload, headers=headers) resp = await self._http.post(url, json=payload, headers=headers)