Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Message.reply_text leads to Message thread not found #4205

Closed
aiastia opened this issue Apr 14, 2024 · 9 comments · Fixed by #4207
Closed

[BUG] Message.reply_text leads to Message thread not found #4205

aiastia opened this issue Apr 14, 2024 · 9 comments · Fixed by #4207
Labels

Comments

@aiastia
Copy link

aiastia commented Apr 14, 2024

Steps to Reproduce

It works when I use it directly in a group, but when I quote a message the send command doesn't work.

Expected behaviour

截图_20240414143721

Actual behaviour

async def get_hitokoto(update: Update, context: ContextTypes) -> None:
url = "https://v1.hitokoto.cn/"
await update.message.reply_text(f"{url }", parse_mode="Markdown",message_thread_id=None)

Operating System

docker

Version of Python, python-telegram-bot & dependencies

python-telegram-bot==21.1

Relevant log output

2024-04-14 06:31:43,530 - telegram.ext.Application - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/ext/_application.py", line 1277, in process_update
    await coroutine
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/ext/_handlers/basehandler.py", line 157, in handle_update
    return await self.callback(update, context)
  File "/workspaces/tg/mypackage/tgbot.py", line 745, in get_hitokoto
    await update.message.reply_text(f"{formatted_output}", parse_mode="Markdown",message_thread_id=None)
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/_message.py", line 1652, in reply_text
    return await self.get_bot().send_message(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 2846, in send_message
    return await super().send_message(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/_bot.py", line 1012, in send_message
    return await self._send_message(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 587, in _send_message
    result = await super()._send_message(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/_bot.py", line 731, in _send_message
    result = await self._post(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/_bot.py", line 615, in _post
    return await self._do_post(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/ext/_extbot.py", line 369, in _do_post
    return await self.rate_limiter.process_request(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/ext/_aioratelimiter.py", line 245, in process_request
    return await self._run_request(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/ext/_aioratelimiter.py", line 203, in _run_request
    return await callback(*args, **kwargs)
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/_bot.py", line 644, in _do_post
    result = await request.post(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/request/_baserequest.py", line 200, in post
    result = await self._request_wrapper(
  File "/home/codespace/.python/current/lib/python3.10/site-packages/telegram/request/_baserequest.py", line 379, in _request_wrapper
    raise BadRequest(message)
telegram.error.BadRequest: Message thread not found

Additional Context

No response

@Bibo-Joshi
Copy link
Member

Hi. Thanks for reaching out. I tried to reproduce the behavior, but don't see an issue. What I've tried is the following.

Use this mwe:

import logging

from telegram import Update
from telegram.ext import (
    ApplicationBuilder,
    ContextTypes,
    MessageHandler,
    filters,
    Defaults,
)

logging.basicConfig(
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.WARNING
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("telegram.Bot").setLevel(logging.DEBUG)
logging.getLogger("telegram.ext.ExtBot").setLevel(logging.DEBUG)


async def callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    chat_type = update.message.chat.type
    has_topics = bool(update.message.message_thread_id)
    await update.message.reply_text(
        f"Test reply for chat of type: {chat_type} with topics: {has_topics}",
        message_thread_id=None,
    )


if __name__ == "__main__":

    application = (
        ApplicationBuilder()
        .token("TOKEN")
        # I've tried with and without defaults.
        .defaults(Defaults(do_quote=True))
        .build()
    )
    application.add_handler(MessageHandler(filters.TEXT, callback))

    application.run_polling(allowed_updates=Update.ALL_TYPES)

Then I've sent messages to the bot in the following chats:

  1. private chat
  2. group chat
  3. general topic of a forum group
  4. special topic of a forum group

Everything worked as expected for me.

Can you please provide an MWE along with instructions on how to trigger the exception? Please mention group type, privacy settings of the bot, and any other relevant parameters.

@aiastia
Copy link
Author

aiastia commented Apr 14, 2024

import logging

from telegram import Update
from telegram.ext import (
    ApplicationBuilder,
    ContextTypes,
    MessageHandler,
    filters,
    Defaults,CommandHandler,
)

logging.basicConfig(
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.WARNING
)
# set higher logging level for httpx to avoid all GET and POST requests being logged
logging.getLogger("telegram.Bot").setLevel(logging.DEBUG)
logging.getLogger("telegram.ext.ExtBot").setLevel(logging.DEBUG)


async def callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    chat_type = update.message.chat.type
    has_topics = bool(update.message.message_thread_id)
    await update.message.reply_text(
        f"Test reply for chat of type: {chat_type} with topics: {has_topics}",
        message_thread_id=None,
    )


if __name__ == "__main__":

    application = (
        ApplicationBuilder()
        .token("token")
        # I've tried with and without defaults.
        .defaults(Defaults(do_quote=True))
        .build()
    )
    #application.add_handler(MessageHandler(filters.TEXT, callback))
    application.add_handler(CommandHandler("ja", callback))
    application.run_polling(allowed_updates=Update.ALL_TYPES)

@aiastia
Copy link
Author

aiastia commented Apr 14, 2024

Like this, when you use the ja command in this group, it works normally, but when you use ja to reply to a message in the group, an error message is reported.

@aiastia
Copy link
Author

aiastia commented Apr 14, 2024

Can you see the picture? If you use the command like the picture, you will get an error. like this
IMG_1361

@aiastia
Copy link
Author

aiastia commented Apr 14, 2024

IMG_1362

@Bibo-Joshi Bibo-Joshi changed the title [BUG] 21.1 await update.message.reply_text(f"{formatted_output}", parse_mode="Markdown",message_thread_id=None) [BUG] Message.reply_text leads to Message thread not found Apr 14, 2024
@Bibo-Joshi
Copy link
Member

Thanks for elaborating (though in the future, please send 1 comment instead of several and paste monospaced text instead of posting screenshots 😉 )

I think I found the issued

Message.message_thread_id can signify either the id of a forum topic. Or it can be the message_id of a message this message is a reply to. We have no way of knownig which one it is.
However, the parameter message_thread_id of send_message accepts only ids of forum topics. Passing a message id is not allowed (instead reply_parameters must be used).

My first idea was that we can fix that, by checking if Message.message_thread_id == Message.reply_to_message.message_id. however, this doesn't work either because:

Message 1

Message 2

Message 3

Here, Message3 will have message_thread_id==1 even though reply_to_message.message_id==2. I.e., 3 is a reply to 2, but since 2 is a reply to 1, the thread is the one of message 1.

TBH at first glance, I don't think how we can make this work reliably as long as we don't know if message_thread_id originates from a forum topic or from a message_id.


As a side note: if we get this to work, we should make reply_*.param.message_thread_id default to DEFAULT_NONE such that passing None actually removes the parameter.

@Bibo-Joshi
Copy link
Member

Poolitzer thankfully pointed me to Message.is_topic_message which is true exactly in the cases where message_thread_id is the id of a forum topic :) that way we can make it work. Have implemented most of the changes already and will open a PR shortly

@Bibo-Joshi
Copy link
Member

See #4207. @aiastia maybe you can install fro that branch and double check if that fixes the problem for you?

@aiastia
Copy link
Author

aiastia commented Apr 15, 2024

It worked. Thank you so much

@aiastia aiastia closed this as completed Apr 15, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
2 participants