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

If bot tries to reply to a message that replied to another message of the bot, the bot crashes. #135

Closed
iamcosmin opened this issue Jul 27, 2023 · 3 comments · Fixed by #136
Labels
bug Something isn't working investigating This issue is currently being investigated

Comments

@iamcosmin
Copy link
Contributor

// An event handler for all text messages.
bot.on(TeleverseEvent.text, (ctx) => ctx.reply('Hello world!'));

With this event handler, the bot will reply to every message you send.
Now, send a message, and the bot will reply to it.
Then, send another message, but reply to the message that the bot sent you.

The bot will crash with the following message:

Unhandled exception:
TelegramException [400]:
(Bad Request: message thread not found)
#0      HttpClient.postURI (package:televerse/src/utils/http.dart:32:7)
<asynchronous suspension>
#1      RawAPI.sendMessage (package:televerse/src/televerse/raw_api.dart:247:37)
<asynchronous suspension>
#2      MessageMixin.reply (package:televerse/src/televerse/context/mixins/message_mixin.dart:25:12)
<asynchronous suspension>
@HeySreelal
Copy link
Collaborator

Hey, thanks for shooting this. I'm aware of the issue, but I'm not sure what's causing it. I'll investigate and get back to you as soon as I know more.

Btw, this sounds like a bug.

@HeySreelal HeySreelal added bug Something isn't working investigating This issue is currently being investigated labels Jul 27, 2023
@HeySreelal
Copy link
Collaborator

TLDR; this seems like an issue with the Telegram servers.

I tried my best to replicate the error in multiple ways. I came to an interesting conclusion that this is actually a bug with the Telegram Bot API servers.

What exactly is happening?

As far as I know, incoming message updates from the General Topic have no message_thread_id parameter. (in other words messageThreadId will be null for General Topic messages). But for some reasons unknown, when the user "REPLIES" to bot's message in the General Topic, all of a sudden a message_thread_id appears on the incoming message update, it's not just that the message_thread_id received is a non-existing thread's ID.

Hence calling sendMessage or any methods to send messages fails and throws (Bad Request: message thread not found) exception.

In the context of Televerse

As of now (27/07/2023), Televerse has this implementation on the MessageMixin methods:

messageThreadId: messageThreadId ?? _msg.messageThreadId

This was to solve #110, whenever an update is received Televerse library will automatically set the messageThreadId to the incoming message updates messageThreadId. So when an invalid message_thread_id is passed by the Bot API this all comes to a fail.

Workaround

As I checked, this exception can be handled by the handler passed to Televerse.onError. You'll just have to tweak the code a little bit to await the ctx.reply(...). Here's the code:

  bot.onError((err, stackTrace) {
    print("We got problems!");
    /// handle it :)
  });

 bot.on(TeleverseEvent.text, (ctx) async {
    ctx as MessageContext;
    await ctx.reply("There we go...");
  });

But well, to be honest, this isn't a real solution - this is just catching the exception. In other words, your bot is not really replying to the user's message in General Topic (if the message is replying to another message of the bot). So, I really don't suggest this one. Otherwise, you'll have to manually send a reply using the sendMessage method in the bot.api getter.

Note

I have raised an issue in the tdlib/telegram-bot-api repo about this issue. See it here: tdlib/telegram-bot-api#447

👨🏻‍💻 Tough Decision

This leaves me with either two choices:

Hope this makes sense, regards :)

@HeySreelal
Copy link
Collaborator

HeySreelal commented Jul 28, 2023

This is fixed with a minor tweak. After the discussion with @levlam in tdlib/telegram-bot-api#447, I realized the Bot API has no bug and works as intended.

So, added a minor tweak to pass null to the messageThreadId parameter if the message doesn't belong to a Forum Topic (General Topic is not really a Topic).

This is released in V1.10.6. Thanks again for raising this @iamcosmin :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working investigating This issue is currently being investigated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants