def list_handlers(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] conn = connected(bot, update, chat, user.id, need_admin=False) if not conn == False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title filter_list = "*Filters in {}:*\n" else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = "local filters" filter_list = "*local filters:*\n" else: chat_name = chat.title filter_list = "*Filters in {}*:\n".format(chat_name) all_handlers = sql.get_chat_triggers(chat_id) if not all_handlers: update.effective_message.reply_text("No filters in *{}*!".format(chat_name)) return for keyword in all_handlers: entry = " - {}\n".format(escape_markdown(keyword)) if len(entry) + len(filter_list) > telegram.MAX_MESSAGE_LENGTH: update.effective_message.reply_text(filter_list, parse_mode=telegram.ParseMode.MARKDOWN) filter_list = entry else: filter_list += entry if not filter_list == BASIC_FILTER_STRING: update.effective_message.reply_text(filter_list, parse_mode=telegram.ParseMode.MARKDOWN)
def stop_all_filters(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user message = update.effective_message if chat.type == "private": chat.title = "local filters" else: owner = chat.get_member(user.id) chat.title = chat.title if owner.status != 'creator': message.reply_text("You must be this chat creator.") return x = 0 flist = sql.get_chat_triggers(chat.id) if not flist: message.reply_text("There aren't any active filters in {} !".format( chat.title)) return f_flist = [] for f in flist: x += 1 f_flist.append(f) for fx in f_flist: sql.remove_filter(chat.id, fx) message.reply_text( "{} filters from this chat have been removed.".format(x))
def stop_filter(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] args = update.effective_message.text.split(None, 1) conn = connected(bot, update, chat, user.id) if not conn == False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = chat.id if chat.type == "private": chat_name = "local notes" else: chat_name = chat.title if len(args) < 2: return chat_filters = sql.get_chat_triggers(chat_id) if not chat_filters: update.effective_message.reply_text("No filters are active here!") return for keyword in chat_filters: if keyword == args[1]: sql.remove_filter(chat_id, args[1]) update.effective_message.reply_text( "_Filter Deleted Successfully_ *{}*.".format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop update.effective_message.reply_text( "Your Filter Keyword is Incorrect please check Your Keyword /filters")
def stop_all_filters(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user message = update.effective_message if chat.type == "private": chat.title = "local filters" else: owner = chat.get_member(user.id) chat.title = chat.title if owner.status != 'creator': message.reply_text("ʏᴏᴜ ᴍᴜsᴛ ʙᴇ ᴛʜɪs ᴄʜᴀᴛ's ᴄʀᴇᴀᴛᴏʀ.") return x = 0 flist = sql.get_chat_triggers(chat.id) if not flist: message.reply_text("ᴛʜᴇʀᴇ ᴀʀᴇɴ'ᴛ ᴀɴʏ ᴀᴄᴛɪᴠᴇ ғɪʟᴛᴇʀs ɪɴ {} !".format( chat.title)) return f_flist = [] for f in flist: x += 1 f_flist.append(f) for fx in f_flist: sql.remove_filter(chat.id, fx) message.reply_text( "{} ғɪʟᴛᴇʀs ғʀᴏᴍ ᴛʜɪs ᴄʜᴀᴛ ʜᴀᴠᴇ ʙᴇᴇɴ ʀᴇᴍᴏᴠᴇᴅ.".format(x))
def list_handlers(bot: Bot, update: Update): chat = update.effective_chat all_handlers = sql.get_chat_triggers(chat.id) update_chat_title = chat.title message_chat_title = update.effective_message.chat.title if update_chat_title == message_chat_title: BASIC_FILTER_STRING = "<b>Filters in this chat:</b>\n" else: BASIC_FILTER_STRING = f"<b>Filters in {update_chat_title}</b>:\n" if not all_handlers: if update_chat_title == message_chat_title: update.effective_message.reply_text("No filters are active here!") else: update.effective_message.reply_text(f"No filters are active in <b>{update_chat_title}</b>!", parse_mode=telegram.ParseMode.HTML) return filter_list = "" for keyword in all_handlers: entry = f" - <code>{html.escape(keyword)}</code>\n" if len(entry) + len(filter_list) + len(BASIC_FILTER_STRING) > telegram.MAX_MESSAGE_LENGTH: filter_list = BASIC_FILTER_STRING + html.escape(filter_list) update.effective_message.reply_text(filter_list, parse_mode=telegram.ParseMode.HTML) filter_list = entry else: filter_list += entry if not filter_list == BASIC_FILTER_STRING: filter_list = BASIC_FILTER_STRING + filter_list update.effective_message.reply_text(filter_list, parse_mode=telegram.ParseMode.HTML)
def stop_filter(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] args = update.effective_message.text.split(None, 1) conn = connected(bot, update, chat, user.id) if not conn == False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = chat.id if chat.type == "private": chat_name = "local notes" else: chat_name = chat.title if len(args) < 2: return chat_filters = sql.get_chat_triggers(chat_id) if not chat_filters: update.effective_message.reply_text("ɴᴏ ᴀᴄᴛɪᴠᴇ ғɪʟᴛᴇʀs ᴀʀᴇ ʜᴇʀᴇ!") return for keyword in chat_filters: if keyword == args[1]: sql.remove_filter(chat_id, args[1]) update.effective_message.reply_text( "ғɪʟᴛᴇʀ ᴅᴇʟᴇᴛᴇᴅ sᴜᴄᴄᴇssғᴜʟʟʏ ғʀᴏᴍ *{}*.".format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop update.effective_message.reply_text( "ʏᴏᴜʀ ғɪʟᴛᴇʀ ᴋᴇʏᴡᴏʀᴅ ɪs ɪɴᴄᴏʀʀᴇᴄᴛ ᴘʟᴇᴀsᴇ ᴄʜᴇᴄᴋ ʏᴏᴜʀ ᴋᴇʏᴡᴏʀᴅ /filters")
def reply_filter(bot: Bot, update: Update): chat = update.effective_chat message = update.effective_message 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])" + keyword + r"( |$|[^\w])" match = regex_searcher(pattern, to_match) if not match: #Skip to next item continue if match: filt = sql.get_filter(chat.id, keyword) 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(buttons) keyboard = InlineKeyboardMarkup(keyb) try: message.reply_text(filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Unsupported url protocol": message.reply_text("You seem to be trying to use an unsupported url protocol. Telegram " "doesn't support buttons for some protocols, such as tg://.") elif excp.message == "Reply message not found": bot.send_message(chat.id, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard) else: message.reply_text("This note could not be sent, as it is incorrectly formatted.") 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. message.reply_text(filt.reply) break
def stop_filter(bot: Bot, update: Update): chat = update.effective_chat msg = update.effective_message args = msg.text.split(None, 1) if len(args) < 2: return chat_filters = sql.get_chat_triggers(chat.id) if not chat_filters: msg.reply_text("No filters are active here!") return for keyword in chat_filters: if keyword == args[1]: sql.remove_filter(chat.id, args[1]) msg.reply_text("Yep, I'll stop replying to that.") raise DispatcherHandlerStop msg.reply_text("That's not a current filter - run /filters for all active filters.")
def list_handlers(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] conn = connected(bot, update, chat, user.id, need_admin=False) if not conn == False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title filter_list = "𝐋𝐢𝐬𝐭 𝐨𝐟 𝐚𝐥𝐥 𝐟𝐢𝐥𝐭𝐞𝐫𝐬 𝐢𝐧 \n\n*{}* \n\n".format(chat_name) else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = "ᴘʀɪᴠᴀᴛᴇ ᴄʜᴀᴛ" filter_list = "*𝐋𝐨𝐜𝐚𝐥 𝐅𝐢𝐥𝐭𝐞𝐫𝐬 :*\n" else: chat_name = chat.title filter_list = "*𝐋𝐢𝐬𝐭 𝐨𝐟 𝐚𝐥𝐥 𝐟𝐢𝐥𝐭𝐞𝐫𝐬 𝐢𝐧 {}*\n\n".format(chat_name) all_handlers = sql.get_chat_triggers(chat_id) if not all_handlers: update.effective_message.reply_text( "ɴᴏ ғɪʟᴛᴇʀs ɪɴ 👉 {} !".format(chat_name)) return for keyword in all_handlers: entry = " 👉 `{}`\n\n".format(escape_markdown(keyword)) if len(entry) + len(filter_list) > telegram.MAX_MESSAGE_LENGTH: update.effective_message.reply_text( filter_list, parse_mode=telegram.ParseMode.MARKDOWN) filter_list = entry else: filter_list += entry if not filter_list == BASIC_FILTER_STRING: update.effective_message.reply_text( filter_list, parse_mode=telegram.ParseMode.MARKDOWN)
def __chat_settings__(chat_id, user_id): cust_filters = sql.get_chat_triggers(chat_id) return "There are `{}` custom filters here.".format(len(cust_filters))
def reply_filter(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] to_match = extract_text(message) if not to_match: return if message.reply_to_message: message = message.reply_to_message 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): filt = sql.get_filter(chat.id, keyword) buttons = sql.get_buttons(chat.id, filt.keyword) if len(buttons) > 0: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) 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, reply_markup=keyboard) 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: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) should_preview_disabled = True if "telegra.ph" in filt.reply or "youtu.be" in filt.reply: should_preview_disabled = False try: message.reply_text( filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=should_preview_disabled, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Unsupported url protocol": message.reply_text( "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, or ask in @D_ar_k_Angel for help.") elif excp.message == "Reply message not found": bot.send_message(chat.id, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard) else: message.reply_text( "This note could not be sent, as it is incorrectly formatted. Ask in " "@D_ar_k_Angel if you can't figure out why!") 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. message.reply_text(filt.reply) break
def reply_filter(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] to_match = extract_text(message) if not to_match: return if message.reply_to_message: message = message.reply_to_message 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): filt = sql.get_filter(chat.id, keyword) buttons = sql.get_buttons(chat.id, filt.keyword) media_caption = filt.caption if filt.caption is not None else "" if filt.is_sticker: message.reply_sticker(filt.reply) elif filt.is_document: message.reply_document(filt.reply, caption=media_caption, parse_mode=ParseMode.MARKDOWN) elif filt.is_image: if len(buttons) > 0: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) message.reply_photo(filt.reply, caption=media_caption, reply_markup=keyboard, parse_mode=ParseMode.MARKDOWN) else: message.reply_photo(filt.reply, caption=media_caption, parse_mode=ParseMode.MARKDOWN) elif filt.is_audio: message.reply_audio(filt.reply, caption=media_caption, parse_mode=ParseMode.MARKDOWN) elif filt.is_voice: message.reply_voice(filt.reply, caption=media_caption, parse_mode=ParseMode.MARKDOWN) elif filt.is_video: message.reply_video(filt.reply, caption=media_caption, parse_mode=ParseMode.MARKDOWN) elif filt.has_markdown: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) should_preview_disabled = True if "telegra.ph" in filt.reply or "youtu.be" in filt.reply: should_preview_disabled = False try: message.reply_text( filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=should_preview_disabled, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Unsupported url protocol": message.reply_text( "ʏᴏᴜ sᴇᴇᴍ ᴛᴏ ʙᴇ ᴛʀʏɪɴɢ ᴛᴏ ᴜsᴇ ᴀɴ ᴜɴsᴜᴘᴘᴏʀᴛᴇᴅ ᴜʀʟ ᴘʀᴏᴛᴏᴄᴏʟ. ᴛᴇʟᴇɢʀᴀᴍ" "ᴅᴏᴇsɴ'ᴛ sᴜᴘᴘᴏʀᴛ ʙᴜᴛᴛᴏɴs ғᴏʀ sᴏᴍᴇ ᴘʀᴏᴛᴏᴄᴏʟs, sᴜᴄʜ ᴀs tg://. ᴘʟᴇᴀsᴇ ᴛʀʏ " "ᴀɢᴀɪɴ, ᴏʀ ᴀsᴋ ɪɴ @stemlime_bot ғᴏʀ ʜᴇʟᴘ.") elif excp.message == "Reply message not found": bot.send_message(chat.id, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard) else: message.reply_text( "ᴛʜɪs ɴᴏᴛᴇ ᴄᴏᴜʟᴅ ɴᴏᴛ ʙᴇ sᴇɴᴛ, ᴀs ɪᴛ ɪs ɪɴᴄᴏʀʀᴇᴄᴛʟʏ ғᴏʀᴍᴀᴛᴛᴇᴅ. ᴀsᴋ " "@stemlime_bot ɪғ ʏᴏᴜ ᴄᴀɴ'ᴛ ғɪɢᴜʀᴇ ᴏᴜᴛ ᴡʜʏ!") 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. message.reply_text(filt.reply) break