def reply_filter(update: Update, context: CallbackContext) -> str: chat: Optional[Chat] = update.effective_chat message: Optional[Message] = update.effective_message user: Optional[User] = update.effective_user if not user: # Ignore channel return if user.id == 777000: return chat_warn_filters = sql.get_chat_warn_triggers(chat.id) to_match = extract_text(message) if not to_match: return "" for keyword in chat_warn_filters: pattern = r"( |^|[^\w])" + re.escape(keyword) + r"( |$|[^\w])" if re.search(pattern, to_match, flags=re.IGNORECASE): user: Optional[User] = update.effective_user warn_filter = sql.get_warn_filter(chat.id, keyword) return warn(user, chat, warn_filter.reply, message) return ""
def reply_filter(update, context): chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] if not update.effective_user or update.effective_user.id == 777000: return to_match = extract_text(message) if not to_match: return chat_filters = sql.get_chat_triggers(chat.id) for keyword in chat_filters: pattern = r"( |^|[^\w])" + re.escape(keyword) + r"( |$|[^\w])" if re.search(pattern, to_match, flags=re.IGNORECASE): if MessageHandlerChecker.check_user(update.effective_user.id): return filt = sql.get_filter(chat.id, keyword) if filt.reply == "there is should be a new reply": buttons = sql.get_buttons(chat.id, filt.keyword) keyb = build_keyboard_parser(context.bot, chat.id, buttons) keyboard = InlineKeyboardMarkup(keyb) VALID_WELCOME_FORMATTERS = [ "first", "last", "fullname", "username", "id", "chatname", "mention", ] if filt.reply_text: if "%%%" in filt.reply_text: split = filt.reply_text.split("%%%") if all(split): text = random.choice(split) else: text = filt.reply_text else: text = filt.reply_text if text.startswith("~!") and text.endswith("!~"): sticker_id = text.replace("~!", "").replace("!~", "") try: context.bot.send_sticker( chat.id, sticker_id, reply_to_message_id=message.message_id, ) return except BadRequest as excp: if (excp.message == "Wrong remote file identifier specified: wrong padding in the string" ): context.bot.send_message( chat.id, "Message couldn't be sent, Is the sticker id valid?", ) return else: LOGGER.exception("Error in filters: " + excp.message) return valid_format = escape_invalid_curly_brackets( text, VALID_WELCOME_FORMATTERS) if valid_format: filtext = valid_format.format( first=escape(message.from_user.first_name), last=escape(message.from_user.last_name or message.from_user.first_name), fullname=" ".join( [ escape(message.from_user.first_name), escape(message.from_user.last_name), ] if message.from_user.last_name else [escape(message.from_user.first_name)]), username="******" + escape(message.from_user.username) if message.from_user.username else mention_html( message.from_user.id, message.from_user.first_name), mention=mention_html(message.from_user.id, message.from_user.first_name), chatname=escape(message.chat.title) if message.chat.type != "private" else escape( message.from_user.first_name), id=message.from_user.id, ) else: filtext = "" else: filtext = "" if filt.file_type in (sql.Types.BUTTON_TEXT, sql.Types.TEXT): try: context.bot.send_message( chat.id, markdown_to_html(filtext), reply_to_message_id=message.message_id, parse_mode=ParseMode.HTML, disable_web_page_preview=True, reply_markup=keyboard, ) except BadRequest as excp: error_catch = get_exception(excp, filt, chat) if error_catch == "noreply": try: context.bot.send_message( chat.id, markdown_to_html(filtext), parse_mode=ParseMode.HTML, disable_web_page_preview=True, reply_markup=keyboard, ) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) send_message( update.effective_message, get_exception(excp, filt, chat), ) else: try: send_message( update.effective_message, get_exception(excp, filt, chat), ) except BadRequest as excp: LOGGER.exception("Failed to send message: " + excp.message) else: try: ENUM_FUNC_MAP[filt.file_type]( chat.id, filt.file_id, caption=markdown_to_html(filtext), reply_to_message_id=message.message_id, parse_mode=ParseMode.HTML, disable_web_page_preview=True, reply_markup=keyboard, ) except BadRequest: send_message( message, "I don't have the permission to send the content of the filter.", ) break else: if filt.is_sticker: message.reply_sticker(filt.reply) elif filt.is_document: message.reply_document(filt.reply) elif filt.is_image: message.reply_photo(filt.reply) elif filt.is_audio: message.reply_audio(filt.reply) elif filt.is_voice: message.reply_voice(filt.reply) elif filt.is_video: message.reply_video(filt.reply) elif filt.has_markdown: buttons = sql.get_buttons(chat.id, filt.keyword) keyb = build_keyboard_parser(context.bot, chat.id, buttons) keyboard = InlineKeyboardMarkup(keyb) try: send_message( update.effective_message, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard, ) except BadRequest as excp: if excp.message == "Unsupported url protocol": try: send_message( update.effective_message, "You seem to be trying to use an unsupported url protocol. " "Telegram doesn't support buttons for some protocols, such as tg://. Please try " "again...", ) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) elif excp.message == "Reply message not found": try: context.bot.send_message( chat.id, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard, ) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) else: try: send_message( update.effective_message, "This message couldn't be sent as it's incorrectly formatted.", ) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) LOGGER.warning("Message %s could not be parsed", str(filt.reply)) LOGGER.exception( "Could not parse filter %s in chat %s", str(filt.keyword), str(chat.id), ) else: # LEGACY - all new filters will have has_markdown set to True. try: send_message(update.effective_message, filt.reply) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) break
def del_blacklist(update, context): chat = update.effective_chat message = update.effective_message user = update.effective_user bot = context.bot to_match = extract_text(message) if not to_match: return getmode, value = sql.get_blacklist_setting(chat.id) chat_filters = sql.get_chat_blacklist(chat.id) for trigger in chat_filters: pattern = r"( |^|[^\w])" + re.escape(trigger) + r"( |$|[^\w])" if re.search(pattern, to_match, flags=re.IGNORECASE): try: if getmode == 0: return elif getmode == 1: message.delete() elif getmode == 2: message.delete() warn( update.effective_user, chat, ("Using blacklisted trigger: {}".format(trigger)), message, update.effective_user, ) return elif getmode == 3: message.delete() bot.restrict_chat_member( chat.id, update.effective_user.id, permissions=ChatPermissions(can_send_messages=False), ) bot.sendMessage( chat.id, f"Muted {user.first_name} for using Blacklisted word: {trigger}!", ) return elif getmode == 4: message.delete() res = chat.unban_member(update.effective_user.id) if res: bot.sendMessage( chat.id, f"Kicked {user.first_name} for using Blacklisted word: {trigger}!", ) return elif getmode == 5: message.delete() chat.kick_member(user.id) bot.sendMessage( chat.id, f"Banned {user.first_name} for using Blacklisted word: {trigger}", ) return elif getmode == 6: message.delete() bantime = extract_time(message, value) chat.kick_member(user.id, until_date=bantime) bot.sendMessage( chat.id, f"Banned {user.first_name} until '{value}' for using Blacklisted word: {trigger}!", ) return elif getmode == 7: message.delete() mutetime = extract_time(message, value) bot.restrict_chat_member( chat.id, user.id, until_date=mutetime, permissions=ChatPermissions(can_send_messages=False), ) bot.sendMessage( chat.id, f"Muted {user.first_name} until '{value}' for using Blacklisted word: {trigger}!", ) return except BadRequest as excp: if excp.message == "Message to delete not found": pass else: LOGGER.exception("Error while deleting blacklist message.") break