get_bot_chat_history()

Client.get_bot_chat_history(chat_id: int | str, start_message_id: int = 1, limit: int = 100, reverse: bool = False, chunk_size: int = 100) AsyncGenerator[Message, None]

Fetch message history for bots in private chats and groups by batch scanning message IDs.

This method works around Telegram’s bot limitation on messages.getHistory by querying batches of up to 100 message IDs concurrently using get_messages(). It skips non-existent or deleted messages and yields only valid Message objects.

Example

async for message in app.get_bot_chat_history(chat_id=123456789, start_message_id=1, limit=50):
    print(f"[{message.id}] {message.text}")
Parameters:
  • chat_id (int | str) – Unique identifier (int or username) of the target chat.

  • start_message_id (int, optional) – The message ID to begin scanning from. Defaults to 1.

  • limit (int, optional) – Maximum number of valid messages to yield. Defaults to 100.

  • reverse (bool, optional) – If True, scans downwards (decreasing IDs) from start_message_id. Defaults to False (scans upwards).

  • chunk_size (int, optional) – Number of message IDs to request in a single RPC batch (max 100). Defaults to 100.

Yields:

Message – Valid message objects in the requested range.

Fetch message history for bots in private chats and groups by batch scanning message IDs.