fix: address second-round review feedback on bot identity PR
This commit is contained in:
@@ -149,6 +149,17 @@ def test_get_all_bot_accounts_includes_runtime_aliases(monkeypatch):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_all_bot_accounts_keeps_canonical_qq_identity(monkeypatch):
|
||||||
|
utils_module, _logger = load_utils_module(
|
||||||
|
monkeypatch,
|
||||||
|
qq_account=123456,
|
||||||
|
platforms=["qq:999999", "webui:888888", "TG:tg_bot"],
|
||||||
|
)
|
||||||
|
|
||||||
|
assert utils_module.get_all_bot_accounts()["qq"] == "123456"
|
||||||
|
assert utils_module.get_all_bot_accounts()["webui"] == "123456"
|
||||||
|
|
||||||
|
|
||||||
def test_unknown_platform_no_longer_falls_back_to_qq(monkeypatch):
|
def test_unknown_platform_no_longer_falls_back_to_qq(monkeypatch):
|
||||||
utils_module, logger = load_utils_module(monkeypatch, qq_account=123456, platforms=[])
|
utils_module, logger = load_utils_module(monkeypatch, qq_account=123456, platforms=[])
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ from src.common.logger import get_logger
|
|||||||
from src.common.database.database_model import Expression
|
from src.common.database.database_model import Expression
|
||||||
from src.common.database.database import get_db_session
|
from src.common.database.database import get_db_session
|
||||||
from src.common.data_models.expression_data_model import MaiExpression
|
from src.common.data_models.expression_data_model import MaiExpression
|
||||||
|
from src.chat.utils.utils import is_bot_self
|
||||||
from src.common.utils.utils_message import MessageUtils
|
from src.common.utils.utils_message import MessageUtils
|
||||||
from src.common.utils.system_utils import is_bot_self
|
|
||||||
|
|
||||||
from .expression_utils import check_expression_suitability, parse_expression_response
|
from .expression_utils import check_expression_suitability, parse_expression_response
|
||||||
|
|
||||||
|
|||||||
@@ -44,8 +44,10 @@ class DirectMessageSender:
|
|||||||
# 获取麦麦的信息
|
# 获取麦麦的信息
|
||||||
bot_user_id = get_bot_account(chat_stream.platform)
|
bot_user_id = get_bot_account(chat_stream.platform)
|
||||||
if not bot_user_id:
|
if not bot_user_id:
|
||||||
logger.warning(f"[私聊][{self.private_name}]平台 {chat_stream.platform} 未配置机器人账号,发送消息时回退到 QQ 账号")
|
bot_user_id = get_bot_account("qq")
|
||||||
bot_user_id = str(getattr(global_config.bot, "qq_account", "")).strip()
|
if not bot_user_id:
|
||||||
|
logger.error(f"[私聊][{self.private_name}]平台 {chat_stream.platform} 无可用机器人账号,无法发送消息")
|
||||||
|
raise RuntimeError("机器人账号未配置")
|
||||||
bot_user_info = UserInfo(
|
bot_user_info = UserInfo(
|
||||||
user_id=bot_user_id,
|
user_id=bot_user_id,
|
||||||
user_nickname=global_config.bot.nickname,
|
user_nickname=global_config.bot.nickname,
|
||||||
|
|||||||
@@ -1117,8 +1117,10 @@ class DefaultReplyer:
|
|||||||
"""构建单个发送消息"""
|
"""构建单个发送消息"""
|
||||||
bot_user_id = get_bot_account(self.chat_stream.platform)
|
bot_user_id = get_bot_account(self.chat_stream.platform)
|
||||||
if not bot_user_id:
|
if not bot_user_id:
|
||||||
logger.warning(f"平台 {self.chat_stream.platform} 未配置机器人账号,发送消息时回退到 QQ 账号")
|
bot_user_id = get_bot_account("qq")
|
||||||
bot_user_id = str(getattr(global_config.bot, "qq_account", "")).strip()
|
if not bot_user_id:
|
||||||
|
logger.error(f"平台 {self.chat_stream.platform} 无可用机器人账号,无法构建发送消息")
|
||||||
|
raise RuntimeError("机器人账号未配置")
|
||||||
|
|
||||||
maim_message = MessageBase(
|
maim_message = MessageBase(
|
||||||
message_info=BaseMessageInfo(
|
message_info=BaseMessageInfo(
|
||||||
|
|||||||
@@ -957,8 +957,10 @@ class PrivateReplyer:
|
|||||||
"""构建单个发送消息"""
|
"""构建单个发送消息"""
|
||||||
bot_user_id = get_bot_account(self.chat_stream.platform)
|
bot_user_id = get_bot_account(self.chat_stream.platform)
|
||||||
if not bot_user_id:
|
if not bot_user_id:
|
||||||
logger.warning(f"平台 {self.chat_stream.platform} 未配置机器人账号,发送消息时回退到 QQ 账号")
|
bot_user_id = get_bot_account("qq")
|
||||||
bot_user_id = str(getattr(global_config.bot, "qq_account", "")).strip()
|
if not bot_user_id:
|
||||||
|
logger.error(f"平台 {self.chat_stream.platform} 无可用机器人账号,无法构建发送消息")
|
||||||
|
raise RuntimeError("机器人账号未配置")
|
||||||
|
|
||||||
maim_message = MessageBase(
|
maim_message = MessageBase(
|
||||||
message_info=BaseMessageInfo(
|
message_info=BaseMessageInfo(
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ def get_all_bot_accounts() -> dict[str, str]:
|
|||||||
bot_accounts["tg"] = telegram_account
|
bot_accounts["tg"] = telegram_account
|
||||||
|
|
||||||
for platform_name, account in platform_accounts.items():
|
for platform_name, account in platform_accounts.items():
|
||||||
if platform_name in {"tg", "telegram"}:
|
if platform_name in {"tg", "telegram", "qq", "webui"}:
|
||||||
continue
|
continue
|
||||||
bot_accounts[platform_name] = account
|
bot_accounts[platform_name] = account
|
||||||
|
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ def find_messages(
|
|||||||
after_time=after_time,
|
after_time=after_time,
|
||||||
)
|
)
|
||||||
if filter_bot:
|
if filter_bot:
|
||||||
from src.chat.utils.utils import _get_configured_qq_account, get_all_bot_accounts
|
from src.chat.utils.utils import get_all_bot_accounts, get_bot_account
|
||||||
|
|
||||||
bot_accounts = get_all_bot_accounts()
|
bot_accounts = get_all_bot_accounts()
|
||||||
exclusion_conditions: list[Any] = []
|
exclusion_conditions: list[Any] = []
|
||||||
@@ -177,7 +177,7 @@ def find_messages(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 兼容旧数据:历史机器人消息在所有平台上都使用 QQ 账号进行存储。
|
# 兼容旧数据:历史机器人消息在所有平台上都使用 QQ 账号进行存储。
|
||||||
if qq_fallback := _get_configured_qq_account():
|
if qq_fallback := get_bot_account("qq"):
|
||||||
exclusion_conditions.append(Messages.user_id == qq_fallback)
|
exclusion_conditions.append(Messages.user_id == qq_fallback)
|
||||||
|
|
||||||
if exclusion_conditions:
|
if exclusion_conditions:
|
||||||
|
|||||||
@@ -85,8 +85,10 @@ async def _send_to_target(
|
|||||||
additional_config["selected_expressions"] = selected_expressions
|
additional_config["selected_expressions"] = selected_expressions
|
||||||
bot_user_id = get_bot_account(target_stream.platform)
|
bot_user_id = get_bot_account(target_stream.platform)
|
||||||
if not bot_user_id:
|
if not bot_user_id:
|
||||||
logger.warning(f"[SendService] 平台 {target_stream.platform} 未配置机器人账号,发送消息时回退到 QQ 账号")
|
bot_user_id = get_bot_account("qq")
|
||||||
bot_user_id = str(getattr(global_config.bot, "qq_account", "")).strip()
|
if not bot_user_id:
|
||||||
|
logger.error(f"[SendService] 平台 {target_stream.platform} 无可用机器人账号,无法发送消息")
|
||||||
|
return False
|
||||||
|
|
||||||
maim_message = MessageBase(
|
maim_message = MessageBase(
|
||||||
message_info=BaseMessageInfo(
|
message_info=BaseMessageInfo(
|
||||||
|
|||||||
Reference in New Issue
Block a user