0

I have a chat id and a message id. I want to find out whether this message exists or not. So far the only way I can figure out to do it is a nasty hack (try to delete or edit message and hope the bot has no rights to actually do so, theoretically it should throw and exception "message not found" and not "no rights").

Surely there is a better way?

3 Answers3

1

In this example from_user_id means id of user which will get copied message.

try:
    msg_id = await config.bot.copy_message(from_user_id, group_id, message_id)
    await config.bot.delete_message(from_user_id, msg_id.message_id)
    message_exists = True
except TelegramBadRequest:
    message_exists = False
Magprone
  • 23
  • 6
0

As for now, (December 2022) Telegram Bot API doesn't have a direct method to check whether a message exists or not.

Old workaround

This question has been asked and as a workaround, it was suggested to use the forwardMessage method. But since the question was asked several updates have been released and one of them brought a new feature - Protected Content in Groups and Channels, and if it is enabled then forwardMessage returns 400 Bad Request error on an attempt to forward any message.

New workaround

As a new workaround copyMessage could be used, this method will succeed even in copying messages from groups with the enabled Protected Content feature. If the message was deleted, then 400 Bad Request error returns.

Here is aiogram's documentation on this method.

This workaround I found during reading documentation of another Python Telegram Bot library and here is an important note from there:

Since the release of Bot API 5.5 it can be impossible to forward messages from some chats. Use the attributes telegram.Message.has_protected_content and telegram.Chat.has_protected_content to check this.

As a workaround, it is still possible to use copy_message(). However, this behavior is undocumented and might be changed by Telegram.

elebur
  • 465
  • 1
  • 5
  • So basically I have a bot dump channel where bot dumps copied messages just to check they exist? – Alexei Andronov Dec 22 '22 at 09:23
  • @AlexeiAndronov, yes. But keep in mind that this might affect [_messages per second_](https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this) limit – elebur Dec 24 '22 at 11:02
0

In the python aiogram library, you can try to use the telegram.Bot.get_chat_member method to check if a message with a given message ID exists in a chat. This method returns a telegram.ChatMember object, which contains information about the user who sent the message.

I tried to write something that could check if a message with a given message ID exists in a chat, let me know if I understood your question correctly:

from aiogram import Bot

def check_message_exists(message_id, chat_id):
    bot = Bot.get_current()
    try:
        message = bot.get_chat_member(chat_id, message_id)
    except Exception as e:
        if str(e) == "Message to delete not found":
            return False
        else:
            raise e
    return True

If the message does not exist, it will raise an exception with the message "Message to delete not found". In this case, the function returns False. If the message does exist, the function returns True.

Note that this method requires the bot to be a member of the chat in which the message was sent. If the bot is not a member of the chat, it will not be able to access the message and will raise an exception.

Let me know if it helps!

bitfede
  • 31
  • 5
  • 1
    Strange, it gives me "User not found" for every message. I guess this is because it was sent in a channel? But it also gives "User not found" for forwarded messages in the channel So, I have two messages in that channel (bot is an admin) One is a forward from a user (no forward privacy is enabled for that user) Second is a post from the channel Both give "User not found" – Alexei Andronov Dec 22 '22 at 09:20