def get_invalid_chats(update: Update, context: CallbackContext, remove: bool = False): bot = context.bot chat_id = update.effective_chat.id chats = user_sql.get_all_chats() kicked_chats, progress = 0, 0 chat_list = [] progress_message = None for chat in chats: if ((100 * chats.index(chat)) / len(chats)) > progress: progress_bar = f"{progress}% completed in getting invalid chats." if progress_message: try: bot.editMessageText(progress_bar, chat_id, progress_message.message_id) except: pass else: progress_message = bot.sendMessage(chat_id, progress_bar) progress += 5 cid = chat.chat_id sleep(0.1) try: bot.get_chat(cid, timeout=60) except (BadRequest, Unauthorized): kicked_chats += 1 chat_list.append(cid) except: pass try: progress_message.delete() except: pass if not remove: return kicked_chats else: for muted_chat in chat_list: sleep(0.1) user_sql.rem_chat(muted_chat) return kicked_chats
def broadcast(update: Update, context: CallbackContext): to_send = update.effective_message.text.split(None, 1) if len(to_send) >= 2: to_group = False to_user = False if to_send[0] == "/broadcastgroups": to_group = True if to_send[0] == "/broadcastusers": to_user = True else: to_group = to_user = True chats = sql.get_all_chats() or [] users = get_all_users() failed = 0 failed_user = 0 if to_group: for chat in chats: try: context.bot.sendMessage( int(chat.chat_id), to_send[1], parse_mode="MARKDOWN", disable_web_page_preview=True, ) sleep(0.1) except TelegramError: failed += 1 if to_user: for user in users: try: context.bot.sendMessage( int(user.user_id), to_send[1], parse_mode="MARKDOWN", disable_web_page_preview=True, ) sleep(0.1) except TelegramError: failed_user += 1 update.effective_message.reply_text( f"Broadcast complete.\nGroups failed: {failed}.\nUsers failed: {failed_user}." )
def chats(update: Update, context: CallbackContext): all_chats = sql.get_all_chats() or [] chatfile = 'List of chats.\n0. Chat name | Chat ID | Members count\n' P = 1 for chat in all_chats: try: curr_chat = bot.getChat(chat.chat_id) bot_member = curr_chat.get_member(bot.id) chat_members = curr_chat.get_members_count(bot.id) chatfile += "{}. {} | {} | {}\n".format(P, chat.chat_name, chat.chat_id, chat_members) P = P + 1 except: pass with BytesIO(str.encode(chatfile)) as output: output.name = "chatlist.txt" update.effective_message.reply_document( document=output, filename="chatlist.txt", caption="Here is the list of chats in my database.")