feat(dingtalk): add disable_private_chat to reject 1:1 DMs

Add a `disable_private_chat` config flag (JSON alias `disablePrivateChat`,
default False) to the DingTalk channel. When enabled, any non-group (1:1)
message is rejected with a Chinese notice directing the user to group chat
("该机器人未开启私聊,请在群聊中与我对话。") before permission/pairing logic
runs, so even allowlisted senders are redirected. Group messages are
unaffected.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
李明振andClaude authored and Xubin Ren committed 2026-07-27 02:33:41 +08:00
1 parent 68717937e8
commit 14e692e40d
2 files changed
+95

No files matched your search

+16
View File
@@ -175,6 +175,7 @@ class DingTalkConfig(Base):
allow_remote_media_redirects: bool = False
remote_media_redirect_allowed_hosts: list[str] = Field(default_factory=list)
group_user_isolation: bool = False # If True, each user in group chat gets their own session
disable_private_chat: bool = False # If True, reject 1:1 DMs with a notice; group chats only
class DingTalkChannel(BaseChannel):
@@ -750,6 +751,21 @@ class DingTalkChannel(BaseChannel):
session_key = None
if is_group and self.config.group_user_isolation:
session_key = f"{self.name}:group:{conversation_id}:{sender_id}"
if not is_group and self.config.disable_private_chat:
# Private chat is disabled: reply with a notice and drop the
# message before any permission/pairing logic runs, so even
# allowlisted users are redirected to group chat.
self.logger.info("private chat disabled; rejecting DM from {}", sender_name)
await self.send(
OutboundMessage(
channel=self.name,
chat_id=str(chat_id),
content="该机器人未开启私聊,请在群聊中与我对话。",
)
)
return
await self._handle_message(
sender_id=sender_id,
chat_id=chat_id,