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 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) 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://. Please try " "again, or ask in @MarieSupport 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 " "@MarieSupport 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 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 = update.effective_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( tld(chat.id, "No filters are active in {}!").format(chat_name)) return for keyword in chat_filters: if keyword == args[1]: sql.remove_filter(chat_id, args[1]) update.effective_message.reply_text( tld(chat.id, "Yep, I'll stop replying to that in *{}*.").format( chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop update.effective_message.reply_text( tld( chat.id, "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] all_handlers = sql.get_chat_triggers(chat.id) if not all_handlers: update.effective_message.reply_text("No filters are active here!") return filter_list = BASIC_FILTER_STRING 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_filter(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] args = update.effective_message.text.split(None, 1) 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( "Yep, I'll stop replying to that.") raise DispatcherHandlerStop update.effective_message.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 = tld(chat.id, "*List of filters in {}:*\n") else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = tld(chat.id, "local filters") filter_list = tld(chat.id, "*local filters:*\n") else: chat_name = chat.title filter_list = tld(chat.id, "*Filters in {}*:\n") all_handlers = sql.get_chat_triggers(chat_id) if not all_handlers: update.effective_message.reply_text( tld(chat.id, "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.format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN) filter_list = entry else: filter_list += entry update.effective_message.reply_text(filter_list.format(chat_name), 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 __chat_settings__(bot, update, chat, chatP, user): cust_filters = sql.get_chat_triggers(chat.id) return "There are `{}` custom filters here.".format(len(cust_filters))