feat(dingtalk): add support for group chat messages
This commit is contained in:
@@ -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,8 +308,19 @@ 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}
|
||||||
|
if chat_id.startswith("group:"):
|
||||||
|
# Group chat
|
||||||
|
url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send"
|
||||||
|
payload = {
|
||||||
|
"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 = {
|
payload = {
|
||||||
"robotCode": self.config.client_id,
|
"robotCode": self.config.client_id,
|
||||||
"userIds": [chat_id],
|
"userIds": [chat_id],
|
||||||
|
|||||||
Reference in New Issue
Block a user