From a4761d523ba5107822d475a9010138b6ab08b60b Mon Sep 17 00:00:00 2001 From: BennyThink Date: Tue, 4 May 2021 15:27:11 +0800 Subject: [PATCH] add flood limit, add hachoir --- bot.py | 30 +++++++++++++++++++++++++++--- requirements.txt | 9 ++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index e6da25a..e2f94e4 100644 --- a/bot.py +++ b/bot.py @@ -14,6 +14,7 @@ import threading import asyncio import traceback +import fakeredis import youtube_dl from telethon import TelegramClient, events from tgbot_ping import get_runtime @@ -23,16 +24,29 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(filename)s [%(le token = os.getenv("TOKEN") or "17Zg" app_id = int(os.getenv("APP_ID") or "922") app_hash = os.getenv("APP_HASH") or "490" + bot = TelegramClient('bot', app_id, app_hash).start(bot_token=token) +r = fakeredis.FakeStrictRedis() + +EXPIRE = 5 + async def upload_callback(current, total, chat_id, message): - msg = f'Uploading {round(current / total * 100, 2)}%: {current}/{total}' - await bot.edit_message(chat_id, message, msg) + key = f"{chat_id}-{message.id}" + # if the key exists, we shouldn't send edit message + if not r.exists(key): + msg = f'Uploading {round(current / total * 100, 2)}%: {current}/{total}' + await bot.edit_message(chat_id, message, msg) + r.set(key, "ok", ex=EXPIRE) async def sync_edit_message(chat_id, message, msg): - await bot.edit_message(chat_id, message, msg) + # try to avoid flood + key = f"{chat_id}-{message.id}" + if not r.exists(key): + await bot.edit_message(chat_id, message, msg) + r.set(key, "ok", ex=EXPIRE) def go(chat_id, message, msg): @@ -79,6 +93,15 @@ async def send_welcome(event): raise events.StopPropagation +@bot.on(events.NewMessage(pattern='/help')) +async def send_welcome(event): + async with bot.action(event.chat_id, 'typing'): + await bot.send_message(event.chat_id, "Bot is not working? " + "Wait a few seconds, send your link again or report bugs at " + "https://github.com/tgbot-collection/ytdl-bot/issues") + raise events.StopPropagation + + @bot.on(events.NewMessage(pattern='/ping')) async def send_welcome(event): async with bot.action(event.chat_id, 'typing'): @@ -122,4 +145,5 @@ async def echo_all(event): if __name__ == '__main__': + bot.start() bot.run_until_disconnected() diff --git a/requirements.txt b/requirements.txt index ad0e984..b29b78c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,7 @@ -telethon -youtube-dl tgbot-ping -cryptg \ No newline at end of file +cryptg +fakeredis + +telethon==1.21.1 +youtube-dl==2021.4.26 +hachoir==3.1.2 \ No newline at end of file