Skip to content

Fix get_chat_history method to work properly with min_id and max_id#203

Merged
KurimuzonAkuma merged 5 commits into
KurimuzonAkuma:devfrom
Danipulok:fix/message-history
Aug 5, 2025
Merged

Fix get_chat_history method to work properly with min_id and max_id#203
KurimuzonAkuma merged 5 commits into
KurimuzonAkuma:devfrom
Danipulok:fix/message-history

Conversation

@Danipulok

@Danipulok Danipulok commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

PR Description: Fix get_chat_history Boundary and Reverse Issues

Overview

This pull request fixes critical bugs in Pyrogram's get_chat_history method, which failed to handle min_id, max_id, and reverse parameters correctly, often returning no messages or incorrect ranges. The changes ensure reliable message retrieval within specified boundaries and proper sorting.

Issues Fixed

  • Boundary Handling: The original method ignored or mishandled min_id and max_id combinations, returning no messages for valid ranges.
  • Reverse Mode: When reverse=True, the method often failed to return messages or started from incorrect IDs.
  • Pagination: Offset and pagination logic was inconsistent, leading to missing or duplicated messages.
  • Edge Cases: Small ranges (e.g., min_id=50, max_id=55) and invalid ranges (e.g., min_id > max_id) were not handled correctly.

Changes Made

  • Adjusted get_chunk to properly handle min_id and max_id as inclusive boundaries.
  • Fixed reverse iteration to start from the correct message ID (e.g., min_id or max_id+1).
  • Improved pagination to prevent missing messages and ensure correct order (ascending for reverse=True, descending otherwise).
  • Added edge case handling for small ranges and invalid inputs.

Example

To verify, use this example with a public Telegram group like @tginfo:

import asyncio
from pyrogram import Client

async def check_get_chat_history():
    async with Client("client") as app:
        async for msg in app.get_chat_history(
            chat_id="@tginfo",
            limit=10,
            min_id=100,
            max_id=500,
            reverse=True,
        ):
            print(f"Message ID: {msg.id}")

asyncio.run(check_get_chat_history())

Expected:

Message ID: 100
Message ID: 102
Message ID: 103
Message ID: 104
Message ID: 105
Message ID: 106
Message ID: 108
Message ID: 109
Message ID: 110
Message ID: 112

Current output:
<None>

Proof of Fix

Before the fix, queries with min_id and max_id (e.g., 10-100) or reverse=True often returned 0 messages or incorrect IDs. After the fix:

  • Queries with min_id=10, max_id=100 return messages in the correct range (e.g., 15 messages, IDs 78-99).
  • reverse=True correctly returns messages in ascending order (e.g., IDs 10-19 for limit=10).
  • Small ranges (e.g., min_id=50, max_id=55) return expected messages (e.g., 6 messages, IDs 50-55).
  • Pagination works without duplicates, covering the full range (e.g., 25 messages, IDs 30-57).

Conclusion

This PR resolves critical bugs in get_chat_history, ensuring accurate message retrieval with boundaries and reverse sorting. The changes are backward-compatible and can be verified with the provided example in any public Telegram chat.

@KurimuzonAkuma
KurimuzonAkuma merged commit e70f40d into KurimuzonAkuma:dev Aug 5, 2025
12 checks passed
SpEcHiDe added a commit to TelegramPlayground/PyroTGFork that referenced this pull request Aug 13, 2025
KurimuzonAkuma/kurigram#203

Co-authored-by: Danipulok <Danipulok@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants