Merge pull request #6532 from sebastian0619/fix/vocechat-user-id

This commit is contained in:
jxxghp
2026-09-01 15:34:53 +08:00
committed by GitHub
2 changed files with 58 additions and 3 deletions
+13 -2
View File
@@ -236,15 +236,26 @@ class VoceChat:
def __send_request(self, userid: str, caption: str) -> bool: def __send_request(self, userid: str, caption: str) -> bool:
""" """
向VoceChat发送报文 向VoceChat发送报文
userid格式:UID#xxx / GID#xxx userid格式:数字用户ID / UID#xxx / GID#xxx
""" """
if not self._client: if not self._client:
return False return False
if userid.startswith("GID#"): if userid.startswith("GID#"):
action = "send_to_group" action = "send_to_group"
else: idstr = userid[4:]
elif userid.startswith("UID#"):
action = "send_to_user" action = "send_to_user"
idstr = userid[4:] idstr = userid[4:]
elif "#" not in userid:
action = "send_to_user"
idstr = userid
else:
logger.error(f"VoceChat消息接收对象格式错误:{userid}")
return False
if not idstr.isdigit():
logger.error(f"VoceChat消息接收对象ID无效:{userid}")
return False
with lock: with lock:
result = self._client.post_res(f"{self._host}api/bot/{action}/{idstr}", data=caption.encode("utf-8")) result = self._client.post_res(f"{self._host}api/bot/{action}/{idstr}", data=caption.encode("utf-8"))
+44
View File
@@ -0,0 +1,44 @@
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from app.modules.vocechat.vocechat import VoceChat
@pytest.mark.parametrize(
("userid", "endpoint"),
[
("123", "send_to_user/123"),
("UID#123", "send_to_user/123"),
("GID#456", "send_to_group/456"),
],
)
def test_send_msg_normalizes_vocechat_target(userid: str, endpoint: str):
client = Mock()
client.post_res.return_value = SimpleNamespace(status_code=200)
vocechat = VoceChat(
VOCECHAT_HOST="https://voce.example.com",
VOCECHAT_API_KEY="test-key",
VOCECHAT_CHANNEL_ID="456",
)
vocechat._client = client
assert vocechat.send_msg(title="测试消息", userid=userid) is True
assert client.post_res.call_args.args[0] == (
f"https://voce.example.com/api/bot/{endpoint}"
)
@pytest.mark.parametrize("userid", ["UID#", "GID#", "SID#123", "abc"])
def test_send_msg_rejects_invalid_vocechat_target(userid: str):
client = Mock()
vocechat = VoceChat(
VOCECHAT_HOST="https://voce.example.com",
VOCECHAT_API_KEY="test-key",
VOCECHAT_CHANNEL_ID="456",
)
vocechat._client = client
assert vocechat.send_msg(title="测试消息", userid=userid) is False
client.post_res.assert_not_called()