def add_blacklist(update, context): msg = update.effective_message chat = update.effective_chat user = update.effective_user words = msg.text.split(None, 1) conn = connected(context.bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1] to_blacklist = list( set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) for trigger in to_blacklist: sql.add_to_blacklist(chat_id, trigger.lower()) if len(to_blacklist) == 1: send_message( update.effective_message, "Added blacklist <code>{}</code> in chat: <b>{}</b>!".format( html.escape(to_blacklist[0]), html.escape(chat_name)), parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "Added blacklist trigger: <code>{}</code> in <b>{}</b>!". format(len(to_blacklist), html.escape(chat_name)), parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "Tell me which words you would like to add in blacklist.", )
def blacklist(update, context): chat = update.effective_chat user = update.effective_user args = context.args conn = connected(context.bot, update, chat, user.id, need_admin=False) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if chat.type == "private": return else: chat_id = update.effective_chat.id chat_name = chat.title filter_list = "Current blacklisted words in <b>{}</b>:\n".format(chat_name) all_blacklisted = sql.get_chat_blacklist(chat_id) if len(args) > 0 and args[0].lower() == "copy": for trigger in all_blacklisted: filter_list += "<code>{}</code>\n".format(html.escape(trigger)) else: for trigger in all_blacklisted: filter_list += " - <code>{}</code>\n".format(html.escape(trigger)) # for trigger in all_blacklisted: # filter_list += " - <code>{}</code>\n".format(html.escape(trigger)) split_text = split_message(filter_list) for text in split_text: if filter_list == "Current blacklisted words in <b>{}</b>:\n".format( html.escape(chat_name)): send_message( update.effective_message, "No blacklisted words in <b>{}</b>!".format( html.escape(chat_name)), parse_mode=ParseMode.HTML, ) return send_message(update.effective_message, text, parse_mode=ParseMode.HTML)
def list_handlers(update, context): chat = update.effective_chat user = update.effective_user conn = connected(context.bot, update, chat, user.id, need_admin=False) if not conn is False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title filter_list = "*Filter 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" all_handlers = sql.get_chat_triggers(chat_id) if not all_handlers: send_message(update.effective_message, "No filters saved 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: send_message( update.effective_message, filter_list.format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN, ) filter_list = entry else: filter_list += entry send_message( update.effective_message, filter_list.format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN, )
def stop_filter(update, context): chat = update.effective_chat user = update.effective_user args = update.effective_message.text.split(None, 1) conn = connected(context.bot, update, chat, user.id) if not conn is 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 filters" else: chat_name = chat.title if len(args) < 2: send_message(update.effective_message, "What should i stop?") return chat_filters = sql.get_chat_triggers(chat_id) if not chat_filters: send_message(update.effective_message, "No filters active here!") return for keyword in chat_filters: if keyword == args[1]: sql.remove_filter(chat_id, args[1]) send_message( update.effective_message, "Okay, I'll stop replying to that filter in *{}*.".format( chat_name), parse_mode=telegram.ParseMode.MARKDOWN, ) raise DispatcherHandlerStop send_message( update.effective_message, "That's not a filter - Click: /filters to get currently active filters.", )
def blackliststicker(update: Update, context: CallbackContext): msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] bot, args = context.bot, context.args conn = connected(bot, update, chat, user.id, need_admin=False) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if chat.type == "private": return else: chat_id = update.effective_chat.id chat_name = chat.title sticker_list = "<b>List blacklisted stickers currently in {}:</b>\n".format( chat_name) all_stickerlist = sql.get_chat_stickers(chat_id) if len(args) > 0 and args[0].lower() == 'copy': for trigger in all_stickerlist: sticker_list += "<code>{}</code>\n".format(html.escape(trigger)) elif len(args) == 0: for trigger in all_stickerlist: sticker_list += " - <code>{}</code>\n".format(html.escape(trigger)) split_text = split_message(sticker_list) for text in split_text: if sticker_list == "<b>List blacklisted stickers currently in {}:</b>\n".format( chat_name).format(html.escape(chat_name)): send_message( update.effective_message, "There are no blacklist stickers in <b>{}</b>!".format( html.escape(chat_name)), parse_mode=ParseMode.HTML) return send_message(update.effective_message, text, parse_mode=ParseMode.HTML)
def list_locks(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # Connection check conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_name = chat.title else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command is meant to use in group not in PM", ) return "" chat = update.effective_chat chat_name = update.effective_message.chat.title res = build_lock_message(chat.id) if conn: res = res.replace("Locks in", "*{}*".format(chat_name)) send_message(update.effective_message, res, parse_mode=ParseMode.MARKDOWN)
def unlock(update, context) -> str: args = context.args chat = update.effective_chat user = update.effective_user message = update.effective_message if is_user_admin(chat, message.from_user.id): if len(args) >= 1: ltype = args[0].lower() if ltype in LOCK_TYPES: # Connection check conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = chat.title text = "Unlocked {} for everyone in {}!".format( ltype, chat_name) else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command is meant to use in group not in PM", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title text = "Unlocked {} for everyone!".format(ltype) sql.update_lock(chat.id, ltype, locked=False) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>" "\n#UNLOCK" "\n<b>Admin:</b> {}" "\nUnlocked <code>{}</code>.".format( html.escape(chat.title), mention_html(user.id, user.first_name), ltype, )) elif ltype in UNLOCK_CHAT_RESTRICTION: # Connection check conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = chat.title text = "Unlocked {} for everyone in {}!".format( ltype, chat_name) else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command is meant to use in group not in PM", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title text = "Unlocked {} for everyone!".format(ltype) can_change_info = chat.get_member( context.bot.id).can_change_info if not can_change_info: send_message( update.effective_message, "I don't have permission to change group info.", parse_mode="markdown") return current_permission = context.bot.getChat(chat_id).permissions context.bot.set_chat_permissions( chat_id=chat_id, permissions=get_permission_list( eval(str(current_permission)), UNLOCK_CHAT_RESTRICTION[ltype.lower()], ), ) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>" "\n#UNLOCK" "\n<b>Admin:</b> {}" "\nUnlocked <code>{}</code>.".format( html.escape(chat.title), mention_html(user.id, user.first_name), ltype, )) else: send_message( update.effective_message, "What are you trying to unlock...? Try /locktypes for the list of lockables.", ) else: send_message(update.effective_message, "What are you trying to unlock...?") return ""
def lock(update, context) -> str: args = context.args chat = update.effective_chat user = update.effective_user if (can_delete(chat, context.bot.id) or update.effective_message.chat.type == "private"): if len(args) >= 1: ltype = args[0].lower() if ltype in LOCK_TYPES: # Connection check conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = chat.title text = "Locked {} for non-admins in {}!".format( ltype, chat_name) else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command is meant to use in group not in PM", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title text = "Locked {} for non-admins!".format(ltype) sql.update_lock(chat.id, ltype, locked=True) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>" "\n#LOCK" "\n<b>Admin:</b> {}" "\nLocked <code>{}</code>.".format( html.escape(chat.title), mention_html(user.id, user.first_name), ltype, )) elif ltype in LOCK_CHAT_RESTRICTION: # Connection check conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = chat.title text = "Locked {} for all non-admins in {}!".format( ltype, chat_name) else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command is meant to use in group not in PM", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title text = "Locked {} for all non-admins!".format(ltype) current_permission = context.bot.getChat(chat_id).permissions context.bot.set_chat_permissions( chat_id=chat_id, permissions=get_permission_list( eval(str(current_permission)), LOCK_CHAT_RESTRICTION[ltype.lower()], ), ) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>" "\n#Permission_LOCK" "\n<b>Admin:</b> {}" "\nLocked <code>{}</code>.".format( html.escape(chat.title), mention_html(user.id, user.first_name), ltype, )) else: send_message( update.effective_message, "What are you trying to lock...? Try /locktypes for the list of lockables", ) else: send_message(update.effective_message, "What are you trying to lock...?") else: send_message( update.effective_message, "I am not administrator or haven't got enough rights.", ) return ""
def add_blackliststicker(update: Update, context: CallbackContext): bot = context.bot msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] words = msg.text.split(None, 1) bot = context.bot conn = connected(bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1].replace('https://t.me/addstickers/', '') to_blacklist = list( set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) added = 0 for trigger in to_blacklist: try: get = bot.getStickerSet(trigger) sql.add_to_stickers(chat_id, trigger.lower()) added += 1 except BadRequest: send_message(update.effective_message, "Sticker `{}` can not be found!".format(trigger), parse_mode="markdown") if added == 0: return if len(to_blacklist) == 1: send_message( update.effective_message, "Sticker <code>{}</code> added to blacklist stickers in <b>{}</b>!" .format(html.escape(to_blacklist[0]), html.escape(chat_name)), parse_mode=ParseMode.HTML) else: send_message( update.effective_message, "<code>{}</code> stickers added to blacklist sticker in <b>{}</b>!" .format(added, html.escape(chat_name)), parse_mode=ParseMode.HTML) elif msg.reply_to_message: added = 0 trigger = msg.reply_to_message.sticker.set_name if trigger is None: send_message(update.effective_message, "Sticker is invalid!") return try: get = bot.getStickerSet(trigger) sql.add_to_stickers(chat_id, trigger.lower()) added += 1 except BadRequest: send_message(update.effective_message, "Sticker `{}` can not be found!".format(trigger), parse_mode="markdown") if added == 0: return send_message( update.effective_message, "Sticker <code>{}</code> added to blacklist stickers in <b>{}</b>!" .format(trigger, html.escape(chat_name)), parse_mode=ParseMode.HTML) else: send_message( update.effective_message, "Tell me what stickers you want to add to the blacklist.")
def blacklist_mode(update: Update, context: CallbackContext): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] bot, args = context.bot, context.args conn = connected(bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title if args: if args[0].lower() == 'off' or args[0].lower( ) == 'nothing' or args[0].lower() == 'no': settypeblacklist = 'turn off' sql.set_blacklist_strength(chat_id, 0, "0") elif args[0].lower() == 'del' or args[0].lower() == 'delete': settypeblacklist = 'left, the message will be deleted' sql.set_blacklist_strength(chat_id, 1, "0") elif args[0].lower() == 'warn': settypeblacklist = 'warned' sql.set_blacklist_strength(chat_id, 2, "0") elif args[0].lower() == 'mute': settypeblacklist = 'muted' sql.set_blacklist_strength(chat_id, 3, "0") elif args[0].lower() == 'kick': settypeblacklist = 'kicked' sql.set_blacklist_strength(chat_id, 4, "0") elif args[0].lower() == 'ban': settypeblacklist = 'banned' sql.set_blacklist_strength(chat_id, 5, "0") elif args[0].lower() == 'tban': if len(args) == 1: teks = """It looks like you are trying to set a temporary value to blacklist, but has not determined the time; use `/blstickermode tban <timevalue>`. Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return settypeblacklist = 'temporary banned for {}'.format(args[1]) sql.set_blacklist_strength(chat_id, 6, str(args[1])) elif args[0].lower() == 'tmute': if len(args) == 1: teks = """It looks like you are trying to set a temporary value to blacklist, but has not determined the time; use `/blstickermode tmute <timevalue>`. Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return settypeblacklist = 'temporary muted for {}'.format(args[1]) sql.set_blacklist_strength(chat_id, 7, str(args[1])) else: send_message( update.effective_message, "I only understand off/del/warn/ban/kick/mute/tban/tmute!") return if conn: text = "Blacklist sticker mode changed, users will be `{}` at *{}*!".format( settypeblacklist, chat_name) else: text = "Blacklist sticker mode changed, users will be `{}`!".format( settypeblacklist) send_message(update.effective_message, text, parse_mode="markdown") return "<b>{}:</b>\n" \ "<b>Admin:</b> {}\n" \ "Changed sticker blacklist mode. users will be {}.".format(html.escape(chat.title), mention_html(user.id, html.escape(user.first_name)), settypeblacklist) else: getmode, getvalue = sql.get_blacklist_setting(chat.id) if getmode == 0: settypeblacklist = "not active" elif getmode == 1: settypeblacklist = "delete" elif getmode == 2: settypeblacklist = "warn" elif getmode == 3: settypeblacklist = "mute" elif getmode == 4: settypeblacklist = "kick" elif getmode == 5: settypeblacklist = "ban" elif getmode == 6: settypeblacklist = "temporarily banned for {}".format(getvalue) elif getmode == 7: settypeblacklist = "temporarily muted for {}".format(getvalue) if conn: text = "Blacklist sticker mode is currently set to *{}* in *{}*.".format( settypeblacklist, chat_name) else: text = "Blacklist sticker mode is currently set to *{}*.".format( settypeblacklist) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) return ""
def unblackliststicker(update: Update, context: CallbackContext): bot = context.bot msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] words = msg.text.split(None, 1) bot = context.bot conn = connected(bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1].replace('https://t.me/addstickers/', '') to_unblacklist = list( set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) successful = 0 for trigger in to_unblacklist: success = sql.rm_from_stickers(chat_id, trigger.lower()) if success: successful += 1 if len(to_unblacklist) == 1: if successful: send_message( update.effective_message, "Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!" .format(html.escape(to_unblacklist[0]), html.escape(chat_name)), parse_mode=ParseMode.HTML) else: send_message(update.effective_message, "This sticker is not on the blacklist...!") elif successful == len(to_unblacklist): send_message( update.effective_message, "Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!". format(successful, html.escape(chat_name)), parse_mode=ParseMode.HTML) elif not successful: send_message( update.effective_message, "None of these stickers exist, so they cannot be removed.". format(successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) else: send_message( update.effective_message, "Sticker <code>{}</code> deleted from blacklist. {} did not exist, so it's not deleted." .format(successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) elif msg.reply_to_message: trigger = msg.reply_to_message.sticker.set_name if trigger is None: send_message(update.effective_message, "Sticker is invalid!") return success = sql.rm_from_stickers(chat_id, trigger.lower()) if success: send_message( update.effective_message, "Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!". format(trigger, chat_name), parse_mode=ParseMode.HTML) else: send_message( update.effective_message, "{} not found on blacklisted stickers...!".format(trigger)) else: send_message( update.effective_message, "Tell me what stickers you want to add to the blacklist.")
def blacklist_mode(update, context): chat = update.effective_chat user = update.effective_user msg = update.effective_message args = context.args conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message( update.effective_message, "This command can be only used in group not in PM", ) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title if args: if (args[0].lower() == "off" or args[0].lower() == "nothing" or args[0].lower() == "no"): settypeblacklist = "do nothing" sql.set_blacklist_strength(chat_id, 0, "0") elif args[0].lower() == "del" or args[0].lower() == "delete": settypeblacklist = "delete blacklisted message" sql.set_blacklist_strength(chat_id, 1, "0") elif args[0].lower() == "warn": settypeblacklist = "warn the sender" sql.set_blacklist_strength(chat_id, 2, "0") elif args[0].lower() == "mute": settypeblacklist = "mute the sender" sql.set_blacklist_strength(chat_id, 3, "0") elif args[0].lower() == "kick": settypeblacklist = "kick the sender" sql.set_blacklist_strength(chat_id, 4, "0") elif args[0].lower() == "ban": settypeblacklist = "ban the sender" sql.set_blacklist_strength(chat_id, 5, "0") elif args[0].lower() == "tban": if len(args) == 1: teks = """It looks like you tried to set time value for blacklist but you didn't specified time; Try, `/blacklistmode tban <timevalue>`. Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" restime = extract_time(msg, args[1]) if not restime: teks = """Invalid time value! Example of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" settypeblacklist = "temporarily ban for {}".format(args[1]) sql.set_blacklist_strength(chat_id, 6, str(args[1])) elif args[0].lower() == "tmute": if len(args) == 1: teks = """It looks like you tried to set time value for blacklist but you didn't specified time; try, `/blacklistmode tmute <timevalue>`. Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" restime = extract_time(msg, args[1]) if not restime: teks = """Invalid time value! Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks.""" send_message(update.effective_message, teks, parse_mode="markdown") return "" settypeblacklist = "temporarily mute for {}".format(args[1]) sql.set_blacklist_strength(chat_id, 7, str(args[1])) else: send_message( update.effective_message, "I only understand: off/del/warn/ban/kick/mute/tban/tmute!", ) return "" if conn: text = "Changed blacklist mode: `{}` in *{}*!".format( settypeblacklist, chat_name) else: text = "Changed blacklist mode: `{}`!".format(settypeblacklist) send_message(update.effective_message, text, parse_mode="markdown") return ("<b>{}:</b>\n" "<b>Admin:</b> {}\n" "Changed the blacklist mode. will {}.".format( html.escape(chat.title), mention_html(user.id, html.escape(user.first_name)), settypeblacklist, )) else: getmode, getvalue = sql.get_blacklist_setting(chat.id) if getmode == 0: settypeblacklist = "do nothing" elif getmode == 1: settypeblacklist = "delete" elif getmode == 2: settypeblacklist = "warn" elif getmode == 3: settypeblacklist = "mute" elif getmode == 4: settypeblacklist = "kick" elif getmode == 5: settypeblacklist = "ban" elif getmode == 6: settypeblacklist = "temporarily ban for {}".format(getvalue) elif getmode == 7: settypeblacklist = "temporarily mute for {}".format(getvalue) if conn: text = "Current blacklistmode: *{}* in *{}*.".format( settypeblacklist, chat_name) else: text = "Current blacklistmode: *{}*.".format(settypeblacklist) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) return ""
def unblacklist(update, context): msg = update.effective_message chat = update.effective_chat user = update.effective_user words = msg.text.split(None, 1) conn = connected(context.bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1] to_unblacklist = list( set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) successful = 0 for trigger in to_unblacklist: success = sql.rm_from_blacklist(chat_id, trigger.lower()) if success: successful += 1 if len(to_unblacklist) == 1: if successful: send_message( update.effective_message, "Removed <code>{}</code> from blacklist in <b>{}</b>!". format(html.escape(to_unblacklist[0]), html.escape(chat_name)), parse_mode=ParseMode.HTML, ) else: send_message(update.effective_message, "This is not a blacklist trigger!") elif successful == len(to_unblacklist): send_message( update.effective_message, "Removed <code>{}</code> from blacklist in <b>{}</b>!".format( successful, html.escape(chat_name)), parse_mode=ParseMode.HTML, ) elif not successful: send_message( update.effective_message, "None of these triggers exist so it can't be removed.".format( successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "Removed <code>{}</code> from blacklist. {} did not exist, " "so were not removed.".format(successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML, ) else: send_message( update.effective_message, "Tell me which words you would like to remove from blacklist!", )
def filters(update, context): chat = update.effective_chat user = update.effective_user msg = update.effective_message args = msg.text.split( None, 1) # use python's maxsplit to separate Cmd, keyword, and reply_text conn = connected(context.bot, update, chat, user.id) if not conn is 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 filters" else: chat_name = chat.title if not msg.reply_to_message and len(args) < 2: send_message( update.effective_message, "Please provide keyboard keyword for this filter to reply with!", ) return if msg.reply_to_message: if len(args) < 2: send_message( update.effective_message, "Please provide keyword for this filter to reply with!", ) return else: keyword = args[1] else: extracted = split_quotes(args[1]) if len(extracted) < 1: return # set trigger -> lower, so as to avoid adding duplicate filters with different cases keyword = extracted[0].lower() # Add the filter # Note: perhaps handlers can be removed somehow using sql.get_chat_filters for handler in dispatcher.handlers.get(HANDLER_GROUP, []): if handler.filters == (keyword, chat_id): dispatcher.remove_handler(handler, HANDLER_GROUP) text, file_type, file_id = get_filter_type(msg) if not msg.reply_to_message and len(extracted) >= 2: offset = len(extracted[1]) - len( msg.text) # set correct offset relative to command + notename text, buttons = button_markdown_parser(extracted[1], entities=msg.parse_entities(), offset=offset) text = text.strip() if not text: send_message( update.effective_message, "There is no note message - You can't JUST have buttons, you need a message to go with it!", ) return elif msg.reply_to_message and len(args) >= 2: if msg.reply_to_message.text: text_to_parsing = msg.reply_to_message.text elif msg.reply_to_message.caption: text_to_parsing = msg.reply_to_message.caption else: text_to_parsing = "" offset = len(text_to_parsing ) # set correct offset relative to command + notename text, buttons = button_markdown_parser(text_to_parsing, entities=msg.parse_entities(), offset=offset) text = text.strip() elif not text and not file_type: send_message( update.effective_message, "Please provide keyword for this filter reply with!", ) return elif msg.reply_to_message: if msg.reply_to_message.text: text_to_parsing = msg.reply_to_message.text elif msg.reply_to_message.caption: text_to_parsing = msg.reply_to_message.caption else: text_to_parsing = "" offset = len(text_to_parsing ) # set correct offset relative to command + notename text, buttons = button_markdown_parser(text_to_parsing, entities=msg.parse_entities(), offset=offset) text = text.strip() if (msg.reply_to_message.text or msg.reply_to_message.caption) and not text: send_message( update.effective_message, "There is no note message - You can't JUST have buttons, you need a message to go with it!", ) return else: send_message(update.effective_message, "Invalid filter!") return add = addnew_filter(update, chat_id, keyword, text, file_type, file_id, buttons) # This is an old method # sql.add_filter(chat_id, keyword, content, is_sticker, is_document, is_image, is_audio, is_voice, is_video, buttons) if add is True: send_message( update.effective_message, "Saved filter '{}' in *{}*!".format(keyword, chat_name), parse_mode=telegram.ParseMode.MARKDOWN, ) raise DispatcherHandlerStop