import time, asyncio
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.me & filters.command("clearchat") & ~filters.private)
async def clearchat(app, msg):
start = time.time()
async for x in client.iter_history(message.chat.id, limit=1, reverse=True):
first = x.message_id
chunk = 98
ids = range(first, msg.message_id)
for _ in (ids[i:i+chunk] for i in range(0, len(ids), chunk)):
try:
asyncio.create_task(app.delete_messages(msg.chat.id, _))
except:
pass
end = time.time() - start
vel = len(ids) / end
await msg.edit_text(f"{len(ids)} messages were successfully deleted in {end-start}s.\n{round(vel, 2)}mex/s")
app.run()
首先,如果你想用机器人删除历史记录,你应该保存消息ID。 否则,您可以使用userbot(使用用户帐户)来清除它。 您可以编辑所有聊天信息并获取它们的ID,然后在每次迭代中删除100条信息
警告:由于电报限制,无法使用Bot和BotAPI发送聊天记录。因此,您应该使用mtprotoapi框架,并使用前面提到的用户帐户
首先,执行此操作需要pyrogram库(您也可以使用telethon),并实例化一个客户机,然后您可以使用和关键字添加处理程序或启动客户机。然后通过聊天获取所有消息ID,并将其保存在列表中。最后,使用delete_messages客户端方法删除它们:
启动userbot后,将其添加到组中,并发送“/clearchat”。如果userbot具有删除消息权限,它将开始删除所有消息
有关热解图文件,请参见https://docs.pyrogram.org
(但是,您不应打印终端中的所有消息,以避免服务器过载)
清除控制台的正确代码如下:
如How to clear the interpreter console?所示
附言。 您可以使用相同的代码,将
bot_token=""
参数添加到客户端,并删除iter_历史部分,以便在具有消息ID的情况下使用bot删除消息如果将来您想从组中接收消息并打印它们,但没有收到消息更新,请在组中添加bot作为管理员,或在BotFather中禁用bot隐私模式
为了获得更好的热解图性能,您应该安装
tgcrypto
库相关问题 更多 >
编程相关推荐