pythontelegrampyrogramtelegram-api

How to output the list of channels in the console that are of certain channel type using pyrogram python library?


I am trying to print out all channels of specific category. I have initialised the session, the user-bot is going through the chats, but does not print to the console the chats that meet the requirements of conditional If-statement.

When I execute:

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

I get an output like this for all the chats I have:

{
    "_": "Chat",
    "id": -4031246626,
    "type": "ChatType.GROUP",
    "is_creator": true,
    "title": "comments",
    "has_protected_content": false,
    "members_count": 0
}

However, when I try to output specific information about "dialog" (iterator from the loop) - the program does not output anything into the console. Here is the full code of the function, where "app" is the client:

async def main():
    async with app:
        print("[LOG] --- [INFO] ---> The client has Started.")
        
        # Get all dialogs (chats) the user is involved with
        
        try:
            async for dialog in app.get_dialogs():
                print(dialog.chat)
                if dialog.chat.type == "ChatType.PRIVATE":
                    print(f"Private Chat: {dialog.chat.title} | ID: {dialog.chat.id}")
                
        except Exception as e:
            print(f"[LOG] --- [ERROR] ---> {e}")
            
app.run(main())

What can I do to get the desired output I want?


Solution

  • I figured out the problem. Two things were wrong:

    1. I did not import the required modules, as such I have added:
        from pyrogram.enums import ChatType
    
    1. My arguments passed in the if-statement were wrong, and it should have been:
        if dialog.chat.type in [ChatType.GROUP, ChatType.SUPERGROUP, ChatType.CHANNEL]