from telegram.ext.filters import filters from telegram.ext.messagehandler import MessageHandler from telegram import ParseMode from telegram.ext import Updater, CommandHandler, ConversationHandler, CallbackQueryHandler from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ForceReply, ReplyKeyboardMarkup import urllib.request from threading import Thread from random import randint import os def start(update, context): def thread_function(update, context): chat_id = update.effective_chat.id text = ('Hello dear user!') context.bot.send_message(chat_id, text, reply_markup=main_keyboard) thread = Thread(target=thread_function, args=(update,context)) thread.start() def help(update, context): def thread_function(update, context): chat_id = update.effective_chat.id text = ('Hello dear user!') context.bot.send_message(chat_id, text, reply_markup=main_keyboard) thread = Thread(target=thread_function, args=(update,context)) thread.start() # Download URL def download_url(update, context): def thread_function(update, context): chat_id = update.effective_chat.id text = ('πŸ“ Send your file url\n\n' '⚠️ If you dont send your url before 60s the operation will cancel!') context.bot.send_message(chat_id, text, parse_mode=ParseMode.HTML) return 'GET_FILE_NAME' thread = Thread(target=thread_function, args=(update,context)) thread.start() def get_file_name(update, context): def thread_function(update, context): chat_id = update.effective_chat.id context.user_data['url'] = update.message.text try: response = urllib.request.urlopen(urllib.request.Request(context.user_data['url'], method='HEAD')) except: text = ('⁉️ Sorry! Invalid url!') context.bot.send_message(chat_id, text, reply_markup=main_keyboard) return ConversationHandler.END if response.status != 200: text = ('⁉️ Sorry! Unable to download the url!') context.bot.send_message(chat_id, text, reply_markup=main_keyboard) return ConversationHandler.END context.user_data['file_size'] = float(response.headers["Content-Length"]) / 2**20 if context.user_data['file_size'] > 50: text = ('🚫 Sorry! File size is larger than 50MB!') context.bot.send_message(chat_id, text, reply_markup=main_keyboard) return ConversationHandler.END text = ('βœ… All right! Send your new file name\n' 'πŸ’’ If you want to set file name automatically send /None command!\n\n' '⚠️ If you dont send your url before 60s the operation will cancel!') context.bot.send_message(chat_id, text, parse_mode=ParseMode.HTML) return 'UPLOAD_FILE' thread = Thread(target=thread_function, args=(update,context)) thread.start() def upload_file(update, context): def thread_function(update, context): chat_id = update.effective_chat.id context.user_data['file_name'] = update.message.text if update.message.text == '/None': context.user_data['file_name'] = context.user_data['url'].split('/')[-1] else: context.user_data['file_name'] = update.message.text download_dir = path + 'temp/' + context.user_data['file_name'] try: urllib.request.urlretrieve(context.user_data['url'], download_dir) context.bot.send_document(chat_id, document=open(download_dir, 'rb'), reply_markup=main_keyboard) os.remove(download_dir) except: text = ('🚫 Sorry! Try again later!') context.bot.send_message(chat_id, text, reply_markup=main_keyboard) return ConversationHandler.END thread = Thread(target=thread_function, args=(update,context)) thread.start() def timeout_operation(update, context): text = 'Timout ... 😴' update.message.reply_text(text, reply_markup=main_keyboard) return ConversationHandler.END def cancel_operation(update, context): text = '⁉️ Operation Canceled!' update.message.reply_text(text, reply_markup=main_keyboard) return ConversationHandler.END # Rename Files def start_rename_files(update, context): def thread_function(update, context): chat_id = update.effective_chat.id text = ('πŸ“ Send your file with size less than 50MB\n\n' '⚠️ If you dont send your file before 60s the operation will /cancel!') context.bot.send_message(chat_id, text, parse_mode=ParseMode.HTML) return 'GET_FILE' thread = Thread(target=thread_function, args=(update,context)) thread.start() def get_file(update, context): def thread_function(update, context): chat_id = update.effective_chat.id context.user_data['file'] = update.message.document file_name = update.message.document.file_name if update.message.document.file_size > 20 * 2**20: update.message.reply_text('🚫 Sorry! File size larger than 20 MB!') return ConversationHandler.END file_extension = file_name.split('.')[-1] if len(file_extension) > 5: context.user_data['ext'] = 'None' else: context.user_data['ext'] = file_name.split('.')[-1] text = ('πŸ“ Send your new file name\n' f'πŸ’’ If your name has not file extension we will add .{context.user_data["ext"]} to your file name\n\n' '⚠️ If you dont send your file before 60s the operation will /cancel!') context.bot.send_message(chat_id, text, reply_markup=ForceReply(), parse_mode=ParseMode.HTML) return 'GO_TO_RENAME' thread = Thread(target=thread_function, args=(update,context)) thread.start() def rename_file_and_upload(update, context): def thread_function(update, context): chat_id = update.effective_chat.id file_name = update.message.text if file_name.find('.') < 0 and context.user_data['ext']: file_name += ('.' + context.user_data['ext']) with open(f'{path}/temp/{file_name}', 'wb') as f: context.bot.get_file(context.user_data['file']).download(out = f) context.bot.send_document(chat_id, document=open(f'{path}/temp/{file_name}', 'rb'), reply_markup=main_keyboard) os.remove(f'{path}/temp/{file_name}') return ConversationHandler.END thread = Thread(target=thread_function, args=(update,context)) thread.start() # Sync Excel Files def start_excel_sync(update, context): def thread_function(update, context): chat_id = update.effective_chat.id text = ('πŸ“ Send the firts excel file\n\n' '⚠️ If you dont send your file before 60s the operation will /cancel!') context.bot.send_message(chat_id, text, parse_mode=ParseMode.HTML) return 'GET_FIRST_EXCEL' thread = Thread(target=thread_function, args=(update,context)) thread.start() def get_first_excel(update, context): def thread_function(update, context): chat_id = update.effective_chat.id context.user_data['first_excel'] = update.message.document if update.message.document.file_name.split('.')[-1] not in ['xlsx', 'xls']: text = ('🚫 Sorry! You can just send excel files!') context.bot.send_message(chat_id, text, reply_markup=main_keyboard) return ConversationHandler.END text = ('πŸ“ Send the second excel file\n\n' '⚠️ If you dont send your file before 60s the operation will /cancel!') context.bot.send_message(chat_id, text, parse_mode=ParseMode.HTML) return 'GET_SECOND_EXCEL' thread = Thread(target=thread_function, args=(update,context)) thread.start() def get_second_excel(update, context): def thread_function(update, context): chat_id = update.effective_chat.id context.user_data['second_excel'] = update.message.document if update.message.document.file_name.split('.')[-1] not in ['xlsx', 'xls']: text = ('🚫 Sorry! You can just send excel files!') context.bot.send_message(chat_id, text, reply_markup=main_keyboard) return ConversationHandler.END text = ('πŸ“ Send the column name that you want merge on it\n\n' '⚠️ If you dont send your name before 60s the operation will /cancel!') context.bot.send_message(chat_id, text, parse_mode=ParseMode.HTML) return 'UPLOAD_FILE' thread = Thread(target=thread_function, args=(update,context)) thread.start() def merge_and_upload(update, context): def thread_function(update, context): id = randint(1000000000, 9999999999) chat_id = update.effective_chat.id with open(f'{path}/temp/{id}_a.xlsx', 'wb') as f: context.bot.get_file(context.user_data['first_excel']).download(out=f) with open(f'{path}/temp/{id}_b.xlsx', 'wb') as f: context.bot.get_file(context.user_data['second_excel']).download(out=f) if merge_two_excel(id, update.message.document): context.bot.send_document(chat_id, document=open(f'{path}/temp/s_{id}.xlsx', 'rb'), reply_markup=main_keyboard) else: os.remove(f'{path}/temp/{id}_a.xlsx') os.remove(f'{path}/temp/{id}_b.xlsx') text = '⁉️ Ω‘Sorry! Operation has been failed\n\n' context.bot.send_message(chat_id, text, reply_markup=main_keyboard, parse_mode=ParseMode.HTML) return ConversationHandler.END thread = Thread(target=thread_function, args=(update,context)) thread.start() def merge_two_excel(id, column_tag): try: import pandas as pd data_1 = pd.read_excel(f'{path}/temp/{id}_a.xlsx') data_2 = pd.read_excel(f'{path}/temp/{id}_b.xlsx') data = data_1.merge(data_2, on=column_tag, how='outer', suffixes=('_x', '_y')) data = (data.rename(columns = lambda x: x.replace('_x', '')).fillna(data.filter(regex='_y$') .rename(columns = lambda x: x.replace('_y', ''))).filter(regex=r'.*(?