feat(dingtalk): add group_user_isolation to separate sessions per user in group chats

Add a new config option group_user_isolation (default: false) to the
DingTalk channel. When enabled, each user in a group chat gets their own
session while bot replies are still routed to the shared group chat.
This commit is contained in:
李明振 2026-05-27 11:20:12 +08:00 committed by Xubin Ren
parent 0042f68f94
commit da0aafcfbd
2 changed files with 54 additions and 0 deletions

View File

@ -160,6 +160,7 @@ class DingTalkConfig(Base):
allow_from: list[str] = Field(default_factory=list)
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
class DingTalkChannel(BaseChannel):
@ -693,6 +694,9 @@ class DingTalkChannel(BaseChannel):
self.logger.info("inbound: {} from {}", content, sender_name)
is_group = conversation_type == "2" and conversation_id
chat_id = f"group:{conversation_id}" if is_group else sender_id
session_key = None
if is_group and self.config.group_user_isolation:
session_key = f"{self.name}:group:{conversation_id}:{sender_id}"
await self._handle_message(
sender_id=sender_id,
chat_id=chat_id,
@ -702,6 +706,7 @@ class DingTalkChannel(BaseChannel):
"platform": "dingtalk",
"conversation_type": conversation_type,
},
session_key=session_key,
)
except Exception:
self.logger.exception("Error publishing message")

View File

@ -98,6 +98,55 @@ async def test_group_message_keeps_sender_id_and_routes_chat_id() -> None:
assert msg.metadata["conversation_type"] == "2"
@pytest.mark.asyncio
async def test_group_user_isolation_false_uses_shared_session() -> None:
"""By default group messages share the same session_key."""
config = DingTalkConfig(
client_id="app", client_secret="secret", allow_from=["*"], group_user_isolation=False
)
bus = MessageBus()
channel = DingTalkChannel(config, bus)
for user_id in ("user1", "user2"):
await channel._on_message(
"hello",
sender_id=user_id,
sender_name=user_id,
conversation_type="2",
conversation_id="conv123",
)
msg1 = await bus.consume_inbound()
msg2 = await bus.consume_inbound()
assert msg1.session_key == msg2.session_key == "dingtalk:group:conv123"
assert msg1.chat_id == msg2.chat_id == "group:conv123"
@pytest.mark.asyncio
async def test_group_user_isolation_true_separates_sessions() -> None:
"""When group_user_isolation is True, each user gets their own session_key."""
config = DingTalkConfig(
client_id="app", client_secret="secret", allow_from=["*"], group_user_isolation=True
)
bus = MessageBus()
channel = DingTalkChannel(config, bus)
for user_id in ("user1", "user2"):
await channel._on_message(
"hello",
sender_id=user_id,
sender_name=user_id,
conversation_type="2",
conversation_id="conv123",
)
msg1 = await bus.consume_inbound()
msg2 = await bus.consume_inbound()
assert msg1.session_key == "dingtalk:group:conv123:user1"
assert msg2.session_key == "dingtalk:group:conv123:user2"
assert msg1.chat_id == msg2.chat_id == "group:conv123"
@pytest.mark.asyncio
async def test_group_send_uses_group_messages_api() -> None:
config = DingTalkConfig(client_id="app", client_secret="secret", allow_from=["*"])