skip to Main Content

I’m currently trying to create something that needs to get the number of messages in a group/channel. The best way imo is to get the id of the latest message in this chat
As my bot will handle deleted messages, we don’t care about them

I tried that :

total_messages = await Client.get_chat_history_count(chat_id)

But I get the following error :

2022-06-17 13:22:34,479 - ERROR - pyrogram.dispatcher - MainThread - Telegram says: [400 BOT_METHOD_INVALID] - The method can't be used by bots (caused by "messages.GetHistory")
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pyrogram/dispatcher.py", line 240, in handler_worker
    await handler.callback(self.client, *args)
  File "/root/bot.py", line 117, in range
    total_mess = await teledump.get_chat_history_count(Var.idtodump)
  File "/usr/local/lib/python3.9/site-packages/pyrogram/methods/messages/get_chat_history_count.py", line 54, in get_chat_history_count
    r = await self.invoke(
  File "/usr/local/lib/python3.9/site-packages/pyrogram/methods/advanced/invoke.py", line 77, in invoke
    r = await self.session.invoke(
  File "/usr/local/lib/python3.9/site-packages/pyrogram/session/session.py", line 362, in invoke
    return await self.send(query, timeout=timeout)
  File "/usr/local/lib/python3.9/site-packages/pyrogram/session/session.py", line 332, in send
    RPCError.raise_it(result, type(data))
  File "/usr/local/lib/python3.9/site-packages/pyrogram/errors/rpc_error.py", line 91, in raise_it
    raise getattr(
pyrogram.errors.exceptions.bad_request_400.BotMethodInvalid: Telegram says: [400 BOT_METHOD_INVALID] - The method can't be used by bots (caused by "messages.GetHistory")

Any help would be appreciated !

3

Answers


  1. Telegram does not allow bots to retrieve the chat history, unless you retrieve a specific message by its chat and message id app.get_messages(-100123123, 123).

    What you might need is to watch all incoming messages and just store their ids, so when you need the most recent message, you already have its id stored for use.

    Login or Signup to reply.
  2. To get the message ID, i encourage you to dive into Telethon and use NewMessage. This method listen to every new message and gives you all the information you need.

    https://docs.telethon.dev/en/stable/modules/events.html#telethon.events.newmessage.NewMessage

    Here is how to get started:

    https://docs.telethon.dev/en/stable/basic/installation.html

    Good luck!

    from telethon import TelegramClient, events
    chat_ids = []
    client = TelegramClient('session', api_id, api_hash)
    
    @client.on(events.NewMessage(chats=chat_ids))
    async def newMessageListener(event):
         print(event.message.id)
    

    this will give you what you need.

    EDIT:

    chat_ids is an array of the chats you want to listen for, in your case, the ID of your chat.

    Login or Signup to reply.
  3. Send a message and get her id. Then subtract this id.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search