Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用电报机器人删除所有聊天记录?_Python_Python 3.x_Telegram_Telegram Bot_Python Telegram Bot - Fatal编程技术网

Python 如何使用电报机器人删除所有聊天记录?

Python 如何使用电报机器人删除所有聊天记录?,python,python-3.x,telegram,telegram-bot,python-telegram-bot,Python,Python 3.x,Telegram,Telegram Bot,Python Telegram Bot,是否可以删除我与bot聊天的所有聊天历史记录(消息) 因此,控制台版本可能类似于: import os os.sys("clear") - if Linux os.sys("cls") - if Windows 我只想用bot删除聊天中的所有消息 def deleteChat(message): #delete chat code 首先,如果你想用机器人删除历史记录,你应该保存消息ID。 否则,您可以使用userbot(使用用户帐户)清除历史记

是否可以删除我与bot聊天的所有聊天历史记录(消息)

因此,控制台版本可能类似于:

import os
os.sys("clear") - if Linux
os.sys("cls") - if Windows
我只想用bot删除聊天中的所有消息

def deleteChat(message):
    #delete chat code

首先,如果你想用机器人删除历史记录,你应该保存消息ID。 否则,您可以使用userbot(使用用户帐户)清除历史记录。 你可以输入所有聊天信息并获取它们的ID,然后一次删除100条

警告:由于电报限制,无法使用Bot和BotAPI发送聊天记录。因此,您应该使用mtprotoapi框架,并使用前面提到的用户帐户

首先,执行此操作需要库(也可以使用),并实例化一个客户机,然后可以添加一个处理程序或使用with关键字启动客户机。然后通过聊天获取所有消息ID,并将其保存在列表中。最后,使用delete_messages客户端方法删除它们:

from pyrogram import Client, filters

app = Client(
    "filename", # Will create a file named filename.session which will contain userbot "cache"
    # You could also change "filename" to ":memory:" for better performance as it will write userbot session in ram
    api_id=0, # You can get api_hash and api_id by creating an app on
    api_hash="", # my.telegram.org/apps (needed if you use MTProto instead of BotAPI)
)


@app.on_message(filters.group & filters.command("delall"))
def func(app, message):
    ids = [msg.message_id for msg in app.iter_history(message.chat.id)]
    chunk_size = 100

    # Splitting the list in chunks of 100 messages ids
    ids = [ids[i:i + chunk_size] for i in range(0, len(ids), chunk_size)]

    for m_ids in ids:
        try:
            app.delete_messages(
                message.chat.id,
                m_ids
            )
        except Exception as e:
            app.send_message(
                message.chat.id,
                f"An error occurred: {e}"
            )
            break # Excepts errors given by Telegram, like floodwaits

app.run()
启动userbot后,将其添加到组中,并发送“/delall”。如果userbot具有删除消息权限,它将开始删除所有消息

有关热解图文档,请参阅


(但是,您不应打印终端中的所有消息,以避免VPS过载)

清除控制台的正确代码如下:

import os

def clear():
    os.system('cls' if os.name=='nt' else 'clear')
如图所示

附言。 您可以使用相同的代码,将bot_token=“”参数添加到客户端,并删除iter_历史记录部分,以便在具有消息ID的情况下使用bot删除消息

如果将来您希望从组接收消息并打印它们,但未收到消息更新,请在组中添加bot作为管理员,或在中禁用bot隐私模式

为了获得更好的pyrogram性能,您应该安装tgcrypto库并使用(将来,pyrogram默认为异步。此代码适用于0.18.x版本)

这是许多删除聊天记录的方法之一,我写这篇文章是为了保持简单