diff --git a/locale/ja_JP/LC_MESSAGES/BetterForward.po b/locale/ja_JP/LC_MESSAGES/BetterForward.po index dd4c5bb..7b8a79d 100644 --- a/locale/ja_JP/LC_MESSAGES/BetterForward.po +++ b/locale/ja_JP/LC_MESSAGES/BetterForward.po @@ -1126,6 +1126,10 @@ msgstr "ユーザー {} との新しいスレッドを作成する" msgid "Failed to forward message from user {}" msgstr "ユーザー {} からのメッセージ転送できない" +#, python-brace-format +msgid "Replied-to message not found, sending without reply: {}" +msgstr "返信先メッセージが見つからないため、返信なしで送信します:{}" + #: src/handlers/message_handler.py:402 src/handlers/message_handler.py:405 #, python-brace-format msgid "Failed to forward message to user {}" diff --git a/locale/zh_CN/LC_MESSAGES/BetterForward.po b/locale/zh_CN/LC_MESSAGES/BetterForward.po index b50727b..37258a3 100644 --- a/locale/zh_CN/LC_MESSAGES/BetterForward.po +++ b/locale/zh_CN/LC_MESSAGES/BetterForward.po @@ -1114,6 +1114,10 @@ msgstr "与用户{}新建话题" msgid "Failed to forward message from user {}" msgstr "转发用户{}的消息失败" +#, python-brace-format +msgid "Replied-to message not found, sending without reply: {}" +msgstr "被回复的消息不存在,将忽略回复后发送:{}" + #: src/handlers/message_handler.py:402 src/handlers/message_handler.py:405 #, python-brace-format msgid "Failed to forward message to user {}" diff --git a/src/handlers/message_handler.py b/src/handlers/message_handler.py index 5bfa84f..c08de91 100644 --- a/src/handlers/message_handler.py +++ b/src/handlers/message_handler.py @@ -639,9 +639,24 @@ def _excerpt_quote(message: Message): return None return quote + @staticmethod + def _is_reply_not_found_error(exc: Exception) -> bool: + """Return True when Telegram cannot find the mapped reply target.""" + description = getattr(exc, "description", "") or "" + haystack = f"{exc} {description}".lower() + markers = ( + "message to be replied not found", + "replied message not found", + "reply message not found", + "message_to_be_replied_not_found", + ) + return any(marker in haystack for marker in markers) + @staticmethod def _is_quote_error(exc: Exception) -> bool: """Return True when Telegram rejected the quoted excerpt.""" + if MessageHandler._is_reply_not_found_error(exc): + return False description = getattr(exc, "description", "") or "" haystack = f"{exc} {description}".lower() return "quote" in haystack @@ -653,7 +668,12 @@ def _quote_blockquote(quote) -> str: @staticmethod def _reply_send_kwargs(reply_id: int | None, quote=None) -> dict: - """Build send_* kwargs for a mapped reply, including a native quote excerpt.""" + """Build send_* kwargs for a mapped reply, including a native quote excerpt. + + ``allow_sending_without_reply`` is set so Telegram can drop the reply + instead of failing. Cross-chat / forum-topic replies still reject a + missing target, so callers also retry once without reply kwargs. + """ if reply_id is None: return {} if quote is not None: @@ -664,9 +684,13 @@ def _reply_send_kwargs(reply_id: int | None, quote=None) -> dict: quote=quote.text, quote_entities=entities, quote_position=getattr(quote, "position", None), + allow_sending_without_reply=True, ) } - return {"reply_to_message_id": reply_id} + return { + "reply_to_message_id": reply_id, + "allow_sending_without_reply": True, + } @staticmethod def _embed_quote_excerpt(message: Message, msg_text: str | None, msg_caption: str | None, @@ -802,6 +826,19 @@ def _dispatch_send_by_type(self, message: Message, msg_text: str, msg_caption: s logger.error(_("Unsupported message type") + message.content_type) raise ValueError(_("Unsupported message type") + message.content_type) + def _send_dropping_missing_reply(self, send_fn, reply_id: int | None): + """Call ``send_fn(reply_id)``; retry once without a reply if the target is gone.""" + try: + return send_fn(reply_id) + except (PartialSendError, ApiTelegramException) as e: + api_error = e.cause if isinstance(e, PartialSendError) else e + already_sent = e.sent if isinstance(e, PartialSendError) else [] + if already_sent or reply_id is None or not self._is_reply_not_found_error(api_error): + raise + logger.warning( + _("Replied-to message not found, sending without reply: {}").format(api_error)) + return send_fn(None) + def _send_message_by_type(self, message: Message, msg_text: str, msg_caption: str, chat_id: int, thread_id: int = None, reply_id: int = None, silent: bool = False) -> list[Message]: @@ -825,9 +862,14 @@ def _send_message_by_type(self, message: Message, msg_text: str, msg_caption: st chat_id, thread_id, [self._quote_blockquote(quote)], None, silent) quote = None + def dispatch(current_reply_id: int | None, current_quote=None) -> list[Message]: + return self._dispatch_send_by_type( + message, msg_text, msg_caption, chat_id, thread_id, + current_reply_id, silent, current_quote) + try: - sent = self._dispatch_send_by_type( - message, msg_text, msg_caption, chat_id, thread_id, reply_id, silent, quote) + sent = self._send_dropping_missing_reply( + lambda current_reply_id: dispatch(current_reply_id, quote), reply_id) except (PartialSendError, ApiTelegramException) as e: api_error = e.cause if isinstance(e, PartialSendError) else e already_sent = e.sent if isinstance(e, PartialSendError) else [] @@ -844,9 +886,12 @@ def _send_message_by_type(self, message: Message, msg_text: str, msg_caption: st msg_text, msg_caption, needs_prefix = self._embed_quote_excerpt( message, msg_text, msg_caption, quote) if needs_prefix: - prefix = self._send_text_chunks( - chat_id, thread_id, [self._quote_blockquote(quote)], reply_id, silent) + prefix = self._send_dropping_missing_reply( + lambda current_reply_id: self._send_text_chunks( + chat_id, thread_id, [self._quote_blockquote(quote)], + current_reply_id, silent), + reply_id) reply_id = None - sent = self._dispatch_send_by_type( - message, msg_text, msg_caption, chat_id, thread_id, reply_id, silent, None) + sent = self._send_dropping_missing_reply( + lambda current_reply_id: dispatch(current_reply_id, None), reply_id) return prefix + sent diff --git a/tests/test_message_handler.py b/tests/test_message_handler.py index 7e50641..a22cb8a 100644 --- a/tests/test_message_handler.py +++ b/tests/test_message_handler.py @@ -1,6 +1,7 @@ """Core message forwarding and stale-topic compensation tests.""" import sqlite3 +from types import SimpleNamespace from unittest.mock import MagicMock, patch import pytest @@ -360,6 +361,7 @@ def test_full_reply_from_user_maps_to_source_message(handler_env): kwargs = bot.send_message.call_args.kwargs assert kwargs["chat_id"] == GROUP_ID assert kwargs["reply_to_message_id"] == admin_msg_id + assert kwargs["allow_sending_without_reply"] is True assert "reply_parameters" not in kwargs assert kwargs["text"] == "full reply" @@ -406,6 +408,7 @@ def test_manual_quote_from_user_forwards_excerpt(handler_env): assert reply.message_id == admin_msg_id assert reply.quote == "this is a long" assert reply.quote_position == 13 + assert reply.allow_sending_without_reply is True assert kwargs["text"] == "got it" @@ -441,6 +444,7 @@ def test_auto_quote_keeps_full_reply_behavior(handler_env): kwargs = bot.send_message.call_args.kwargs assert kwargs["reply_to_message_id"] == admin_msg_id + assert kwargs["allow_sending_without_reply"] is True assert "reply_parameters" not in kwargs assert kwargs["text"] == "swipe reply" @@ -483,6 +487,7 @@ def test_group_manual_quote_forwards_excerpt_to_user(handler_env): assert reply.message_id == user_msg_id assert reply.quote == "several things" assert reply.quote_position == 10 + assert reply.allow_sending_without_reply is True assert kwargs["text"] == "about this part" @@ -550,5 +555,279 @@ def test_invalid_quote_falls_back_to_embed_and_full_reply(handler_env): assert bot.send_message.call_count == 2 retry = bot.send_message.call_args.kwargs assert retry["reply_to_message_id"] == admin_msg_id + assert retry["allow_sending_without_reply"] is True assert "reply_parameters" not in retry assert retry["text"] == "
missing slice\ncomment" + + +def test_is_reply_not_found_error_detects_known_markers(): + assert MessageHandler._is_reply_not_found_error( + make_api_error("Bad Request: message to be replied not found") + ) + assert MessageHandler._is_reply_not_found_error( + make_api_error("Bad Request: replied message not found") + ) + assert MessageHandler._is_reply_not_found_error( + make_api_error("MESSAGE_TO_BE_REPLIED_NOT_FOUND") + ) + assert not MessageHandler._is_reply_not_found_error( + make_api_error("Bad Request: QUOTE_TEXT_INVALID") + ) + assert not MessageHandler._is_quote_error( + make_api_error("Bad Request: message to be replied not found") + ) + + +def test_reply_send_kwargs_sets_allow_sending_without_reply(): + assert MessageHandler._reply_send_kwargs(None) == {} + kwargs = MessageHandler._reply_send_kwargs(42) + assert kwargs == {"reply_to_message_id": 42, "allow_sending_without_reply": True} + quoted = MessageHandler._reply_send_kwargs(42, make_quote("slice", position=3)) + reply = quoted["reply_parameters"] + assert reply.message_id == 42 + assert reply.quote == "slice" + assert reply.allow_sending_without_reply is True + + +def _seed_admin_copy(db_path: str, user_id: int, thread_id: int, admin_msg_id=11, user_copy_id=22): + _seed_topic(db_path, user_id, thread_id) + with sqlite3.connect(db_path) as conn: + conn.execute( + "INSERT INTO messages (received_id, forwarded_id, topic_id, in_group) VALUES (?, ?, ?, ?)", + (admin_msg_id, user_copy_id, thread_id, True), + ) + conn.commit() + return admin_msg_id, user_copy_id + + +def test_user_reply_to_deleted_group_message_forwards_without_reply(handler_env): + handler, bot, cache, db_path = handler_env + user_id = 101 + thread_id = 555 + admin_msg_id, user_copy_id = _seed_admin_copy(db_path, user_id, thread_id) + cache.set(f"chat_{user_id}_threadid", thread_id) + + forwarded = MagicMock(spec=Message) + forwarded.message_id = 9100 + bot.send_message.side_effect = [ + make_api_error("Bad Request: message to be replied not found"), + forwarded, + ] + + source = make_message(message_id=user_copy_id, chat_id=user_id, user_id=0, text="old admin") + message = make_message( + message_id=90, + chat_id=user_id, + user_id=user_id, + text="有客服吗,这个", + reply_to_message=source, + ) + handler.handle_message(message) + + assert bot.send_message.call_count == 2 + first = bot.send_message.call_args_list[0].kwargs + retry = bot.send_message.call_args_list[1].kwargs + assert first["reply_to_message_id"] == admin_msg_id + assert first["allow_sending_without_reply"] is True + assert retry["text"] == "有客服吗,这个" + assert retry.get("reply_to_message_id") is None + assert "reply_parameters" not in retry + assert retry.get("allow_sending_without_reply") is None + bot.forward_message.assert_not_called() + with sqlite3.connect(db_path) as conn: + row = conn.execute( + "SELECT forwarded_id, topic_id FROM messages WHERE received_id = ?", + (90,), + ).fetchone() + assert row == (9100, thread_id) + + +def test_group_reply_to_deleted_user_message_forwards_without_reply(handler_env): + handler, bot, cache, db_path = handler_env + user_id = 303 + thread_id = 777 + user_msg_id = 11 + group_copy_id = 22 + _seed_topic(db_path, user_id, thread_id) + + forwarded = MagicMock(spec=Message) + forwarded.message_id = 5100 + bot.send_message.side_effect = [ + make_api_error("Bad Request: replied message not found"), + forwarded, + ] + + source = make_message( + message_id=group_copy_id, + chat_id=GROUP_ID, + chat_type="supergroup", + user_id=0, + text="user original", + message_thread_id=thread_id, + ) + message = make_message( + message_id=68, + chat_id=GROUP_ID, + chat_type="supergroup", + user_id=999, + text="admin reply", + message_thread_id=thread_id, + reply_to_message=source, + ) + handler.handle_message(message) + + assert bot.send_message.call_count == 2 + retry = bot.send_message.call_args.kwargs + assert retry["chat_id"] == user_id + assert retry["text"] == "admin reply" + assert retry.get("reply_to_message_id") is None + assert "reply_parameters" not in retry + alert_calls = [ + call for call in bot.send_message.call_args_list + if call.kwargs.get("chat_id") == GROUP_ID + or (call.args and call.args[0] == GROUP_ID) + ] + assert alert_calls == [] + + +def test_sticker_reply_to_deleted_message_retries_without_reply(handler_env): + handler, bot, cache, db_path = handler_env + user_id = 101 + thread_id = 555 + admin_msg_id, user_copy_id = _seed_admin_copy(db_path, user_id, thread_id) + cache.set(f"chat_{user_id}_threadid", thread_id) + + forwarded = MagicMock(spec=Message) + forwarded.message_id = 9101 + bot.send_sticker.side_effect = [ + make_api_error("Bad Request: message to be replied not found"), + forwarded, + ] + + source = make_message(message_id=user_copy_id, chat_id=user_id, user_id=0, text="old") + message = make_message( + message_id=91, + chat_id=user_id, + user_id=user_id, + text=None, + content_type="sticker", + sticker=SimpleNamespace(file_id="STICKER_ID"), + reply_to_message=source, + ) + handler.handle_message(message) + + assert bot.send_sticker.call_count == 2 + first = bot.send_sticker.call_args_list[0].kwargs + retry = bot.send_sticker.call_args_list[1].kwargs + assert first["reply_to_message_id"] == admin_msg_id + assert first["allow_sending_without_reply"] is True + assert retry.get("reply_to_message_id") is None + assert retry.get("allow_sending_without_reply") is None + bot.forward_message.assert_not_called() + + +def test_photo_reply_to_deleted_message_retries_without_reply(handler_env): + handler, bot, cache, db_path = handler_env + user_id = 101 + thread_id = 555 + admin_msg_id, user_copy_id = _seed_admin_copy(db_path, user_id, thread_id) + cache.set(f"chat_{user_id}_threadid", thread_id) + + forwarded = MagicMock(spec=Message) + forwarded.message_id = 9102 + bot.send_photo.side_effect = [ + make_api_error("Bad Request: message to be replied not found"), + forwarded, + ] + + source = make_message(message_id=user_copy_id, chat_id=user_id, user_id=0, text="old") + message = make_message( + message_id=92, + chat_id=user_id, + user_id=user_id, + text=None, + content_type="photo", + caption="see this", + photo=[SimpleNamespace(file_id="PHOTO_ID")], + reply_to_message=source, + ) + handler.handle_message(message) + + assert bot.send_photo.call_count == 2 + first = bot.send_photo.call_args_list[0].kwargs + retry = bot.send_photo.call_args_list[1].kwargs + assert first["reply_to_message_id"] == admin_msg_id + assert first["allow_sending_without_reply"] is True + assert first["caption"] == "see this" + assert retry.get("reply_to_message_id") is None + assert retry["caption"] == "see this" + bot.forward_message.assert_not_called() + + +def test_quoted_reply_to_deleted_message_drops_reply_not_quote_fallback(handler_env): + handler, bot, cache, db_path = handler_env + user_id = 101 + thread_id = 555 + admin_msg_id, user_copy_id = _seed_admin_copy(db_path, user_id, thread_id) + cache.set(f"chat_{user_id}_threadid", thread_id) + + forwarded = MagicMock(spec=Message) + forwarded.message_id = 9103 + bot.send_message.side_effect = [ + make_api_error("Bad Request: message to be replied not found"), + forwarded, + ] + + source = make_message(message_id=user_copy_id, chat_id=user_id, user_id=0, text="original") + message = make_message( + message_id=93, + chat_id=user_id, + user_id=user_id, + text="comment", + reply_to_message=source, + quote=make_quote("orig", position=0, is_manual=True), + ) + handler.handle_message(message) + + assert bot.send_message.call_count == 2 + first = bot.send_message.call_args_list[0].kwargs + retry = bot.send_message.call_args.kwargs + assert first["reply_parameters"].message_id == admin_msg_id + assert retry["text"] == "comment" + assert retry.get("reply_to_message_id") is None + assert "reply_parameters" not in retry + bot.forward_message.assert_not_called() + + +def test_invalid_quote_then_missing_reply_embeds_excerpt_without_reply(handler_env): + handler, bot, cache, db_path = handler_env + user_id = 101 + thread_id = 555 + admin_msg_id, user_copy_id = _seed_admin_copy(db_path, user_id, thread_id) + cache.set(f"chat_{user_id}_threadid", thread_id) + + forwarded = MagicMock(spec=Message) + forwarded.message_id = 9104 + bot.send_message.side_effect = [ + make_api_error("Bad Request: QUOTE_TEXT_INVALID"), + make_api_error("Bad Request: message to be replied not found"), + forwarded, + ] + + source = make_message(message_id=user_copy_id, chat_id=user_id, user_id=0, text="original") + message = make_message( + message_id=94, + chat_id=user_id, + user_id=user_id, + text="comment", + reply_to_message=source, + quote=make_quote("missing slice", position=2, is_manual=True), + ) + handler.handle_message(message) + + assert bot.send_message.call_count == 3 + retry = bot.send_message.call_args.kwargs + assert retry.get("reply_to_message_id") is None + assert "reply_parameters" not in retry + assert retry["text"] == "
missing slice\ncomment" + bot.forward_message.assert_not_called()