switch to context based callbacks

This commit is contained in:
jaskaranSM 2020-04-16 12:13:08 +05:30
parent 802fefdfe4
commit d7bc8f3c9a
10 changed files with 49 additions and 48 deletions

View File

@ -103,6 +103,6 @@ try:
except KeyError: except KeyError:
USE_SERVICE_ACCOUNTS = False USE_SERVICE_ACCOUNTS = False
updater = tg.Updater(token=BOT_TOKEN) updater = tg.Updater(token=BOT_TOKEN,use_context=True)
bot = updater.bot bot = updater.bot
dispatcher = updater.dispatcher dispatcher = updater.dispatcher

View File

@ -13,7 +13,7 @@ from .modules import authorize, list, cancel_mirror, mirror_status, mirror, clon
@run_async @run_async
def stats(bot, update): def stats(update,context):
currentTime = get_readable_time((time.time() - botStartTime)) currentTime = get_readable_time((time.time() - botStartTime))
total, used, free = shutil.disk_usage('.') total, used, free = shutil.disk_usage('.')
total = get_readable_file_size(total) total = get_readable_file_size(total)
@ -21,32 +21,32 @@ def stats(bot, update):
free = get_readable_file_size(free) free = get_readable_file_size(free)
stats = f'Bot Uptime: {currentTime}\n' \ stats = f'Bot Uptime: {currentTime}\n' \
f'Total disk space: {total}\n' \ f'Total disk space: {total}\n' \
f'Used: {used}\n' \ f'Used: {used}\n' \
f'Free: {free}' f'Free: {free}'
sendMessage(stats, bot, update) sendMessage(stats, context.bot, update)
@run_async @run_async
def start(bot, update): def start(update,context):
sendMessage("This is a bot which can mirror all your links to Google drive!\n" sendMessage("This is a bot which can mirror all your links to Google drive!\n"
"Type /help to get a list of available commands", bot, update) "Type /help to get a list of available commands", context.bot, update)
@run_async @run_async
def ping(bot, update): def ping(update, context):
start_time = int(round(time.time() * 1000)) start_time = int(round(time.time() * 1000))
reply = sendMessage("Starting Ping", bot, update) reply = sendMessage("Starting Ping", context.bot, update)
end_time = int(round(time.time() * 1000)) end_time = int(round(time.time() * 1000))
editMessage(f'{end_time - start_time} ms', reply) editMessage(f'{end_time - start_time} ms', reply)
@run_async @run_async
def log(bot, update): def log(update, context):
sendLogFile(bot, update) sendLogFile(context.bot, update)
@run_async @run_async
def bot_help(bot, update): def bot_help(update, context):
help_string = f''' help_string = f'''
/{BotCommands.HelpCommand}: To get this message /{BotCommands.HelpCommand}: To get this message
@ -71,7 +71,7 @@ def bot_help(bot, update):
/{BotCommands.LogCommand}: Get a log file of the bot. Handy for getting crash reports /{BotCommands.LogCommand}: Get a log file of the bot. Handy for getting crash reports
''' '''
sendMessage(help_string, bot, update) sendMessage(help_string, context.bot, update)
def main(): def main():

View File

@ -175,6 +175,8 @@ class GoogleDriveHelper:
return download_url return download_url
def upload(self, file_name: str): def upload(self, file_name: str):
if USE_SERVICE_ACCOUNTS:
self.service_account_count = len(os.listdir("accounts"))
self.__listener.onUploadStarted() self.__listener.onUploadStarted()
file_dir = f"{DOWNLOAD_DIR}{self.__listener.message.message_id}" file_dir = f"{DOWNLOAD_DIR}{self.__listener.message.message_id}"
file_path = f"{file_dir}/{file_name}" file_path = f"{file_dir}/{file_name}"

View File

@ -9,7 +9,7 @@ from bot.helper.telegram_helper.bot_commands import BotCommands
@run_async @run_async
def authorize(bot, update): def authorize(update,context):
reply_message = update.message.reply_to_message reply_message = update.message.reply_to_message
msg = '' msg = ''
with open('authorized_chats.txt', 'a') as file: with open('authorized_chats.txt', 'a') as file:
@ -31,11 +31,11 @@ def authorize(bot, update):
msg = 'Person Authorized to use the bot!' msg = 'Person Authorized to use the bot!'
else: else:
msg = 'Person already authorized' msg = 'Person already authorized'
sendMessage(msg, bot, update) sendMessage(msg, context.bot, update)
@run_async @run_async
def unauthorize(bot,update): def unauthorize(update,context):
reply_message = update.message.reply_to_message reply_message = update.message.reply_to_message
if reply_message is None: if reply_message is None:
# Trying to unauthorize a chat # Trying to unauthorize a chat
@ -57,7 +57,7 @@ def unauthorize(bot,update):
file.truncate(0) file.truncate(0)
for i in AUTHORIZED_CHATS: for i in AUTHORIZED_CHATS:
file.write(f'{i}\n') file.write(f'{i}\n')
sendMessage(msg, bot, update) sendMessage(msg, context.bot, update)
authorize_handler = CommandHandler(command=BotCommands.AuthorizeCommand, callback=authorize, authorize_handler = CommandHandler(command=BotCommands.AuthorizeCommand, callback=authorize,

View File

@ -11,14 +11,14 @@ from bot.helper.ext_utils.bot_utils import getDownloadByGid, MirrorStatus
@run_async @run_async
def cancel_mirror(bot, update): def cancel_mirror(update,context):
args = update.message.text.split(" ", maxsplit=1) args = update.message.text.split(" ",maxsplit=1)
mirror_message = None mirror_message = None
if len(args) > 1: if len(args) > 1:
gid = args[1] gid = args[1]
dl = getDownloadByGid(gid) dl = getDownloadByGid(gid)
if not dl: if not dl:
sendMessage(f"GID: <code>{gid}</code> not found.", bot, update) sendMessage(f"GID: <code>{gid}</code> not found.",context.bot,update)
return return
with download_dict_lock: with download_dict_lock:
keys = list(download_dict.keys()) keys = list(download_dict.keys())
@ -33,18 +33,17 @@ def cancel_mirror(bot, update):
if BotCommands.MirrorCommand in mirror_message.text or \ if BotCommands.MirrorCommand in mirror_message.text or \
BotCommands.TarMirrorCommand in mirror_message.text: BotCommands.TarMirrorCommand in mirror_message.text:
msg = "Mirror already have been cancelled" msg = "Mirror already have been cancelled"
sendMessage(msg, bot, update) sendMessage(msg,context.bot,update)
return return
else: else:
msg = "Please reply to the /mirror message which was " \ msg = "Please reply to the /mirror message which was used to start the download or /cancel gid to cancel it!"
"used to start the download or /cancel gid to cancel it!" sendMessage(msg,context.bot,update)
sendMessage(msg, bot, update)
return return
if dl.status() == MirrorStatus.STATUS_UPLOADING: if dl.status() == "Uploading":
sendMessage("Upload in Progress, Don't Cancel it.", bot, update) sendMessage("Upload in Progress, Don't Cancel it.", context.bot, update)
return return
elif dl.status() == MirrorStatus.STATUS_ARCHIVING: elif dl.status() == "Archiving":
sendMessage("Archival in Progress, Don't Cancel it.", bot, update) sendMessage("Archival in Progress, Don't Cancel it.", context.bot, update)
return return
else: else:
dl.download().cancel_download() dl.download().cancel_download()
@ -53,7 +52,7 @@ def cancel_mirror(bot, update):
@run_async @run_async
def cancel_all(update, bot): def cancel_all(update, context):
with download_dict_lock: with download_dict_lock:
count = 0 count = 0
for dlDetails in list(download_dict.values()): for dlDetails in list(download_dict.values()):
@ -62,7 +61,7 @@ def cancel_all(update, bot):
dlDetails.download().cancel_download() dlDetails.download().cancel_download()
count += 1 count += 1
delete_all_messages() delete_all_messages()
sendMessage(f'Cancelled {count} downloads!', update, bot) sendMessage(f'Cancelled {count} downloads!', context.bot,update)
cancel_mirror_handler = CommandHandler(BotCommands.CancelMirror, cancel_mirror, cancel_mirror_handler = CommandHandler(BotCommands.CancelMirror, cancel_mirror,

View File

@ -7,15 +7,15 @@ from bot import dispatcher
@run_async @run_async
def cloneNode(bot,update): def cloneNode(update,context):
args = update.message.text.split(" ",maxsplit=1) args = update.message.text.split(" ",maxsplit=1)
if len(args) > 1: if len(args) > 1:
link = args[1] link = args[1]
msg = sendMessage(f"Cloning: <code>{link}</code>",bot,update) msg = sendMessage(f"Cloning: <code>{link}</code>",context.bot,update)
gd = GoogleDriveHelper() gd = GoogleDriveHelper()
result = gd.clone(link) result = gd.clone(link)
deleteMessage(bot,msg) deleteMessage(context.bot,msg)
sendMessage(result,bot,update) sendMessage(result,context.bot,update)
else: else:
sendMessage("Provide G-Drive Shareable Link to Clone.",bot,update) sendMessage("Provide G-Drive Shareable Link to Clone.",bot,update)

View File

@ -7,18 +7,18 @@ import threading
from bot.helper.telegram_helper.bot_commands import BotCommands from bot.helper.telegram_helper.bot_commands import BotCommands
@run_async @run_async
def list_drive(bot,update): def list_drive(update,context):
message = update.message.text message = update.message.text
search = message.split(' ',maxsplit=1)[1] search = message.split(' ',maxsplit=1)[1]
LOGGER.info(f"Searching: {search}") LOGGER.info(f"Searching: {search}")
gdrive = GoogleDriveHelper(None) gdrive = GoogleDriveHelper(None)
msg = gdrive.drive_list(search) msg = gdrive.drive_list(search)
if msg: if msg:
reply_message = sendMessage(msg, bot, update) reply_message = sendMessage(msg, context.bot, update)
else: else:
reply_message = sendMessage('No result found', bot, update) reply_message = sendMessage('No result found', context.bot, update)
threading.Thread(target=auto_delete_message, args=(bot, update.message, reply_message)).start() threading.Thread(target=auto_delete_message, args=(context.bot, update.message, reply_message)).start()
list_handler = CommandHandler(BotCommands.ListCommand, list_drive,filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) list_handler = CommandHandler(BotCommands.ListCommand, list_drive,filters=CustomFilters.authorized_chat | CustomFilters.authorized_user)

View File

@ -194,13 +194,13 @@ def _mirror(bot, update, isTar=False):
@run_async @run_async
def mirror(bot, update): def mirror(update, context):
_mirror(bot, update) _mirror(context.bot, update)
@run_async @run_async
def tar_mirror(update, bot): def tar_mirror(update, context):
_mirror(update, bot, True) _mirror(context.bot, update, True)
mirror_handler = CommandHandler(BotCommands.MirrorCommand, mirror, mirror_handler = CommandHandler(BotCommands.MirrorCommand, mirror,

View File

@ -9,11 +9,11 @@ from bot.helper.telegram_helper.bot_commands import BotCommands
import threading import threading
@run_async @run_async
def mirror_status(bot,update): def mirror_status(update,context):
message = get_readable_message() message = get_readable_message()
if len(message) == 0: if len(message) == 0:
message = "No active downloads" message = "No active downloads"
reply_message = sendMessage(message, bot, update) reply_message = sendMessage(message, context.bot, update)
threading.Thread(target=auto_delete_message, args=(bot, update.message, reply_message)).start() threading.Thread(target=auto_delete_message, args=(bot, update.message, reply_message)).start()
return return
index = update.effective_chat.id index = update.effective_chat.id
@ -21,8 +21,8 @@ def mirror_status(bot,update):
if index in status_reply_dict.keys(): if index in status_reply_dict.keys():
deleteMessage(bot, status_reply_dict[index]) deleteMessage(bot, status_reply_dict[index])
del status_reply_dict[index] del status_reply_dict[index]
sendStatusMessage(update,bot) sendStatusMessage(update,context.bot)
deleteMessage(bot,update.message) deleteMessage(context.bot,update.message)
mirror_status_handler = CommandHandler(BotCommands.StatusCommand, mirror_status, mirror_status_handler = CommandHandler(BotCommands.StatusCommand, mirror_status,

View File

@ -1,5 +1,5 @@
requests requests
python-telegram-bot==12.2.0 python-telegram-bot==12.6.1
google-api-python-client>=1.7.11,<1.7.20 google-api-python-client>=1.7.11,<1.7.20
google-auth-httplib2>=0.0.3,<0.1.0 google-auth-httplib2>=0.0.3,<0.1.0
google-auth-oauthlib>=0.4.1,<0.10.0 google-auth-oauthlib>=0.4.1,<0.10.0
@ -10,4 +10,4 @@ python-magic
beautifulsoup4>=4.8.2,<4.8.10 beautifulsoup4>=4.8.2,<4.8.10
Pyrogram>=0.16.0,<0.16.10 Pyrogram>=0.16.0,<0.16.10
TgCrypto>=1.1.1,<1.1.10 TgCrypto>=1.1.1,<1.1.10
youtube-dl youtube-dl