skip to Main Content

I am encountering an issue with my Python script that uses Pyrogram to interact with a private Telegram channel of which I am a member of (a userbot that does specific actions from my account – uses api_id/api_hash from my.telegram.org).

I derived the peer id from the URL of the private channel from Telegram Web (e.g.: https://web.telegram.org/k/#-2200488212 ). It used to work fine with private channels that were created some while ago (I assume telegram has created an update for private channels), but Pyrogram is unable to work with private channels that were created recently, specifically it says invalid peer id. It did work with peer ids that looked something like this: -10051622484 – with a "-100" in the beginning. The recent private channels/groups do not have the "-100" before their peer id, e.g.: -2200488212 . IDK if these are somehow related. Maybe I am wrong, however the same script worked fine with other channels, but struggles with this one.

How to fix this issue?

I looked up a similar issue in the youtube here is the video, a guy uses pyrofork instead of pyrogram in his code.

I tried to install the pyrofork into my virtual environment where my code works – did not help. Uninstalled the pyrogram and left only the pyrofork – encountered an error.

2

Answers


  1. While this might currently be a bug in pyrogram, you can try to get the chat_ids directly.

    Assuming you are able to join the channels and your client is named "app", the following code should achieve this:

    async for dialog in app.get_dialogs():
        print(dialog.chat.id)
    

    Please feel free to share your code for a more detailed answer.

    Login or Signup to reply.
  2. It’s Pyrogram’s bug. See: https://github.com/pyrogram/pyrogram/issues/1314

    Here’s the monkey patch.

    from pyrogram import utils
    
    def get_peer_type_new(peer_id: int) -> str:
        peer_id_str = str(peer_id)
        if not peer_id_str.startswith("-"):
            return "user"
        elif peer_id_str.startswith("-100"):
            return "channel"
        else:
            return "chat"
    
    utils.get_peer_type = get_peer_type_new
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search