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 = tld(chat.id, "cust_filters_local") else: owner = chat.get_member(user.id) chat.title = chat.title if owner.status != 'creator': message.reply_text(tld(chat.id, "notes_must_be_creator")) return x = 0 flist = sql.get_chat_triggers(chat.id) if not flist: message.reply_text( tld(chat.id, "cust_filters_list_empty").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(tld(chat.id, "cust_filters_cleanup_success").format(x))
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 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 @HarukaAyaGroup 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 " "@HarukaAyaGroup 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 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 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, "yO, I'll stop replying for 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 stop_all_filters(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user message = update.effective_message if chat.type == "private": pass else: owner = chat.get_member(user.id) if owner.status != 'creator': message.reply_text(tld(chat.id, "notes_must_be_creator")) return filters_list = sql.get_chat_triggers(chat.id) x = 0 for filter in filters_list: x += 1 sql.remove_filter(chat.id, filter) message.reply_text(tld(chat.id, "cust_filters_cleanup_success").format(x))
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 stop_filter(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user args = update.effective_message.text.split(None, 1) 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": chat_name = tld(chat.id, "cust_filters_local") 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, "cust_filters_list_empty").format(chat_name)) return for keyword in chat_filters: if keyword == args[1].lower(): sql.remove_filter(chat_id, args[1].lower()) update.effective_message.reply_text( tld(chat.id, "cust_filters_stop_success").format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop update.effective_message.reply_text( tld(chat.id, "cust_filters_err_wrong_filter"))
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))
def reply_filter(bot: Bot, update: Update): chat = update.effective_chat message = update.effective_message if 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): filt = sql.get_filter(chat.id, keyword) if filt.is_sticker: message.reply_sticker(filt.reply) elif filt.is_document: try: message.reply_document(filt.reply) except Exception: print("L") 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: try: message.reply_video(filt.reply) except Exception: print("Nut") 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( tld(chat.id, "cust_filters_err_protocol")) 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: try: message.reply_text( tld(chat.id, "cust_filters_err_badformat")) 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)) except Exception: print("Nut") else: # LEGACY - all new filters will have has_markdown set to True. message.reply_text(filt.reply) break
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 export_data(bot: Bot, update: Update, chat_data): msg = update.effective_message # type: Optional[Message] user = update.effective_user # type: Optional[User] spam = spamfilters(update.effective_message.text, update.effective_message.from_user.id, update.effective_chat.id, update.effective_message) if spam == True: return chat_id = update.effective_chat.id chat = update.effective_chat current_chat_id = update.effective_chat.id 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, tld(update.effective_message, "You can use this command in group and not in PM")) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title jam = time.time() new_jam = jam + 43200 cek = get_chat(chat_id, chat_data) if cek.get('status'): if jam <= int(cek.get('value')): waktu = time.strftime("%H:%M:%S %d/%m/%Y", time.localtime(cek.get('value'))) send_message(update.effective_message, tld(update.effective_message, "You can back up your data once in 12 hours!\n[This Person](tg://user?id={}) already backed up.You can back up data again on `{}`").format(cek.get('user'), waktu), parse_mode=ParseMode.MARKDOWN) return else: if user.id != OWNER_ID: put_chat(chat_id, user.id, new_jam, chat_data) else: if user.id != OWNER_ID: put_chat(chat_id, user.id, new_jam, chat_data) # Backup version # Revision: 07/07/2019 backup_ver = 1 bot_base = "Kanna" # Make sure this backup is for this bot bot_id = bot.id # Backuping antiflood flood_mode, flood_duration = antifloodsql.get_flood_setting(chat_id) flood_limit = antifloodsql.get_flood_limit(chat_id) antiflood = {'flood_mode': flood_mode, 'flood_duration': flood_duration, 'flood_limit': flood_limit} # Backuping blacklists all_blacklisted = blacklistsql.get_chat_blacklist(chat_id) blacklist_mode, blacklist_duration = blacklistsql.get_blacklist_setting(chat.id) blacklists = {'blacklist_mode': blacklist_mode, 'blacklist_duration': blacklist_duration, 'blacklists': all_blacklisted} # Backuping blacklists sticker # Backuping disabled cmd_disabled = disabledsql.get_all_disabled(chat_id) disabled = {'disabled': cmd_disabled} # Backuping filters all_filters = filtersql.get_chat_triggers(chat_id) filters_gen = [] for x in all_filters: filt = filtersql.get_filter(chat.id, x) if filt.is_sticker: filt_type = 1 elif filt.is_document: filt_type = 2 elif filt.is_image: filt_type = 3 elif filt.is_audio: filt_type = 4 elif filt.is_voice: filt_type = 5 elif filt.is_video: filt_type = 6 elif filt.has_markdown: filt_type = 0 else: filt_type = 7 filters_gen.append({"name": x, "reply": filt.reply, "type": filt_type}) filters = {'filters': filters_gen} # Backuping greetings msg and config # Backuping locks curr_locks = locksql.get_locks(chat_id) curr_restr = locksql.get_restr(chat_id) if curr_locks: locked_lock = { "sticker": curr_locks.sticker, "audio": curr_locks.audio, "voice": curr_locks.voice, "document": curr_locks.document, "video": curr_locks.video, "contact": curr_locks.contact, "photo": curr_locks.photo, "gif": curr_locks.gif, "url": curr_locks.url, "bots": curr_locks.bots, "forward": curr_locks.forward, "game": curr_locks.game, "location": curr_locks.location, } else: locked_lock = {} if curr_restr: locked_restr = { "messages": curr_restr.messages, "media": curr_restr.media, "other": curr_restr.other, "previews": curr_restr.preview, "all": all([curr_restr.messages, curr_restr.media, curr_restr.other, curr_restr.preview]) } else: locked_restr = {} lock_warn = locksql.get_lockconf(chat_id) locks = {'lock_warn': lock_warn, 'locks': locked_lock, 'restrict': locked_restr} # Backuping notes note_list = notesql.get_all_chat_notes(chat_id) notes = [] for note in note_list: buttonlist = "" note_tag = note.name note_type = note.msgtype getnote = notesql.get_note(chat_id, note.name) if not note.value: note_data = "" else: tombol = notesql.get_buttons(chat_id, note_tag) keyb = [] buttonlist = "" for btn in tombol: if btn.same_line: buttonlist += "[{}](buttonurl:{}:same)\n".format(btn.name, btn.url) else: buttonlist += "[{}](buttonurl:{})\n".format(btn.name, btn.url) note_data = "{}\n\n{}".format(note.value, buttonlist) note_file = note.file if not note_file: note_file = "" notes.append({"note_tag": note_tag, "note_data": note_data, "note_file": note_file, "note_type": note_type}) # Backuping reports get_report = reportsql.user_should_report(chat_id) report = {'report': get_report} # Backuping rules getrules = rulessql.get_rules(chat_id) rules = {"rules": getrules} # Backuping warns config and warn filters # Parsing backups backup = {"bot_id": bot_id, "bot_base": bot_base, "antiflood": antiflood, "blacklists": blacklists, "disabled": disabled, "filters": filters, "locks": locks, "notes": notes, "report": report, "rules": rules, "version": backup_ver} all_backups = json.dumps(backup, indent=4, cls=SetEncoder) f = open("{}-Kanna.backup".format(chat_id), "w") f.write(str(all_backups)) f.close() bot.sendChatAction(current_chat_id, "upload_document") tgl = time.strftime("%H:%M:%S - %d/%m/%Y", time.localtime(time.time())) try: bot.sendMessage(TEMPORARY_DATA, "*Successfully backed up for:*\nChat Name: `{}`\nChat ID: `{}`\nOn: `{}`".format(chat.title, chat_id, tgl), parse_mode=ParseMode.MARKDOWN) except BadRequest: pass send = bot.sendDocument(current_chat_id, document=open('{}-bot.backup'.format(chat_id), 'rb'), caption=tld(update.effective_message, "*Successfully backed up for:*\nChat Name: `{}`\nChat ID: `{}`\nOn: `{}`\n\nNote: This backup is specific to this bot.").format(chat.title, chat_id, tgl), timeout=360, reply_to_message_id=msg.message_id, parse_mode=ParseMode.MARKDOWN) try: # Send to temp data for prevent unexpected issue bot.sendDocument(TEMPORARY_DATA, document=send.document.file_id, caption=tld(update.effective_message, "*Successfully backed up for:*\nChat Name: `{}`\nVhat ID: `{}`\nOn: `{}`\n\nNote: This backup is specific to this bo").format(chat.title, chat_id, tgl), timeout=360, parse_mode=ParseMode.MARKDOWN) except BadRequest: pass os.remove("{}-bot.backup".format(chat_id)) # Cleaning file