skip to Main Content

i tried pyrogram.raw.functions.messages.DeleteChat but get following trackback

pyrogram.errors.exceptions.bad_request_400.PeerIdInvalid: Telegram says: [400 PEER_ID_INVALID] - The peer id being used is invalid or not known yet. Make sure you meet the peer before interacting with it (caused by "messages.DeleteChat")

account.UpdateNotifySettings has the same problem too.

await client.invoke(UpdateNotifySettings(peer=await client.resolve_peer(cid),settings=InputPeerNotifySettings(silent=True)))

i have read this doc and i am sure that the id is correct for client.archive_chats works well with the same id.

the id is like this 5126101582 is there any another kinds of id or my code is wrong

note:
what i need is like this:

enter image description here

3

Answers


  1. Chosen as BEST ANSWER

    fine

    await client.invoke(DeleteHistory(max_id=0, peer=await client.resolve_peer(cid))
    

    works


  2. UPDATE

    Ok, I THINK I’ve found a solution for personal chats too!
    I was messing around with something else and reading this part of the documentation, I have come up with a way of listing every conversation and their respective id:

    from pyrogram import Client
    
    app = Client("my_client")
    
    
    async def main():
        async with app:
            async for dialog in app.get_dialogs():
                print(str(dialog.chat.id) + " - " + str(dialog.chat.first_name or str(dialog.chat.title)) )
    
    
    app.run(main()) 
    

    Basically what it does is loop through all your chats and output their id and "title" in case of a group/channel and a name in case of a chat with a person. You will notice that some ids will be output with a hyphen (-) in front of them, and some won’t.
    You will need to copy that exact string with or without the hyphen and then you can do this to delete all messages from a chat:

    from pyrogram import Client
    
    app = Client("Telecom")
    
    async def main():
        
        await app.start()
        async for message in app.get_chat_history("1212345678"):
            await app.delete_messages("1212345678", message.id)
    
    app.run(main())
    

    ————————— END OF UPDATE ————————

    I could not understand clearly if you want to delete only the messages of a specific chat or if you want to delete the chat per se.
    Anyways, here’s what the documentation says:

    chat_id (int | str) – Unique identifier (int) or username (str) of the target chat. For your personal cloud (Saved Messages) you can simply use “me” or “self”. For a contact that exists in your Telegram address book you can use his phone number (str).

    Reference:
    Pyrogram Documentation – Delete Messages

    Therefore, you cannot delete messages from a chat with the ID, unless it’s a channel/bot/group – and since you’re receiving this error, I’m assuming you want to delete a chat with a person.

    Now, if you are trying to delete, let’s say, messages with a channel, there are several ways to retrieve the right ID.
    The one I use the most is going to web.telegram and changing it to the "legacy" version.
    Once there, click on the chat id you want to delete messages with. You should see something like this:
    Telegram URL
    you will need the numbers after the "c", and before the underscore.
    So let’s say my number is c1503123456789_1111111111111
    You will use 1503123456789.
    You also need to add -100 to it. So the final number will be:
    -1001503123456789.

    I hope that helps somehow.
    Good luck!

    Login or Signup to reply.
  3. you can delete a dialog with leave_chat method.

    Leave chat or channel

    await app.leave_chat(chat_id)
    

    Leave basic chat and also delete the dialog

    await app.leave_chat(chat_id, delete=True)
    

    Bound method leave of Chat.

    Use as a shortcut for:

    await client.leave_chat(123456789)
    

    Example

    await chat.leave()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search