reo7sp/tgbot-cpp

I can't try-catch deleteMessage with messageId "message->replyToMessage->messageId"

Amirhan-Taipovjan-Greatest-I opened this issue · 3 comments

try {
    bot.getEvents().onCommand("delete", [&bot](Message::Ptr message) {
        if (StringTools::startsWith(message->text, "/start")) {
            return;
        }
        int numb = rand50();
        if (numb == 1) {
            try {
                bot.getApi().deleteMessage(message->chat->id, message->replyToMessage->messageId);
            }
            catch (exception& e) {
                printf("XATA BULMADY: %s\n", e.what());
            }
        }
        else bot.getApi().sendAnimation(message->chat->id, InputFile::fromFile("examples/cyrt.gif", "image/gif"), 0, 0, 0, "", "", message->messageId);
        });
}
catch (exception& e) {
    printf("XATA BULMADY: %s\n", e.what());
}

Even with try-catch block I keep getting this exception message like this:
image

I don't understand the error message. I assume it's something about memory access? If you debug this code section, then the error appears at the line with the deleteMessage()?
I have tested the code on my computer and it works: I set numb = 1. Fist, the bot sends a message. Then I reply to this message with the text "/delete" and the message is deleted.

I don't understand the error message. I assume it's something about memory access? If you debug this code section, then the error appears at the line with the deleteMessage()? I have tested the code on my computer and it works: I set numb = 1. Fist, the bot sends a message. Then I reply to this message with the text "/delete" and the message is deleted.

When someone tries to delete already requested to delete message this exception'll be thrown...
And yes, It's about memory access.

Yes, there is an error because replyToMessage (the already deleted message in this case) doesn't exist and points to zero. You would first have to check whether the message that is being replied to (replyToMessage) is valid at all and if so, then take the id of it.