如何删除私人聊天(pyrogram)

发布于 2025-02-02 12:53:15 字数 951 浏览 4 评论 0 原文

我尝试了 pyrogram.raw.functions.messages.deletechat ,但要获得以下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 也有相同的问题。

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

我读过此 client.archive_chats 与相同的ID效果很好。

ID就像此 5126101582 是否有其他类型的ID或我的代码是错误的

注: 我需要的就是这样:

”在此处输入图像说明”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

番薯 2025-02-09 12:53:15

更新

好吧,我认为我也找到了一个个人聊天的解决方案!
我当时弄乱了其他东西,然后阅读文档的这一部分,我来了以一种列出每个对话及其各自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()) 

基本上它的作用是循环循环您的所有聊天,并在组/频道的情况下输出其ID和“标题”,而在与某人进行聊天的情况下,它的名字是一个名字。您会注意到,某些ID将在其前面使用连字符( - )输出,而有些ID则不会。
您需要在有或没有连字符的情况下复制该精确的字符串,然后您可以从聊天中删除所有消息:

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())

------------------------------------------------------------------------------------------------- ----- 更新的结尾 -----------------------------------------------------------------------------------------------------》

是否仅仅要删除特定聊天的消息,或者您想删除聊天本身。
无论如何,这就是文档所说的:

chat_id(int | str) - 目标聊天的唯一标识符(int)或用户名(str)。对于您的个人云(保存消息),您可以简单地使用“我”或“自我”。有关电报通讯录中存在的联系人,您可以使用他的电话号码(str)。

参考:
pyrogram文档 - 删除消息

除非是频道/机器人/组 - 并且由于您收到此错误,我假设您想删除与某人的聊天。

现在,如果您试图删除带有频道的消息,则有几种方法可以检索正确的ID。
我使用的最多的是去Web.Telegram并将其更改为“遗产”版本。
到达那里后,单击您要删除消息的聊天ID。您应该看到这样的东西:

您将需要“ C”之后和下划线之前的数字。
因此,假设我的电话是C1503123456789_11111111111
您将使用1503123456789。
您还需要向其添加-100。因此,最终数字将是:
-1001503123456789。

我希望这对某种方式有所帮助。
祝你好运!

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!

無處可尋 2025-02-09 12:53:15

您可以使用 well_chat 方法删除对话框。

留下聊天或频道

await app.leave_chat(chat_id)

离开基本聊天,还删除对话框

await app.leave_chat(chat_id, delete=True)

的聊天方法。

用作捷径:

await client.leave_chat(123456789)

示例

await chat.leave()

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()
生活了然无味 2025-02-09 12:53:15

精美的

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

作品

fine

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

works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文