pythontelegrampyrogram

How to get chat_id of chat in telegram?


Im using Pyrogram and Telegram Client API and i want to write program that will send messages to chats by using Pyrogram send_message() function. How can i get a unique identifier (int) of the target chat?

I tried to use peer id from browser developer console (f12), but when i put it inside the script it gives error: ValueError: Peer id invalid.


Solution

  • I asked ChatGPT to solve my problem and it worked:

    def get_ids():  
    with app:
        dialogs = app.get_dialogs()
        # Loop through all the dialogs (chats)
        for dialog in dialogs:
            chat = dialog.chat
            print(f"Chat ID: {chat.id}, Title: {chat.title or chat.first_name or chat.username}")