fix(feishu): strip leading bot mention before commands
This commit is contained in:
@@ -56,6 +56,7 @@ def _make_feishu_event(
|
||||
sender_open_id: str = "ou_alice",
|
||||
parent_id: str | None = None,
|
||||
root_id: str | None = None,
|
||||
mentions=None,
|
||||
):
|
||||
message = SimpleNamespace(
|
||||
message_id=message_id,
|
||||
@@ -65,7 +66,7 @@ def _make_feishu_event(
|
||||
content=content,
|
||||
parent_id=parent_id,
|
||||
root_id=root_id,
|
||||
mentions=[],
|
||||
mentions=mentions or [],
|
||||
)
|
||||
sender = SimpleNamespace(
|
||||
sender_type="user",
|
||||
@@ -547,6 +548,71 @@ async def test_on_message_no_extra_api_call_when_no_parent_id() -> None:
|
||||
assert len(captured) == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_on_message_strips_required_leading_bot_mention_for_commands() -> None:
|
||||
channel = _make_feishu_channel(group_policy="mention")
|
||||
channel._processed_message_ids.clear()
|
||||
channel._bot_open_id = "ou_bot"
|
||||
captured = []
|
||||
|
||||
async def _capture(**kwargs):
|
||||
captured.append(kwargs)
|
||||
|
||||
channel._handle_message = _capture
|
||||
mention = SimpleNamespace(
|
||||
key="@_user_1",
|
||||
name="nanobot",
|
||||
id=SimpleNamespace(open_id="ou_bot", user_id=None),
|
||||
)
|
||||
|
||||
with patch.object(channel, "_add_reaction", return_value=None):
|
||||
await channel._on_message(
|
||||
_make_feishu_event(
|
||||
chat_type="group",
|
||||
content=json.dumps({"text": "@_user_1 /new"}),
|
||||
mentions=[mention],
|
||||
)
|
||||
)
|
||||
|
||||
assert len(captured) == 1
|
||||
assert captured[0]["content"] == "/new"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_on_message_keeps_longer_mention_key_that_shares_bot_prefix() -> None:
|
||||
channel = _make_feishu_channel(group_policy="mention")
|
||||
channel._processed_message_ids.clear()
|
||||
channel._bot_open_id = "ou_bot"
|
||||
captured = []
|
||||
|
||||
async def _capture(**kwargs):
|
||||
captured.append(kwargs)
|
||||
|
||||
channel._handle_message = _capture
|
||||
bot_mention = SimpleNamespace(
|
||||
key="@_user_1",
|
||||
name="nanobot",
|
||||
id=SimpleNamespace(open_id="ou_bot", user_id=None),
|
||||
)
|
||||
user_mention = SimpleNamespace(
|
||||
key="@_user_10",
|
||||
name="Alice",
|
||||
id=SimpleNamespace(open_id="ou_alice", user_id=None),
|
||||
)
|
||||
|
||||
with patch.object(channel, "_add_reaction", return_value=None):
|
||||
await channel._on_message(
|
||||
_make_feishu_event(
|
||||
chat_type="group",
|
||||
content=json.dumps({"text": "@_user_10 /new @_user_1"}),
|
||||
mentions=[bot_mention, user_mention],
|
||||
)
|
||||
)
|
||||
|
||||
assert len(captured) == 1
|
||||
assert captured[0]["content"] == "@Alice (ou_alice) /new @nanobot (ou_bot)"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Inbound media tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user