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:
USE_SERVICE_ACCOUNTS = False
updater = tg.Updater(token=BOT_TOKEN)
updater = tg.Updater(token=BOT_TOKEN,use_context=True)
bot = updater.bot
dispatcher = updater.dispatcher

View File

@ -13,7 +13,7 @@ from .modules import authorize, list, cancel_mirror, mirror_status, mirror, clon
@run_async
def stats(bot, update):
def stats(update,context):
currentTime = get_readable_time((time.time() - botStartTime))
total, used, free = shutil.disk_usage('.')
total = get_readable_file_size(total)
@ -21,32 +21,32 @@ def stats(bot, update):
free = get_readable_file_size(free)
stats = f'Bot Uptime: {currentTime}\n' \
f'Total disk space: {total}\n' \
f'Used: {used}\n' \
f'Free: {free}'
sendMessage(stats, bot, update)
f'Used: {used}\n' \
f'Free: {free}'
sendMessage(stats, context.bot, update)
@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"
"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
def ping(bot, update):
def ping(update, context):
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))
editMessage(f'{end_time - start_time} ms', reply)
@run_async
def log(bot, update):
sendLogFile(bot, update)
def log(update, context):
sendLogFile(context.bot, update)
@run_async
def bot_help(bot, update):
def bot_help(update, context):
help_string = f'''
/{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
'''
sendMessage(help_string, bot, update)
sendMessage(help_string, context.bot, update)
def main():

View File

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

View File

@ -9,7 +9,7 @@ from bot.helper.telegram_helper.bot_commands import BotCommands
@run_async
def authorize(bot, update):
def authorize(update,context):
reply_message = update.message.reply_to_message
msg = ''
with open('authorized_chats.txt', 'a') as file:
@ -31,11 +31,11 @@ def authorize(bot, update):
msg = 'Person Authorized to use the bot!'
else:
msg = 'Person already authorized'
sendMessage(msg, bot, update)
sendMessage(msg, context.bot, update)
@run_async
def unauthorize(bot,update):
def unauthorize(update,context):
reply_message = update.message.reply_to_message
if reply_message is None:
# Trying to unauthorize a chat
@ -57,7 +57,7 @@ def unauthorize(bot,update):
file.truncate(0)
for i in AUTHORIZED_CHATS:
file.write(f'{i}\n')
sendMessage(msg, bot, update)
sendMessage(msg, context.bot, update)
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
def cancel_mirror(bot, update):
args = update.message.text.split(" ", maxsplit=1)
def cancel_mirror(update,context):
args = update.message.text.split(" ",maxsplit=1)
mirror_message = None
if len(args) > 1:
gid = args[1]
dl = getDownloadByGid(gid)
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
with download_dict_lock:
keys = list(download_dict.keys())
@ -33,18 +33,17 @@ def cancel_mirror(bot, update):
if BotCommands.MirrorCommand in mirror_message.text or \
BotCommands.TarMirrorCommand in mirror_message.text:
msg = "Mirror already have been cancelled"
sendMessage(msg, bot, update)
sendMessage(msg,context.bot,update)
return
else:
msg = "Please reply to the /mirror message which was " \
"used to start the download or /cancel gid to cancel it!"
sendMessage(msg, bot, update)
msg = "Please reply to the /mirror message which was used to start the download or /cancel gid to cancel it!"
sendMessage(msg,context.bot,update)
return
if dl.status() == MirrorStatus.STATUS_UPLOADING:
sendMessage("Upload in Progress, Don't Cancel it.", bot, update)
if dl.status() == "Uploading":
sendMessage("Upload in Progress, Don't Cancel it.", context.bot, update)
return
elif dl.status() == MirrorStatus.STATUS_ARCHIVING:
sendMessage("Archival in Progress, Don't Cancel it.", bot, update)
elif dl.status() == "Archiving":
sendMessage("Archival in Progress, Don't Cancel it.", context.bot, update)
return
else:
dl.download().cancel_download()
@ -53,7 +52,7 @@ def cancel_mirror(bot, update):
@run_async
def cancel_all(update, bot):
def cancel_all(update, context):
with download_dict_lock:
count = 0
for dlDetails in list(download_dict.values()):
@ -62,7 +61,7 @@ def cancel_all(update, bot):
dlDetails.download().cancel_download()
count += 1
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,

View File

@ -7,15 +7,15 @@ from bot import dispatcher
@run_async
def cloneNode(bot,update):
def cloneNode(update,context):
args = update.message.text.split(" ",maxsplit=1)
if len(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()
result = gd.clone(link)
deleteMessage(bot,msg)
sendMessage(result,bot,update)
deleteMessage(context.bot,msg)
sendMessage(result,context.bot,update)
else:
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
@run_async
def list_drive(bot,update):
def list_drive(update,context):
message = update.message.text
search = message.split(' ',maxsplit=1)[1]
LOGGER.info(f"Searching: {search}")
gdrive = GoogleDriveHelper(None)
msg = gdrive.drive_list(search)
if msg:
reply_message = sendMessage(msg, bot, update)
reply_message = sendMessage(msg, context.bot, update)
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)

View File

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

View File

@ -9,11 +9,11 @@ from bot.helper.telegram_helper.bot_commands import BotCommands
import threading
@run_async
def mirror_status(bot,update):
def mirror_status(update,context):
message = get_readable_message()
if len(message) == 0:
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()
return
index = update.effective_chat.id
@ -21,8 +21,8 @@ def mirror_status(bot,update):
if index in status_reply_dict.keys():
deleteMessage(bot, status_reply_dict[index])
del status_reply_dict[index]
sendStatusMessage(update,bot)
deleteMessage(bot,update.message)
sendStatusMessage(update,context.bot)
deleteMessage(context.bot,update.message)
mirror_status_handler = CommandHandler(BotCommands.StatusCommand, mirror_status,

View File

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