async def delall(bot: Bot, update): """ A Funtion To Handle Incoming /delall Command TO Disconnect All Chats From A Group """ chat_id = update.chat.id user_id = update.from_user.id if update.from_user else None global VERIFY if VERIFY.get(str(chat_id)) == None: # Make Admin's ID List admin_list = [] async for x in bot.iter_chat_members(chat_id=chat_id, filter="administrators"): admin_id = x.user.id admin_list.append(admin_id) admin_list.append(None) VERIFY[str(chat_id)] = admin_list if not user_id in VERIFY.get(str(chat_id)): return await db.delete_all(chat_id) await recacher(chat_id, True, True, bot, update) await update.reply_text( "Sucessfully Deleted All Connected Chats From This Group....")
async def settings(bot, update): if update.from_user.id not in OWNER_ID: await bot.send_message(chat_id=update.chat.id, text="""<b>Hey {}!!</b> <i>Am Just An Advance Auto Filter Bot....π Only For <b><u><a href="https://t.me/joinchat/TRlZZilyh-MVa66t">Universal Film Studio Group</a></u></b></i>""" .format(update.from_user.first_name), parse_mode="html", reply_to_message_id=update.message_id) return chat_id = update.chat.id user_id = update.from_user.id if update.from_user else None global VERIFY if VERIFY.get(str(chat_id)) == None: # Make Admin's ID List admin_list = [631110062] #Jins Has Changed This Code For Privacy 06th May 2021 #async for x in bot.iter_chat_members(chat_id=chat_id, filter="administrators"): #admin_id = x.user.id #admin_list.append(admin_id) #admin_list.append(None) ####################################################### VERIFY[str(chat_id)] = admin_list if not user_id in VERIFY.get( str(chat_id)): # Checks if user is admin of the chat return bot_info = await bot.get_me() bot_first_name = bot_info.first_name text = f"<i>{bot_first_name}'s</i> Settings Pannel.....\n" text += f"\n<i>You Can Use This Menu To Change Connectivity And Know Status Of Your Every Connected Channel, Change Filter Types, Configure Filter Results And To Know Status Of Your Group...</i>" buttons = [[ InlineKeyboardButton("Channels", callback_data=f"channel_list({chat_id})"), InlineKeyboardButton("Filter Types", callback_data=f"types({chat_id})") ], [ InlineKeyboardButton("Configure π ", callback_data=f"config({chat_id})") ], [ InlineKeyboardButton("Status", callback_data=f"status({chat_id})"), InlineKeyboardButton("About", callback_data=f"about({chat_id})") ], [InlineKeyboardButton("Close π", callback_data="close")]] reply_markup = InlineKeyboardMarkup(buttons) await bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup, parse_mode="html", reply_to_message_id=update.message_id)
async def cb_status(bot, update: CallbackQuery): """ A Callback Funtion For Showing Overall Status Of A Group """ global VERIFY query_data = update.data chat_id = update.message.chat.id chat_name = remove_emoji(update.message.chat.title) user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return chat_id = re.findall(r"status\((.+)\)", query_data)[0] total_filters, total_chats, total_achats = await db.status(chat_id) text = f"<b><i>Status Of {chat_name}</i></b>\n" text += f"\n<b>Total Connected Chats:</b> <code>{total_chats}</code>\n" text += f"\n<b>Total Active Chats:</b> <code>{total_achats}</code>\n" text += f"\n<b>Total Filters:</b> <code>{total_filters}</code>" buttons = [[ InlineKeyboardButton("π Back", callback_data="settings"), InlineKeyboardButton("Close π", callback_data="close") ]] reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_about(bot, update: CallbackQuery): """ A Callback Funtion For Showing About Section In Bot Setting Menu """ global VERIFY chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return text = f"<i><u>Bot's Status</u></i>\n" text += f"\n<b><i>Bot's Uptime:</i></b> <code>{time_formatter(time.time() - start_uptime)}</code>\n" text += f"\n<b><i>Bot Funtion:</i></b> <i>Auto Filter Files</i>\n" text += f"""\n<b><i>Bot Support:</i></b> <a href="https://t.me/CrazyBotszGrp">@CrazyBotszGrp</a>\n""" text += """\n<b><i>Source Code:</i></b> <a href="https://github.com/AlbertEinsteinTG/Adv-Filter-Bot-V2">Source</a>""" buttons = [[ InlineKeyboardButton("My Dev β‘", url="https://t.me/AlbertEinstein_TG"), InlineKeyboardButton("π Back", callback_data="settings") ], [InlineKeyboardButton("Close π", callback_data="close")]] reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_filters_delete(bot, update: CallbackQuery): """ A Callback Funtion For Delete A Specific Channel's Filters Connected To A Group """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return channel_id, channel_name = re.findall(r"f_delete\((.+)\)", query_data)[0].split("|", 1) f_delete = await db.del_filters(chat_id, int(channel_id)) if not f_delete: text = "<b><i>Oops..!!</i></b>\n\nEncountered Some Error While Deleteing Filters....\nPlease Check The Logs...." await update.answer(text=text, show_alert=True) return text = f"All Filters Of <code>{channel_id}[{channel_name}]</code> Has Been Deleted Sucessfully From My DB.." buttons = [[ InlineKeyboardButton("Back", callback_data="settings"), InlineKeyboardButton("Close", callback_data="close") ]] reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_disconnect(bot, update: CallbackQuery): """ A Callback Funtion Helping The user To Make A Chat inactive Chat Which Will Make The Bot To Avoid Fetching Results From This Channel """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return channel_id, channel_name = re.findall(r"connect\((.+)\)", query_data)[0].split("|", 1) f_count = await db.cf_count(chat_id, int(channel_id)) remove_active = await db.del_active(chat_id, int(channel_id)) if not remove_active: await update.answer( "Couldnt Full Fill YOur Request...\n Report This @CrazyBotszGrp Along With Bot's Log", show_alert=True) return text = f"<i>Sucessfully Disconnected From</i> <code>{channel_name}</code>\n" text += f"\n<i>Info About <b>{channel_name}</b></i>\n" text += f"\n<i>Channel Name:</i> <code>{channel_name}</code>\n" text += f"\n<i>Channel ID:</i> <code>{channel_id}</code>\n" text += f"\n<i>Channel Files:</i> <code>{f_count}</code>\n" text += f"\n<i>Current Status:</i> <code>Disconnected</code>\n" buttons = [[ InlineKeyboardButton( "π Connect π ", callback_data=f"warn({channel_id}|{channel_name}|connect)"), InlineKeyboardButton( "Delete β", callback_data=f"warn({channel_id}|{channel_name}|c_delete)") ]] buttons.append([ InlineKeyboardButton( "Delete Filters β ", callback_data=f"warn({channel_id}|{channel_name}|f_delete)") ]) buttons.append([ InlineKeyboardButton("π Back", callback_data=f"channel_list({chat_id})") ]) reply_markup = InlineKeyboardMarkup(buttons) await recacher(chat_id, False, True, bot, update) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_connect(bot, update: CallbackQuery): """ A Callback Funtion Helping The user To Make A Chat Active Chat Which Will Make The Bot To Fetch Results From This Channel Too """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return channel_id, channel_name = re.findall(r"connect\((.+)\)", query_data)[0].split("|", 1) channel_id = int(channel_id) f_count = await db.cf_count(chat_id, channel_id) add_active = await db.update_active(chat_id, channel_id, channel_name) if not add_active: await update.answer(f"{channel_name} Is Aldready in Active Connection", show_alert=True) return text = f"<i>Sucessfully Connected To</i> <code>{channel_name}</code>\n" text += f"\n<i>Info About <b>{channel_name}</b></i>\n" text += f"\n<i>Channel Name:</i> <code>{channel_name}</code>\n" text += f"\n<i>Channel ID:</i> <code>{channel_id}</code>\n" text += f"\n<i>Channel Files:</i> <code>{f_count}</code>\n" text += f"\n<i>Current Status:</i> <code>Connected</code>\n" buttons = [[ InlineKeyboardButton( "π¨ Disconnect π¨", callback_data=f"warn({channel_id}|{channel_name}|disconnect)"), InlineKeyboardButton( "Delete β", callback_data=f"warn({channel_id}|{channel_name}|c_delete)") ]] buttons.append([ InlineKeyboardButton( "Delete Filters β ", callback_data=f"warn({channel_id}|{channel_name}|f_delete)") ]) buttons.append([ InlineKeyboardButton("π Back", callback_data=f"channel_list({chat_id})") ]) await recacher(chat_id, False, True, bot, update) reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_max_results(bot, update: CallbackQuery): """ A Callback Funtion For Changing The Count Of Maximum Files TO Be Fetched From Database """ global VERIFY query_data = update.data chat_id = update.message.chat.id chat_name = remove_emoji(update.message.chat.title) user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return count, chat_id = re.findall(r"mf_count\((.+)\)", query_data)[0].split("|", 1) text = f"<i>Choose Your Desired 'Max Filter' To Be Fetched From DB For Every Filter Results Shown In</i> <code>{chat_name}</code>" buttons = [[ InlineKeyboardButton( "50 Results", callback_data=f"set(results|50|{chat_id}|{count})") ], [ InlineKeyboardButton( "100 Results", callback_data=f"set(results|100|{chat_id}|{count})") ], [ InlineKeyboardButton( "150 Results", callback_data=f"set(results|150|{chat_id}|{count})") ], [ InlineKeyboardButton( "200 Results", callback_data=f"set(results|200|{chat_id}|{count})") ], [ InlineKeyboardButton( "250 Results", callback_data=f"set(results|250|{chat_id}|{count})") ], [ InlineKeyboardButton( "300 Results", callback_data=f"set(results|300|{chat_id}|{count})") ], [ InlineKeyboardButton("π Back", callback_data=f"config({chat_id})") ]] reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_max_buttons(bot, update: CallbackQuery): """ A Callback Funtion For Changing The Count Of Result To Be Shown Per Page """ global VERIFY query_data = update.data chat_id = update.message.chat.id chat_name = remove_emoji(update.message.chat.title) user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return count, chat_id = re.findall(r"mr_count\((.+)\)", query_data)[0].split("|", 1) text = f"<i>Choose Your Desired 'Max Filter Count Per Page' For Every Filter Results Shown In</i> <code>{chat_name}</code>" buttons = [[ InlineKeyboardButton( "5 Filters", callback_data=f"set(per_page|5|{chat_id}|{count})") ], [ InlineKeyboardButton( "10 Filters", callback_data=f"set(per_page|10|{chat_id}|{count})") ], [ InlineKeyboardButton( "15 Filters", callback_data=f"set(per_page|15|{chat_id}|{count})") ], [ InlineKeyboardButton( "20 Filters", callback_data=f"set(per_page|20|{chat_id}|{count})") ], [ InlineKeyboardButton( "25 Filters", callback_data=f"set(per_page|25|{chat_id}|{count})") ], [ InlineKeyboardButton( "30 Filters", callback_data=f"set(per_page|30|{chat_id}|{count})") ], [ InlineKeyboardButton("π Back", callback_data=f"config({chat_id})") ]] reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_warn(bot, update: CallbackQuery): """ A Callback Funtion For Acknowledging User's About What Are They Upto """ global VERIFY query_data = update.data chat_id = update.message.chat.id chat_name = remove_emoji(update.message.chat.title) chat_name = chat_name.encode('ascii', 'ignore').decode('ascii')[:35] user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return channel_id, channel_name, action = re.findall(r"warn\((.+)\)", query_data)[0].split("|", 2) if action == "connect": text = f"<i>Are You Sure You Want To Enable Connection With</i> <code>{channel_name}</code><i>..???</i>\n" text += f"\n<i>This Will Show File Links From</i> <code>{channel_name}</code> <i>While Showing Results</i>..." elif action == "disconnect": text = f"<i>Are You Sure You Want To Disable</i> <code>{channel_name}</code> <i>Connection With The Group???....</i>\n" text += f"\n<i>The DB Files Will Still Be There And You Can Connect Back To This Channel Anytime From Settings Menu Without Adding Files To DB Again...</i>\n" text += f"\n<i>This Disabling Just Hide Results From The Filter Results...</i>" elif action == "c_delete": text = f"<i>Are You Sure You Want To Disconnect</i> <code>{channel_name}</code> <i>From This Group??</i>\n" text += f"\n<i><b>This Will Delete Channel And All Its Files From DB Too....!!</b></i>\n" text += f"\nYou Need To Add Channel Again If You Need To Shows It Result..." elif action == "f_delete": text = f"<i>Are You Sure That You Want To Clear All Filter From This Chat</i> <code>{channel_name}</code><i>???</i>\n" text += f"\n<i>This Will Erase All Files From DB..</i>" buttons = [[ InlineKeyboardButton( "Yes", callback_data=f"{action}({channel_id}|{channel_name})"), InlineKeyboardButton("No", callback_data="close") ]] reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_show_invites(bot, update: CallbackQuery): """ A Callback Funtion For Enabling Or Diabling Invite Link Buttons """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return value, chat_id = re.findall(r"show_invites\((.+)\)", query_data)[0].split("|", 1) value = True if value == "True" else False if value: buttons = [[ InlineKeyboardButton( "Disable β", callback_data=f"set(showInv|False|{chat_id}|{value})") ], [ InlineKeyboardButton("Back π", callback_data=f"config({chat_id})") ]] else: buttons = [[ InlineKeyboardButton( "Enable β", callback_data=f"set(showInv|True|{chat_id}|{value})") ], [ InlineKeyboardButton("Back π", callback_data=f"config({chat_id})") ]] text = f"<i>This Config Will Help You To Show Invitation Link Of All Active Chats Along With The Filter Results For The Users To Join.....</i>" reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_channel_delete(bot, update: CallbackQuery): """ A Callback Funtion For Delete A Channel Connection From A Group Chat History Along With All Its Filter Files """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return channel_id, channel_name = re.findall(r"c_delete\((.+)\)", query_data)[0].split("|", 1) channel_id = int(channel_id) c_delete = await db.del_chat(chat_id, channel_id) a_delete = await db.del_active(chat_id, channel_id) # pylint: disable=unused-variable f_delete = await db.del_filters(chat_id, channel_id) if (c_delete and f_delete): text = f"<code>{channel_name} [ {channel_id} ]</code> Has Been Sucessfully Deleted And All Its Files Were Cleared From DB...." else: text = f"<i>Couldn't Delete Channel And All Its Files From DB Sucessfully....</i>\n<i>Please Try Again After Sometimes...Also Make Sure To Check The Logs..!!</i>" await update.answer(text=text, show_alert=True) buttons = [[ InlineKeyboardButton("π Back", callback_data=f"channel_list({chat_id})"), InlineKeyboardButton("Close π", callback_data="close") ]] await recacher(chat_id, True, True, bot, update) reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_pm_file(bot, update: CallbackQuery): """ A Callback Funtion For Enabling Or Diabling File Transfer Through Bot PM """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return value, chat_id = re.findall(r"inPM\((.+)\)", query_data)[0].split("|", 1) value = True if value == "True" else False if value: buttons = [[ InlineKeyboardButton( "Disable β", callback_data=f"set(inPM|False|{chat_id}|{value})") ], [ InlineKeyboardButton("Back π", callback_data=f"config({chat_id})") ]] else: buttons = [[ InlineKeyboardButton( "Enable β ", callback_data=f"set(inPM|True|{chat_id}|{value})") ], [ InlineKeyboardButton("Back π", callback_data=f"config({chat_id})") ]] text = f"<i>This Config Will Help You To Enable/Disable File Transfer Through Bot PM Without Redirecting Them To Channel....</i>" reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_settings(bot, update: CallbackQuery): """ A Callback Funtion For Back Button in /settings Command """ global VERIFY chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): # Check If User Is Admin return bot_status = await bot.get_me() bot_fname = bot_status.first_name text = f"<i>{bot_fname}'s</i> Settings Pannel.....\n" text += f"\n<i>You Can Use This Menu To Change Connectivity And Know Status Of Your Every Connected Channel, Change Filter Types, Configure Filter Results And To Know Status Of Your Group...</i>" buttons = [[ InlineKeyboardButton("Channels", callback_data=f"channel_list({chat_id})"), InlineKeyboardButton("Filter Types", callback_data=f"types({chat_id})") ], [ InlineKeyboardButton("Configure π ", callback_data=f"config({chat_id})") ], [ InlineKeyboardButton("Status", callback_data=f"status({chat_id})"), InlineKeyboardButton("About", callback_data=f"about({chat_id})") ], [InlineKeyboardButton("Close π", callback_data="close")]] reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_set(bot, update: CallbackQuery): """ A Callback Funtion Support For config() """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return action, val, chat_id, curr_val = re.findall(r"set\((.+)\)", query_data)[0].split("|", 3) try: val, chat_id, curr_val = int(val), int(chat_id), int(curr_val) except: chat_id = int(chat_id) if val == curr_val: await update.answer( "New Value Cannot Be Old Value...Please Choose Different Value...!!!", show_alert=True) return prev = await db.find_chat(chat_id) p_max_pages = int(prev["configs"].get("max_pages")) p_max_results = int(prev["configs"].get("max_results")) p_max_per_page = int(prev["configs"].get("max_per_page")) pm_file_chat = True if prev["configs"]["pm_fchat"] == ( True or "True") else False show_invite_link = True if prev["configs"]["show_invite_link"] == ( True or "True") else False if action == "pages": p_max_pages = val elif action == "results": p_max_results = val elif action == "per_page": p_max_per_page = val elif action == "showInv": show_invite_link = True if val == "True" else False elif action == "inPM": pm_file_chat = True if val == "True" else False new = dict(max_pages=p_max_pages, max_results=p_max_results, max_per_page=p_max_per_page, show_invite_link=show_invite_link, pm_fchat=pm_file_chat) append_db = await db.update_configs(chat_id, new) if not append_db: text = "Something Wrong Please Check Bot Log For More Information...." await update.answer(text=text, show_alert=True) return text = f"Your Request Was Updated Sucessfully....\nNow All Upcoming Results Will Show According To This Settings..." buttons = [[ InlineKeyboardButton("Back π", callback_data=f"config({chat_id})"), InlineKeyboardButton("Close π", callback_data="close") ]] reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def connect(bot: Bot, update): """ A Funtion To Handle Incoming /add Command TO COnnect A Chat With Group """ chat_id = update.chat.id user_id = update.from_user.id if update.from_user else None target_chat = update.text.split(None, 1) global VERIFY if VERIFY.get(str(chat_id)) == None: # Make Admin's ID List admin_list = [] async for x in bot.iter_chat_members(chat_id=chat_id, filter="administrators"): admin_id = x.user.id admin_list.append(admin_id) admin_list.append(None) VERIFY[str(chat_id)] = admin_list if not user_id in VERIFY.get(str(chat_id)): return try: if target_chat[1].startswith("@"): if len(target_chat[1]) < 5: await update.reply_text("Invalid Username...!!!") return target = target_chat[1] elif not target_chat[1].startswith("@"): if len(target_chat[1]) < 14: await update.reply_text( "Invalid Chat Id...\nChat ID Should Be Something Like This: <code>-100xxxxxxxxxx</code>" ) return target = int(target_chat[1]) except Exception: await update.reply_text( "Invalid Input...\nYou Should Specify Valid <code>chat_id(-100xxxxxxxxxx)</code> or <code>@username</code>" ) return # Exports invite link from target channel for user to join try: join_link = await bot.export_chat_invite_link(target) except Exception as e: logger.exception(e, exc_info=True) await update.reply_text( f"Make Sure Im Admin At <code>{target}</code> And Have Permission For <i>Inviting Users via Link</i> And Try Again.....!!!\n\n<i><b>Error Logged:</b></i> <code>{e}</code>", parse_mode='html') return userbot_info = await bot.USER.get_me() # Joins to targeted chat using above exported invite link # If aldready joined, code just pass on to next code try: await bot.USER.join_chat(join_link) except UserAlreadyParticipant: pass except Exception as e: logger.exception(e, exc_info=True) await update.reply_text( f"{userbot_info.mention} Couldnt Join The Channel <code>{target}</code> Make Sure Userbot Is Not Banned There Or Add It Manually And Try Again....!!\n\n<i><b>Error Logged:</b></i> <code>{e}</code>", parse_mode='html') return try: c_chat = await bot.get_chat(target) channel_id = c_chat.id channel_name = c_chat.title except Exception as e: await update.reply_text("Encountered Some Issue..Please Check Logs..!!" ) raise e in_db = await db.in_db(chat_id, channel_id) if in_db: await update.reply_text("Channel Aldready In Db...!!!") return wait_msg = await update.reply_text( "Please Wait Till I Add All Your Files From Channel To Db\n\n<i>This May Take 10 or 15 Mins Depending On Your No. Of Files In Channel.....</i>\n\nUntil Then Please Dont Sent Any Other Command Or This Operation May Be Intrupted...." ) try: type_list = ["video", "audio", "document"] data = [] skipCT = 0 for typ in type_list: async for msgs in bot.USER.search_messages( channel_id, filter=typ): #Thanks To @PrgOfficial For Suggesting # Using 'if elif' instead of 'or' to determine 'file_type' # Better Way? Make A PR try: if msgs.video: try: file_id = await bot.get_messages( channel_id, message_ids=msgs.message_id) except FloodWait as e: asyncio.sleep(e.x) file_id = await bot.get_messages( channel_id, message_ids=msgs.message_id) except Exception as e: print(e) continue file_id = file_id.video.file_id file_name = msgs.video.file_name[0:-4] file_caption = msgs.caption if msgs.caption else "" file_size = msgs.video.file_size file_type = "video" elif msgs.audio: try: file_id = await bot.get_messages( channel_id, message_ids=msgs.message_id) except FloodWait as e: asyncio.sleep(e.x) file_id = await bot.get_messages( channel_id, message_ids=msgs.message_id) except Exception as e: print(e) continue file_id = file_id.audio.file_id file_name = msgs.audio.file_name[0:-4] file_caption = msgs.caption if msgs.caption else "" file_size = msgs.audio.file_size file_type = "audio" elif msgs.document: try: file_id = await bot.get_messages( channel_id, message_ids=msgs.message_id) except FloodWait as e: asyncio.sleep(e.x) file_id = await bot.get_messages( channel_id, message_ids=msgs.message_id) except Exception as e: print(str(e)) continue file_id = file_id.document.file_id file_name = msgs.document.file_name[0:-4] file_caption = msgs.caption if msgs.caption else "" file_size = msgs.document.file_size file_type = "document" for i in ["_", "|", "-", "."]: # Work Around try: file_name = file_name.replace(i, " ") except Exception: pass file_link = msgs.link group_id = chat_id unique_id = ''.join( random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(15)) dicted = dict( file_id=file_id, # Done unique_id=unique_id, file_name=file_name, file_caption=file_caption, file_size=file_size, file_type=file_type, file_link=file_link, chat_id=channel_id, group_id=group_id, ) data.append(dicted) except Exception as e: if 'NoneType' in str( e ): # For Some Unknown Reason Some File Names are NoneType skipCT += 1 continue print(e) print( f"{skipCT} Files Been Skipped Due To File Name Been None..... #BlameTG" ) except Exception as e: await wait_msg.edit_text( "Couldnt Fetch Files From Channel... Please look Into Logs For More Details" ) raise e await db.add_filters(data) await db.add_chat(chat_id, channel_id, channel_name) await recacher(chat_id, True, True, bot, update) await wait_msg.edit_text( f"Channel Was Sucessfully Added With <code>{len(data)}</code> Files..")
async def disconnect(bot: Bot, update): """ A Funtion To Handle Incoming /del Command TO Disconnect A Chat With A Group """ chat_id = update.chat.id user_id = update.from_user.id if update.from_user else None target_chat = update.text.split(None, 1) global VERIFY if VERIFY.get(str(chat_id)) == None: # Make Admin's ID List admin_list = [] async for x in bot.iter_chat_members(chat_id=chat_id, filter="administrators"): admin_id = x.user.id admin_list.append(admin_id) admin_list.append(None) VERIFY[str(chat_id)] = admin_list if not user_id in VERIFY.get(str(chat_id)): return try: if target_chat[1].startswith("@"): if len(target_chat[1]) < 5: await update.reply_text("Invalid Username...!!!") return target = target_chat[1] elif not target_chat.startswith("@"): if len(target_chat[1]) < 14: await update.reply_text( "Invalid Chat Id...\nChat ID Should Be Something Like This: <code>-100xxxxxxxxxx</code>" ) return target = int(target_chat[1]) except Exception: await update.reply_text( "Invalid Input...\nYou Should Specify Valid chat_id(-100xxxxxxxxxx) or @username" ) return userbot = await bot.USER.get_me() userbot_name = userbot.first_name userbot_id = userbot.id try: channel_info = await bot.USER.get_chat(target) channel_id = channel_info.id except Exception: await update.reply_text( f"My UserBot [{userbot_name}](tg://user?id={userbot_id}) Couldnt Fetch Details Of `{target}` Make Sure Userbot Is Not Banned There Or Add It Manually And Try Again....!!" ) return in_db = await db.in_db(chat_id, channel_id) if not in_db: await update.reply_text( "This Channel Is Not Connected With The Group...") return wait_msg = await update.reply_text( "Deleting All Files Of This Channel From DB....!!!\n\nPlease Be Patience...Dont Sent Another Command Until This Process Finishes.." ) await db.del_filters(chat_id, channel_id) await db.del_active(chat_id, channel_id) await db.del_chat(chat_id, channel_id) await recacher(chat_id, True, True, bot, update) await wait_msg.edit_text("Sucessfully Deleted All Files From DB....")
async def cb_channel_list(bot, update: CallbackQuery): """ A Callback Funtion For Displaying All Channel List And Providing A Menu To Navigate To Every COnnect Chats For Furthur Control """ global VERIFY query_data = update.data chat_id = update.message.chat.id chat_name = remove_emoji(update.message.chat.title) chat_name = chat_name.encode('ascii', 'ignore').decode('ascii')[:35] user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return chat_id = re.findall(r"channel_list\((.+)\)", query_data)[0] text = "<i>Semms Like You Dont Have Any Channel Connected...</i>\n\n<i>Connect To Any Chat To Continue With This Settings...</i>" db_list = await db.find_chat(int(chat_id)) channel_id_list = [] channel_name_list = [] if db_list: for x in db_list["chat_ids"]: channel_id = x["chat_id"] channel_name = x["chat_name"] try: if (channel_id == None or channel_name == None): continue except: break channel_name = remove_emoji(channel_name).encode( 'ascii', 'ignore').decode('ascii')[:35] channel_id_list.append(channel_id) channel_name_list.append(channel_name) buttons = [] buttons.append([ InlineKeyboardButton("π Back", callback_data="settings"), InlineKeyboardButton("Close π", callback_data="close") ]) if channel_name_list: text = f"<i>List Of Connected Channels With <code>{chat_name}</code> With There Settings..</i>\n" for x in range(1, (len(channel_name_list) + 1)): text += f"\n<code>{x}. {channel_name_list[x-1]}</code>\n" text += "\nChoose Appropriate Buttons To Navigate Through Respective Channels" btn_key = [ "1οΈβ£", "2οΈβ£", "3οΈβ£", "4οΈβ£", "5οΈβ£", "6οΈβ£", "7οΈβ£", "8οΈβ£", "9οΈβ£", "π", "1οΈβ£1οΈβ£", "1οΈβ£2οΈβ£", "1οΈβ£3οΈβ£", "1οΈβ£4οΈβ£", "1οΈβ£5οΈβ£", "1οΈβ£6οΈβ£", "1οΈβ£7οΈβ£", "1οΈβ£8οΈβ£", "1οΈβ£9οΈβ£", "2οΈβ£0οΈβ£" # Just In Case ππ€£ ] for i in range( 1, (len(channel_name_list) + 1)): # Append The Index Number of Channel In Just A Single Line if i == 1: buttons.insert(0, [ InlineKeyboardButton( btn_key[i - 1], callback_data= f"info({channel_id_list[i-1]}|{channel_name_list[i-1]})" ) ]) else: buttons[0].append( InlineKeyboardButton( btn_key[i - 1], callback_data= f"info({channel_id_list[i-1]}|{channel_name_list[i-1]})" )) reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text=text, reply_markup=reply_markup, parse_mode="html")
async def settings(bot, update): chat_id = update.chat.id user_id = update.from_user.id if update.from_user else None global VERIFY if VERIFY.get(str(chat_id)) == None: # Make Admin's ID List admin_list = [] async for x in bot.iter_chat_members(chat_id=chat_id, filter="administrators"): admin_id = x.user.id admin_list.append(admin_id) admin_list.append(None) VERIFY[str(chat_id)] = admin_list if not user_id in VERIFY.get(str(chat_id)): # Checks if user is admin of the chat return bot_info = await bot.get_me() bot_first_name= bot_info.first_name text =f"<i>{bot_first_name}'s</i> Settings Pannel.....\n" text+=f"\n<i>You Can Use This Menu To Change Connectivity And Know Status Of Your Every Connected Channel, Change Filter Types, Configure Filter Results And To Know Status Of Your Group...</i>" buttons = [ [ InlineKeyboardButton ( "Channels", callback_data=f"channel_list({chat_id})" ), InlineKeyboardButton ( "Filter Types", callback_data=f"types({chat_id})" ) ], [ InlineKeyboardButton ( "Configure π ", callback_data=f"config({chat_id})" ) ], [ InlineKeyboardButton ( "Status", callback_data=f"status({chat_id})" ), InlineKeyboardButton ( "About", callback_data=f"about({chat_id})" ) ], [ InlineKeyboardButton ( "Close π", callback_data="close" ) ] ] reply_markup = InlineKeyboardMarkup(buttons) await bot.send_message ( chat_id=chat_id, text=text, reply_markup=reply_markup, parse_mode="html", reply_to_message_id=update.message_id )
async def cb_config(bot, update: CallbackQuery): """ A Callback Funtion For Chaning The Number Of Total Pages / Total Results / Results Per pages / Enable or Diable Invite Link / Enable or Disable PM File Chat """ global VERIFY query_data = update.data chat_id = update.message.chat.id chat_name = remove_emoji(update.message.chat.title) user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return chat_id = re.findall(r"config\((.+)\)", query_data)[0] settings = await db.find_chat(int(chat_id)) mp_count = settings["configs"]["max_pages"] mf_count = settings["configs"]["max_results"] mr_count = settings["configs"]["max_per_page"] pm_file_chat = settings["configs"]["pm_fchat"] show_invite = settings["configs"]["show_invite_link"] text = f"<i><b>Configure Your <u><code>{chat_name}</code></u> Group's Filter Settings...</b></i>\n" text += f"\n<i>{chat_name}</i> Current Settings:\n" text += f"\n - Max Filter: <code>{mf_count}</code>\n" text += f"\n - Max Pages: <code>{mp_count}</code>\n" text += f"\n - Max Filter Per Page: <code>{mr_count}</code>\n" text += f"\n - Show Invitation Link: <code>{show_invite}</code>\n" text += f"\n - Provide File In Bot PM: <code>{pm_file_chat}</code>\n" text += "\nAdjust Above Value Using Buttons Below... " buttons = [[ InlineKeyboardButton("Filter Per Page", callback_data=f"mr_count({mr_count}|{chat_id})"), InlineKeyboardButton("Max Pages", callback_data=f"mp_count({mp_count}|{chat_id})") ]] buttons.append([ InlineKeyboardButton("Total Filter Count", callback_data=f"mf_count({mf_count}|{chat_id})") ]) buttons.append([ InlineKeyboardButton( "Show Invite Links", callback_data=f"show_invites({show_invite}|{chat_id})"), InlineKeyboardButton("Bot File Chat", callback_data=f"inPM({pm_file_chat}|{chat_id})") ]) buttons.append([InlineKeyboardButton("π Back", callback_data=f"settings")]) reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_toggle(bot, update: CallbackQuery): """ A Callback Funtion Support handler For types() """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return chat_id, types, val = re.findall(r"toggle\((.+)\)", query_data)[0].split("|", 2) _types = await db.find_chat(int(chat_id)) _types = _types["types"] vid = _types["video"] doc = _types["document"] aud = _types["audio"] if types == "video": vid = True if val == "True" else False elif types == "audio": aud = True if val == "True" else False elif types == "document": doc = True if val == "True" else False settings = {"video": vid, "audio": aud, "document": doc} process = await db.update_settings(chat_id, settings) if process: await update.answer(text="Filter Types Updated Sucessfully", show_alert=True) else: text = "Something Wrong Please Check Bot Log For More Information...." await update.answer(text, show_alert=True) return _types = await db.find_chat(int(chat_id)) text = f"<i>Filter Types Enabled In <code>{update.message.chat.title}</code></i>\n" _types = _types["types"] vid = _types["video"] doc = _types["document"] aud = _types["audio"] buttons = [] if vid: text += "\n<i><b>Video Index:</b> Enabled</i>\n" v_e = "β " vcb_data = f"toggle({chat_id}|video|False)" else: text += "\n<i><b>Video Index:</b> Disabled</i>\n" v_e = "β" vcb_data = f"toggle({chat_id}|video|True)" if doc: text += "\n<i><b>Document Index:</b> Enabled</i>\n" d_e = "β " dcb_data = f"toggle({chat_id}|document|False)" else: text += "\n<i><b>Document Index:</b> Disabled</i>\n" d_e = "β" dcb_data = f"toggle({chat_id}|document|True)" if aud: text += "\n<i><b>Audio Index:</b> Enabled</i>\n" a_e = "β " acb_data = f"toggle({chat_id}|audio|False)" else: text += "\n<i><b>Audio Index:</b> Disabled</i>\n" a_e = "β" acb_data = f"toggle({chat_id}|audio|True)" text += "\n<i>Below Buttons Will Toggle Respective Media Types As Enabled Or Disabled....\n</i>" text += "<i>This Will Take Into Action As Soon As You Change Them....</i>" buttons.append( [InlineKeyboardButton(f"Video Index : {v_e}", callback_data=vcb_data)]) buttons.append( [InlineKeyboardButton(f"Audio Index : {a_e}", callback_data=acb_data)]) buttons.append([ InlineKeyboardButton(f"Document Index : {d_e}", callback_data=dcb_data) ]) buttons.append([InlineKeyboardButton("π Back", callback_data=f"settings")]) reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_types(bot, update: CallbackQuery): """ A Callback Funtion For Changing The Result Types To Be Shown In While Sending Results """ global VERIFY query_data = update.data chat_id = update.message.chat.id chat_name = remove_emoji(update.message.chat.title) user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return chat_id = re.findall(r"types\((.+)\)", query_data)[0] _types = await db.find_chat(int(chat_id)) text = f"<i>Filter Types Enabled/Disbled In <code>{chat_name}</code></i>\n" _types = _types["types"] vid = _types["video"] doc = _types["document"] aud = _types["audio"] buttons = [] if vid: text += "\n<i><b>Video Index:</b> Enabled</i>\n" v_e = "β " vcb_data = f"toggle({chat_id}|video|False)" else: text += "\n<i><b>Video Index:</b> Disabled</i>\n" v_e = "β" vcb_data = f"toggle({chat_id}|video|True)" if doc: text += "\n<i><b>Document Index:</b> Enabled</i>\n" d_e = "β " dcb_data = f"toggle({chat_id}|document|False)" else: text += "\n<i><b>Document Index:</b> Disabled</i>\n" d_e = "β" dcb_data = f"toggle({chat_id}|document|True)" if aud: text += "\n<i><b>Audio Index:</b> Enabled</i>\n" a_e = "β " acb_data = f"toggle({chat_id}|audio|False)" else: text += "\n<i><b>Audio Index:</b> Disabled</i>\n" a_e = "β" acb_data = f"toggle({chat_id}|audio|True)" text += "\n<i>Below Buttons Will Toggle Respective Media Types As Enabled Or Disabled....\n</i>" text += "<i>This Will Take Into Action As Soon As You Change Them....</i>" buttons.append( [InlineKeyboardButton(f"Video Index: {v_e}", callback_data=vcb_data)]) buttons.append( [InlineKeyboardButton(f"Audio Index: {a_e}", callback_data=acb_data)]) buttons.append([ InlineKeyboardButton(f"Document Index: {d_e}", callback_data=dcb_data) ]) buttons.append([InlineKeyboardButton("π Back", callback_data=f"settings")]) reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")
async def cb_navg(bot, update: CallbackQuery): """ A Callback Funtion For The Next Button Appearing In Results """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id index_val, btn, query = re.findall(r"navigate\((.+)\)", query_data)[0].split("|", 2) try: ruser_id = update.message.reply_to_message.from_user.id except Exception as e: print(e) ruser_id = None admin_list = VERIFY.get(str(chat_id)) if admin_list == None: # Make Admin's ID List admin_list = [] async for x in bot.iter_chat_members(chat_id=chat_id, filter="administrators"): admin_id = x.user.id admin_list.append(admin_id) admin_list.append(None) # Just For Anonymous Admin.... VERIFY[str(chat_id)] = admin_list if not ((user_id == ruser_id) or (user_id in admin_list) ): # Checks if user is same as requested user or is admin await update.answer("Nice Try ;)", show_alert=True) return if btn == "next": index_val = int(index_val) + 1 elif btn == "back": index_val = int(index_val) - 1 achats = ACTIVE_CHATS[str(chat_id)] configs = await db.find_chat(chat_id) pm_file_chat = configs["configs"]["pm_fchat"] show_invite = configs["configs"]["show_invite_link"] show_invite = (False if pm_file_chat == True else show_invite) results = FIND.get(query).get("results") leng = FIND.get(query).get("total_len") max_pages = FIND.get(query).get("max_pages") try: temp_results = results[index_val].copy() except IndexError: return # Quick Fixππ except Exception as e: print(e) return if ((index_val + 1) == max_pages) or ((index_val + 1) == len(results)): # Max Pages temp_results.append([ InlineKeyboardButton( "βͺ Back", callback_data=f"navigate({index_val}|back|{query})") ]) elif int(index_val) == 0: pass else: temp_results.append([ InlineKeyboardButton( "βͺ Back", callback_data=f"navigate({index_val}|back|{query})"), InlineKeyboardButton( "Next β©", callback_data=f"navigate({index_val}|next|{query})") ]) if not int(index_val) == 0: temp_results.append([ InlineKeyboardButton( f"π° Page {index_val + 1}/{len(results) if len(results) < max_pages else max_pages} π°", callback_data="ignore") ]) if show_invite and int(index_val) != 0: ibuttons = [] achatId = [] await gen_invite_links(configs, chat_id, bot, update) for x in achats["chats"] if isinstance(achats, dict) else achats: achatId.append(int(x["chat_id"])) if isinstance( x, dict) else achatId.append(x) for y in INVITE_LINK.get(str(chat_id)): chat_id = int(y["chat_id"]) if chat_id not in achatId: continue chat_name = y["chat_name"] invite_link = y["invite_link"] if ((len(ibuttons) % 2) == 0): ibuttons.append([ InlineKeyboardButton(f"β {chat_name} β", url=invite_link) ]) else: ibuttons[-1].append( InlineKeyboardButton(f"β {chat_name} β", url=invite_link)) for x in ibuttons: temp_results.insert(0, x) ibuttons = None achatId = None reply_markup = InlineKeyboardMarkup(temp_results) text = f"<i>Found</i> <code>{leng}</code> <i>Results For Your Query:</i> <code>{query}</code>" try: await update.message.edit(text, reply_markup=reply_markup, parse_mode="html") except FloodWait as f: # Flood Wait Caused By Spamming Next/Back Buttons await asyncio.sleep(f.x) await update.message.edit(text, reply_markup=reply_markup, parse_mode="html")
async def cb_info(bot, update: CallbackQuery): """ A Callback Funtion For Displaying Details Of The Connected Chat And Provide Ability To Connect / Disconnect / Delete / Delete Filters of That Specific Chat """ global VERIFY query_data = update.data chat_id = update.message.chat.id user_id = update.from_user.id if user_id not in VERIFY.get(str(chat_id)): return channel_id, channel_name = re.findall(r"info\((.+)\)", query_data)[0].split("|", 1) f_count = await db.cf_count(chat_id, int(channel_id)) active_chats = await db.find_active(chat_id) if active_chats: # Checks for active chats connected to a chat dicts = active_chats["chats"] db_cids = [int(x["chat_id"]) for x in dicts] if int(channel_id) in db_cids: active_chats = True status = "Connected" else: active_chats = False status = "Disconnected" else: active_chats = False status = "Disconnected" text = f"<i>Info About <b>{channel_name}</b></i>\n" text += f"\n<i>Channel Name:</i> <code>{channel_name}</code>\n" text += f"\n<i>Channel ID:</i> <code>{channel_id}</code>\n" text += f"\n<i>Channel Files:</i> <code>{f_count}</code>\n" text += f"\n<i>Current Status:</i> <code>{status}</code>\n" if active_chats: buttons = [[ InlineKeyboardButton( "π¨ Disconnect π¨", callback_data=f"warn({channel_id}|{channel_name}|disconnect)"), InlineKeyboardButton( "Delete β", callback_data=f"warn({channel_id}|{channel_name}|c_delete)") ]] else: buttons = [[ InlineKeyboardButton( "π Connect π ", callback_data=f"warn({channel_id}|{channel_name}|connect)"), InlineKeyboardButton( "Delete β", callback_data=f"warn({channel_id}|{channel_name}|c_delete)") ]] buttons.append([ InlineKeyboardButton( "Delete Filters β ", callback_data=f"warn({channel_id}|{channel_name}|f_delete)") ]) buttons.append([ InlineKeyboardButton("π Back", callback_data=f"channel_list({chat_id})") ]) reply_markup = InlineKeyboardMarkup(buttons) await update.message.edit_text(text, reply_markup=reply_markup, parse_mode="html")