From da294f4fc51629a45c34471ee6dc2544d5bd56e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:13:44 +0530 Subject: [PATCH 01/12] Update __main__.py --- tg_bot/__main__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tg_bot/__main__.py b/tg_bot/__main__.py index 884551d..1e9423a 100644 --- a/tg_bot/__main__.py +++ b/tg_bot/__main__.py @@ -352,6 +352,24 @@ def get_settings(bot: Bot, update: Update): else: send_settings(chat.id, user.id, True) +@run_async +def kcfrsct_fnc(bot: Bot, update: Update): + query = update.callback_query + user = update.effective_user + _match = re.match(r"rsct_(.*)_33801", query.data) + # ensure no spinny white circle + if _match: + try: + from tg_bot.modules.sql.cust_filters_sql import get_btn_with_di + _soqka = get_btn_with_di(int(_match.group(1))) + query.answer( + text=_soqka.url.replace("\\n", "\n").replace("\\t", "\t"), + # HPFPOCWBANER: https://stackoverflow.com/a/42965750 + show_alert=True + ) + except Exception as e: + print(e) + bot.answer_callback_query(query.id) @run_async def donate(bot: Bot, update: Update): @@ -415,6 +433,9 @@ def main(): dispatcher.add_handler(settings_callback_handler) dispatcher.add_handler(migrate_handler) dispatcher.add_handler(donate_handler) + dispatcher.add_handler( + CallbackQueryHandler(kcfrsct_fnc, pattern=r"") + ) # dispatcher.add_error_handler(error_callback) From dcea5b98393f3691049f441da7d0f8e2c9b2380a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:16:48 +0530 Subject: [PATCH 02/12] Update misc.py --- tg_bot/modules/helper_funcs/misc.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tg_bot/modules/helper_funcs/misc.py b/tg_bot/modules/helper_funcs/misc.py index bb24600..1bb1147 100644 --- a/tg_bot/modules/helper_funcs/misc.py +++ b/tg_bot/modules/helper_funcs/misc.py @@ -86,11 +86,17 @@ def send_to_list(bot: Bot, send_to: list, message: str, markdown=False, html=Fal def build_keyboard(buttons): keyb = [] for btn in buttons: - if btn.same_line and keyb: - keyb[-1].append(InlineKeyboardButton(btn.name, url=btn.url)) + mybelru = btn.url + ik = None + if mybelru.startswith(("http", "tg://")): + ik = InlineKeyboardButton(btn.name, url=mybelru) else: - keyb.append([InlineKeyboardButton(btn.name, url=btn.url)]) - + ik = InlineKeyboardButton(btn.name, callback_data=f"rsct_{btn.id}_33801") + if ik: + if btn.same_line and keyb: + keyb[-1].append(ik) + else: + keyb.append([ik]) return keyb From eff85f8022451be21b39328cc9959283739f460e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:18:57 +0530 Subject: [PATCH 03/12] Update cust_filters_sql.py --- tg_bot/modules/sql/cust_filters_sql.py | 27 +++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tg_bot/modules/sql/cust_filters_sql.py b/tg_bot/modules/sql/cust_filters_sql.py index 1b888b9..dfefe41 100644 --- a/tg_bot/modules/sql/cust_filters_sql.py +++ b/tg_bot/modules/sql/cust_filters_sql.py @@ -16,10 +16,14 @@ class CustomFilters(BASE): is_audio = Column(Boolean, nullable=False, default=False) is_voice = Column(Boolean, nullable=False, default=False) is_video = Column(Boolean, nullable=False, default=False) + caption = Column(UnicodeText, nullable=True, default=None) has_buttons = Column(Boolean, nullable=False, default=False) # NOTE: Here for legacy purposes, to ensure older filters don't mess up. has_markdown = Column(Boolean, nullable=False, default=False) + # NOTE: Here for -_- purposes, + has_caption = Column(Boolean, nullable=False, default=False) + def __init__(self, chat_id, keyword, reply, is_sticker=False, is_document=False, is_image=False, is_audio=False, is_voice=False, is_video=False, has_buttons=False): @@ -35,6 +39,7 @@ def __init__(self, chat_id, keyword, reply, is_sticker=False, is_document=False, self.has_buttons = has_buttons self.has_markdown = True + def __repr__(self): return "" % self.chat_id @@ -69,6 +74,15 @@ def __init__(self, chat_id, keyword, name, url, same_line=False): CHAT_FILTERS = {} +def get_btn_with_di(ntb_gtid): + try: + return SESSION.query(Buttons).filter( + Buttons.id == ntb_gtid + ).one() + finally: + SESSION.close() + + def get_all_filters(): try: return SESSION.query(CustomFilters).all() @@ -77,7 +91,7 @@ def get_all_filters(): def add_filter(chat_id, keyword, reply, is_sticker=False, is_document=False, is_image=False, is_audio=False, - is_voice=False, is_video=False, buttons=None): + is_voice=False, is_video=False, buttons=None, caption=None, has_caption=False): global CHAT_FILTERS if buttons is None: @@ -95,6 +109,9 @@ def add_filter(chat_id, keyword, reply, is_sticker=False, is_document=False, is_ filt = CustomFilters(str(chat_id), keyword, reply, is_sticker, is_document, is_image, is_audio, is_voice, is_video, bool(buttons)) + if has_caption: + filt.caption = caption + filt.has_caption = has_caption if keyword not in CHAT_FILTERS.get(str(chat_id), []): CHAT_FILTERS[str(chat_id)] = sorted(CHAT_FILTERS.get(str(chat_id), []) + [keyword], @@ -169,6 +186,14 @@ def num_filters(): finally: SESSION.close() +def num_filters_per_chat(chat_id): + try: + return SESSION.query(CustomFilters).filter( + CustomFilters.chat_id == str(chat_id) + ).count() + finally: + SESSION.close() + def num_chats(): try: From 2a04757b0819e266943dc1b76971043e67658cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:22:43 +0530 Subject: [PATCH 04/12] Update cust_filters.py --- tg_bot/modules/cust_filters.py | 71 +++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/tg_bot/modules/cust_filters.py b/tg_bot/modules/cust_filters.py index 032f51e..dd0ec3d 100644 --- a/tg_bot/modules/cust_filters.py +++ b/tg_bot/modules/cust_filters.py @@ -32,7 +32,7 @@ def list_handlers(bot: Bot, update: Update): if not conn == False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title - filter_list = "*Filters in {}:*\n" + filter_list = f"*Filters in {chat_name}:*\n" else: chat_id = update.effective_chat.id if chat.type == "private": @@ -40,8 +40,10 @@ def list_handlers(bot: Bot, update: Update): filter_list = "*local filters:*\n" else: chat_name = chat.title - filter_list = "*Filters in {}*:\n" + filter_list = "*Filters in {}*:\n".format(chat_name) + total_count_f_fliters = sql.num_filters_per_chat(chat_id) + filter_list += f"**Filter Count**: {total_count_f_fliters}\n" all_handlers = sql.get_chat_triggers(chat_id) @@ -83,6 +85,7 @@ def filters(bot: Bot, update: Update): if len(args) < 2: return + extracted = split_quotes(args[1]) if len(extracted) < 1: return @@ -95,6 +98,9 @@ def filters(bot: Bot, update: Update): is_voice = False is_audio = False is_video = False + media_caption = None + has_caption = False + content = None # :\ buttons = [] # determine what the contents of the filter are - text, image, sticker, etc @@ -102,40 +108,60 @@ def filters(bot: Bot, update: Update): offset = len(extracted[1]) - len(msg.text) # set correct offset relative to command + notename content, buttons = button_markdown_parser(extracted[1], entities=msg.parse_entities(), offset=offset) content = content.strip() - if not content: - msg.reply_text("There is no note message - You can't JUST have buttons, you need a message to go with it!") - return + # https://t.me/c/1279877202/3508 - elif msg.reply_to_message and msg.reply_to_message.sticker: + if msg.reply_to_message and msg.reply_to_message.sticker: content = msg.reply_to_message.sticker.file_id is_sticker = True + # stickers don't have caption in BOT API -_- elif msg.reply_to_message and msg.reply_to_message.document: + offset = len(msg.reply_to_message.caption or "") + media_caption, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=offset) content = msg.reply_to_message.document.file_id is_document = True + has_caption = True elif msg.reply_to_message and msg.reply_to_message.photo: - offset = len(msg.reply_to_message.caption) - ignore_underscore_case, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=offset) + offset = len(msg.reply_to_message.caption or "") + media_caption, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=offset) content = msg.reply_to_message.photo[-1].file_id # last elem = best quality is_image = True + has_caption = True elif msg.reply_to_message and msg.reply_to_message.audio: + offset = len(msg.reply_to_message.caption or "") + media_caption, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=offset) content = msg.reply_to_message.audio.file_id is_audio = True + has_caption = True elif msg.reply_to_message and msg.reply_to_message.voice: + offset = len(msg.reply_to_message.caption or "") + media_caption, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=offset) content = msg.reply_to_message.voice.file_id is_voice = True + has_caption = True elif msg.reply_to_message and msg.reply_to_message.video: + offset = len(msg.reply_to_message.caption or "") + media_caption, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=offset) content = msg.reply_to_message.video.file_id is_video = True + has_caption = True - else: - msg.reply_text("You didn't specify what to reply with!") + elif msg.reply_to_message and msg.reply_to_message.text: + content = msg.reply_to_message.text + + elif not content: + # msg.reply_text("You didn't specify what to reply with!") + msg.reply_text("There is no note message - You can't JUST have buttons, you need a message to go with it!") return + # print(media_caption) + # print(buttons) + # print(content) + # Add the filter # Note: perhaps handlers can be removed somehow using sql.get_chat_filters for handler in dispatcher.handlers.get(HANDLER_GROUP, []): @@ -143,7 +169,7 @@ def filters(bot: Bot, update: Update): dispatcher.remove_handler(handler, HANDLER_GROUP) sql.add_filter(chat_id, keyword, content, is_sticker, is_document, is_image, is_audio, is_voice, is_video, - buttons) + buttons, media_caption, has_caption) msg.reply_text("Handler '{}' added in *{}*!".format(keyword, chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop @@ -205,23 +231,22 @@ def reply_filter(bot: Bot, update: Update): 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 "" + keyboard = None + if len(buttons) > 0: + keyboard = InlineKeyboardMarkup(build_keyboard(buttons)) if filt.is_sticker: - message.reply_sticker(filt.reply) + message.reply_sticker(filt.reply, reply_markup=keyboard) elif filt.is_document: - message.reply_document(filt.reply) + message.reply_document(filt.reply, caption=media_caption, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard) elif filt.is_image: - if len(buttons) > 0: - keyb = build_keyboard(buttons) - keyboard = InlineKeyboardMarkup(keyb) - message.reply_photo(filt.reply, reply_markup=keyboard) - else: - message.reply_photo(filt.reply) + message.reply_photo(filt.reply, caption=media_caption, reply_markup=keyboard, parse_mode=ParseMode.MARKDOWN) elif filt.is_audio: - message.reply_audio(filt.reply) + message.reply_audio(filt.reply, caption=media_caption, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard) elif filt.is_voice: - message.reply_voice(filt.reply) + message.reply_voice(filt.reply, caption=media_caption, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard) elif filt.is_video: - message.reply_video(filt.reply) + message.reply_video(filt.reply, caption=media_caption, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard) elif filt.has_markdown: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) @@ -270,6 +295,7 @@ def __chat_settings__(chat_id, user_id): __help__ = """ - /filters: list all active filters in this chat. + *Admin only:* - /filter : add a filter to this chat. The bot will now reply that message whenever 'keyword'\ is mentioned. If you reply to a sticker with a keyword, the bot will reply with that sticker. NOTE: all filter \ @@ -288,3 +314,4 @@ def __chat_settings__(chat_id, user_id): dispatcher.add_handler(FILTER_HANDLER) dispatcher.add_handler(STOP_HANDLER) dispatcher.add_handler(LIST_HANDLER) +dispatcher.add_handler(CUST_FILTER_HANDLER, HANDLER_GROUP) From 7f6faaccb27413a906667860d3bc8640958999e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:23:55 +0530 Subject: [PATCH 05/12] Update filters.py --- tg_bot/modules/helper_funcs/filters.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tg_bot/modules/helper_funcs/filters.py b/tg_bot/modules/helper_funcs/filters.py index 3f14b12..db479b8 100644 --- a/tg_bot/modules/helper_funcs/filters.py +++ b/tg_bot/modules/helper_funcs/filters.py @@ -32,3 +32,10 @@ def filter(self, message: Message): return bool(message.text or message.sticker or message.photo or message.document or message.video) has_text = _HasText() + + class _HasEntities(BaseFilter): + def filter(self, message: Message): + return bool(message.text and message.entities and len(message.entities) >= 2) + + has_entities = _HasEntities() + From 90ddc47fbcbc7542858f50c6b6d7423398c320f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:25:25 +0530 Subject: [PATCH 06/12] Update string_handling.py --- tg_bot/modules/helper_funcs/string_handling.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tg_bot/modules/helper_funcs/string_handling.py b/tg_bot/modules/helper_funcs/string_handling.py index b7cec77..c2c75a7 100644 --- a/tg_bot/modules/helper_funcs/string_handling.py +++ b/tg_bot/modules/helper_funcs/string_handling.py @@ -145,6 +145,8 @@ def button_markdown_parser(txt: str, entities: Dict[MessageEntity, str] = None, def escape_invalid_curly_brackets(text: str, valids: List[str]) -> str: + if not text: + return text new_text = "" idx = 0 while idx < len(text): @@ -240,7 +242,7 @@ def extract_time(message, time_val): unit = time_val[-1] time_num = time_val[:-1] # type: str if not time_num.isdigit(): - message.reply_text("Invalid time amount specified.") + message.reply_text("അസാധുവായ സമയ തുക വ്യക്തമാക്കി.") return "" if unit == 'm': @@ -248,11 +250,13 @@ def extract_time(message, time_val): elif unit == 'h': bantime = int(time.time() + int(time_num) * 60 * 60) elif unit == 'd': + if time_num > 365: + time_num = 365 bantime = int(time.time() + int(time_num) * 24 * 60 * 60) else: # how even...? return "" return bantime else: - message.reply_text("Invalid time type specified. Expected m,h, or d, got: {}".format(time_val[-1])) + message.reply_text("അസാധുവായ സമയ തരം വ്യക്തമാക്കി. പ്രതീക്ഷിച്ചതു m,h, or d, കിട്ടിയത്: {}".format(time_val[-1])) return "" From dc900392a514ee15903a3e2dc1e423a20d61a7db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:26:46 +0530 Subject: [PATCH 07/12] Update msg_types.py --- tg_bot/modules/helper_funcs/msg_types.py | 31 ++++++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/tg_bot/modules/helper_funcs/msg_types.py b/tg_bot/modules/helper_funcs/msg_types.py index e47de2e..c5f9054 100644 --- a/tg_bot/modules/helper_funcs/msg_types.py +++ b/tg_bot/modules/helper_funcs/msg_types.py @@ -15,6 +15,7 @@ class Types(IntEnum): AUDIO = 5 VOICE = 6 VIDEO = 7 + VIDEO_NOTE = 8 def get_note_type(msg: Message): @@ -37,7 +38,7 @@ def get_note_type(msg: Message): data_type = Types.TEXT elif msg.reply_to_message: - entities = msg.reply_to_message.parse_entities() + entities = msg.reply_to_message.parse_entities() or msg.reply_to_message.parse_caption_entities() msgtext = msg.reply_to_message.text or msg.reply_to_message.caption if len(args) >= 2 and msg.reply_to_message.text: # not caption, text text, buttons = button_markdown_parser(msgtext, @@ -76,6 +77,11 @@ def get_note_type(msg: Message): text, buttons = button_markdown_parser(msgtext, entities=entities) data_type = Types.VIDEO + elif msg.reply_to_message.video_note: + content = msg.reply_to_message.video_note.file_id + text, buttons = button_markdown_parser(msgtext, entities=entities) + data_type = Types.VIDEO_NOTE + return note_name, text, data_type, content, buttons @@ -89,7 +95,8 @@ def get_welcome_type(msg: Message): buttons = [] # determine what the contents of the filter are - text, image, sticker, etc - if len(args) >= 2: + # some media, cannot have captions in the Telegram BOT API + if len(args) >= 2 and not msg.reply_to_message: offset = len(args[1]) - len(msg.text) # set correct offset relative to command + notename text, buttons = button_markdown_parser(args[1], entities=msg.parse_entities(), offset=offset) if buttons: @@ -99,32 +106,40 @@ def get_welcome_type(msg: Message): elif msg.reply_to_message and msg.reply_to_message.sticker: content = msg.reply_to_message.sticker.file_id - text = msg.reply_to_message.text + text, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=0) data_type = Types.STICKER elif msg.reply_to_message and msg.reply_to_message.document: content = msg.reply_to_message.document.file_id - text = msg.reply_to_message.text + text, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=0) data_type = Types.DOCUMENT elif msg.reply_to_message and msg.reply_to_message.photo: content = msg.reply_to_message.photo[-1].file_id # last elem = best quality - text = msg.reply_to_message.text + text, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=0) data_type = Types.PHOTO elif msg.reply_to_message and msg.reply_to_message.audio: content = msg.reply_to_message.audio.file_id - text = msg.reply_to_message.text + text, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=0) data_type = Types.AUDIO elif msg.reply_to_message and msg.reply_to_message.voice: content = msg.reply_to_message.voice.file_id - text = msg.reply_to_message.text + text, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=0) data_type = Types.VOICE elif msg.reply_to_message and msg.reply_to_message.video: content = msg.reply_to_message.video.file_id - text = msg.reply_to_message.text + text, buttons = button_markdown_parser(msg.reply_to_message.caption, entities=msg.reply_to_message.parse_entities(), offset=0) data_type = Types.VIDEO + elif msg.reply_to_message.video_note: + msgtext = "" + if len(args) > 1: + msgtext = args[1] + content = msg.reply_to_message.video_note.file_id + text, buttons = button_markdown_parser(msgtext, entities=msg.reply_to_message.parse_caption_entities(), offset=0) + data_type = Types.VIDEO_NOTE + return text, data_type, content, buttons From becb9e89a63376c29247817849b72ee42e3fe864 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:28:23 +0530 Subject: [PATCH 08/12] Update extraction.py --- tg_bot/modules/helper_funcs/extraction.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tg_bot/modules/helper_funcs/extraction.py b/tg_bot/modules/helper_funcs/extraction.py index ce21814..5752adc 100644 --- a/tg_bot/modules/helper_funcs/extraction.py +++ b/tg_bot/modules/helper_funcs/extraction.py @@ -49,8 +49,7 @@ def extract_user_and_text(message: Message, args: List[str]) -> (Optional[int], user = args[0] user_id = get_user_id(user) if not user_id: - message.reply_text("I don't have that user in my db. You'll be able to interact with them if " - "you reply to that person's message instead, or forward one of that user's messages.") + message.reply_text("ഞാൻ ഇങ്ങനെയൊരാളെ ഇവിടെയെങ്ങും കണ്ടിട്ടേയില്ല... ഇയാളുടെ വാലോ തലയോ എന്തെങ്കിലും എനിക്ക് ഒന്ന് അയച്ചു താ (മെസ്സേജ് ആയാലും മതി)... എന്നിട്ട് വേണം വേഗം പണി തുടങ്ങാൻ...") return None, None else: @@ -75,9 +74,7 @@ def extract_user_and_text(message: Message, args: List[str]) -> (Optional[int], message.bot.get_chat(user_id) except BadRequest as excp: if excp.message in ("User_id_invalid", "Chat not found"): - message.reply_text("I don't seem to have interacted with this user before - please forward a message from " - "them to give me control! (like a voodoo doll, I need a piece of them to be able " - "to execute certain commands...)") + message.reply_text("ഞാൻ ഇങ്ങനെയൊരാളെ ഇവിടെയെങ്ങും കണ്ടിട്ടേയില്ല... ഇയാളുടെ വാലോ തലയോ എന്തെങ്കിലും എനിക്ക് ഒന്ന് അയച്ചു താ (മെസ്സേജ് ആയാലും മതി)... എന്നിട്ട് വേണം വേഗം പണി തുടങ്ങാൻ...") else: LOGGER.exception("Exception %s on user %s", excp.message, user_id) From ed850b003e734a289cd7d4bea333050a67a61186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:32:40 +0530 Subject: [PATCH 09/12] Update chat_status.py --- tg_bot/modules/helper_funcs/chat_status.py | 280 ++++++--------------- 1 file changed, 74 insertions(+), 206 deletions(-) diff --git a/tg_bot/modules/helper_funcs/chat_status.py b/tg_bot/modules/helper_funcs/chat_status.py index 830a93c..f1cf6a2 100644 --- a/tg_bot/modules/helper_funcs/chat_status.py +++ b/tg_bot/modules/helper_funcs/chat_status.py @@ -1,293 +1,161 @@ from functools import wraps +from typing import Optional -from telegram import Bot, Chat, ChatMember, Update, User, ParseMode +from telegram import User, Chat, ChatMember, Update, Bot -from tg_bot import dispatcher, DEL_CMDS, WHITELIST_USERS, SUPPORT_USERS, SUDO_USERS +from tg_bot import DEL_CMDS, SUDO_USERS, WHITELIST_USERS -def is_whitelist_plus(chat: Chat, user_id: int, member: ChatMember = None) -> bool: - return user_id in WHITELIST_USERS or user_id in SUPPORT_USERS or user_id in SUDO_USERS +_TELE_GRAM_ID_S = [777000, 7351948, 861055237] -def is_support_plus(chat: Chat, user_id: int, member: ChatMember = None) -> bool: - return user_id in SUPPORT_USERS or user_id in SUDO_USERS - -def is_sudo_plus(chat: Chat, user_id: int, member: ChatMember = None) -> bool: - return user_id in SUDO_USERS +def can_delete(chat: Chat, bot_id: int) -> bool: + return chat.get_member(bot_id).can_delete_messages -def is_user_admin(chat: Chat, user_id: int, member: ChatMember = None) -> bool: +def is_user_ban_protected(chat: Chat, user_id: int, member: ChatMember = None) -> bool: + if user_id in _TELE_GRAM_ID_S: + return True if chat.type == 'private' \ or user_id in SUDO_USERS \ + or user_id in WHITELIST_USERS \ or chat.all_members_are_administrators: return True if not member: member = chat.get_member(user_id) - return member.status in ('administrator', 'creator') -def is_bot_admin(chat: Chat, bot_id: int, bot_member: ChatMember = None) -> bool: - - if chat.type == 'private' or chat.all_members_are_administrators: +def is_user_admin(chat: Chat, user_id: int, member: ChatMember = None) -> bool: + if user_id in _TELE_GRAM_ID_S: return True - if not bot_member: - bot_member = chat.get_member(bot_id) - - return bot_member.status in ('administrator', 'creator') - - -def can_delete(chat: Chat, bot_id: int) -> bool: - return chat.get_member(bot_id).can_delete_messages - - -def is_user_ban_protected(chat: Chat, user_id: int, member: ChatMember = None) -> bool: - if chat.type == 'private' \ or user_id in SUDO_USERS \ - or user_id in WHITELIST_USERS \ or chat.all_members_are_administrators: return True if not member: member = chat.get_member(user_id) - return member.status in ('administrator', 'creator') -def is_user_in_chat(chat: Chat, user_id: int) -> bool: - - member = chat.get_member(user_id) - return member.status not in ('left', 'kicked') - - -def sudo_plus(func): - @wraps(func) - def is_sudo_plus_func(bot: Bot, update: Update, *args, **kwargs): - - user = update.effective_user - chat = update.effective_chat - - if user and is_sudo_plus(chat, user.id): - return func(bot, update, *args, **kwargs) - elif not user: - pass - elif DEL_CMDS and " " not in update.effective_message.text: - update.effective_message.delete() - else: - update.effective_message.reply_text("Who dis non-admin telling me what to do?") - - return is_sudo_plus_func +def is_bot_admin(chat: Chat, bot_id: int, bot_member: ChatMember = None) -> bool: + if chat.type == 'private' \ + or chat.all_members_are_administrators: + return True -def support_plus(func): - @wraps(func) - def is_support_plus_func(bot: Bot, update: Update, *args, **kwargs): - - user = update.effective_user - chat = update.effective_chat + if not bot_member: + bot_member = chat.get_member(bot_id) + return bot_member.status in ('administrator', 'creator') - if user and is_whitelist_plus(chat, user.id): - return func(bot, update, *args, **kwargs) - elif DEL_CMDS and " " not in update.effective_message.text: - update.effective_message.delete() - return is_support_plus_func +def is_user_in_chat(chat: Chat, user_id: int) -> bool: + member = chat.get_member(user_id) + return member.status not in ('left', 'kicked') -def whitelist_plus(func): +def bot_can_delete(func): @wraps(func) - def is_whitelist_plus_func(bot: Bot, update: Update, *args, **kwargs): - - user = update.effective_user - chat = update.effective_chat - - if user and is_whitelist_plus(chat, user.id): + def delete_rights(bot: Bot, update: Update, *args, **kwargs): + if can_delete(update.effective_chat, bot.id): return func(bot, update, *args, **kwargs) else: - update.effective_message.reply_text("You don't have access to use this.") + update.effective_message.reply_text("എനിക്ക് ഇവിടെ സന്ദേശങ്ങൾ ഇല്ലാതാക്കാൻ കഴിയില്ല! " + "ഞാൻ അഡ്മിൻ ആണെന്ന് ഉറപ്പാക്കുക, മറ്റ് ഉപയോക്താവിന്റെ സന്ദേശങ്ങൾ ഇല്ലാതാക്കാൻ എനിക്ക് അനുമതിയുണ്ടെന്ന് ഉറപ്പാക്കുക.") - return is_whitelist_plus_func + return delete_rights -def user_admin(func): +def can_pin(func): @wraps(func) - def is_admin(bot: Bot, update: Update, *args, **kwargs): - - user = update.effective_user - chat = update.effective_chat - - if user and is_user_admin(chat, user.id): + def pin_rights(bot: Bot, update: Update, *args, **kwargs): + if update.effective_chat.get_member(bot.id).can_pin_messages: return func(bot, update, *args, **kwargs) - elif not user: - pass - elif DEL_CMDS and " " not in update.effective_message.text: - update.effective_message.delete() else: - update.effective_message.reply_text("You don't have access to use this.") + update.effective_message.reply_text("എനിക്ക് സന്ദേശങ്ങൾ ഇവിടെ പിൻ ചെയ്യാനാവില്ല! " + "ഞാൻ അഡ്മിൻ ആണെന്ന് ഉറപ്പാക്കുക, സന്ദേശങ്ങൾ പിൻ ചെയ്യാനുള്ള അനുമതി എനിക്ക് ഉണ്ടെന്ന് ഉറപ്പാക്കുക.") - return is_admin + return pin_rights -def user_admin_no_reply(func): +def can_promote(func): @wraps(func) - def is_not_admin_no_reply(bot: Bot, update: Update, *args, **kwargs): - - user = update.effective_user - chat = update.effective_chat - - if user and is_user_admin(chat, user.id): + def promote_rights(bot: Bot, update: Update, *args, **kwargs): + if update.effective_chat.get_member(bot.id).can_promote_members: return func(bot, update, *args, **kwargs) - elif not user: - pass - elif DEL_CMDS and " " not in update.effective_message.text: - update.effective_message.delete() + else: + update.effective_message.reply_text("എനിക്ക് ആളുകളെ പ്രോത്സാഹിപ്പിക്കാനോ / പ്രകടിപ്പിക്കാനോ കഴിയില്ല! " + "ഞാൻ അഡ്മിനാണെന്നും പുതിയ അഡ്മിനുകളേ എനിക്ക് നിയമിക്കാൻ കഴിയുമെന്നും ഉറപ്പാക്കുക.") - return is_not_admin_no_reply + return promote_rights -def user_not_admin(func): +def can_restrict(func): @wraps(func) - def is_not_admin(bot: Bot, update: Update, *args, **kwargs): - - user = update.effective_user - chat = update.effective_chat - - if user and not is_user_admin(chat, user.id): + def promote_rights(bot: Bot, update: Update, *args, **kwargs): + if update.effective_chat.get_member(bot.id).can_restrict_members: return func(bot, update, *args, **kwargs) - elif not user: - pass + else: + update.effective_message.reply_text("എനിക്ക് ഇവിടെ ആളുകളെ നിയന്ത്രിക്കാനാവില്ല! " + "ഞാൻ അഡ്മിനാണെന്നും പുതിയ അഡ്മിനുകളേ എനിക്ക് നിയമിക്കാൻ കഴിയുമെന്നും ഉറപ്പാക്കുക.") - return is_not_admin + return promote_rights def bot_admin(func): @wraps(func) def is_admin(bot: Bot, update: Update, *args, **kwargs): - - chat = update.effective_chat - update_chat_title = chat.title - message_chat_title = update.effective_message.chat.title - - if update_chat_title == message_chat_title: - not_admin = "I'm not admin! " - else: - not_admin = f"I'm not admin in {update_chat_title}! " - - if is_bot_admin(chat, bot.id): + if is_bot_admin(update.effective_chat, bot.id): return func(bot, update, *args, **kwargs) else: - update.effective_message.reply_text(not_admin, parse_mode=ParseMode.HTML) + update.effective_message.reply_text("ഞാൻ അഡ്മിനല്ല!") return is_admin -def bot_can_delete(func): +def user_admin(func): @wraps(func) - def delete_rights(bot: Bot, update: Update, *args, **kwargs): - - chat = update.effective_chat - update_chat_title = chat.title - message_chat_title = update.effective_message.chat.title - - if update_chat_title == message_chat_title: - cant_delete = f"I can't delete messages here!\nMake sure I'm admin and can delete other user's messages." - else: - cant_delete = f"I can't delete messages in {update_chat_title}!\nMake sure I'm admin and can delete other user's messages there." - - if can_delete(chat, bot.id): + def is_admin(bot: Bot, update: Update, *args, **kwargs): + user = update.effective_user # type: Optional[User] + if user and is_user_admin(update.effective_chat, user.id): return func(bot, update, *args, **kwargs) - else: - update.effective_message.reply_text(cant_delete, parse_mode=ParseMode.HTML) - return delete_rights - - -def can_pin(func): - @wraps(func) - def pin_rights(bot: Bot, update: Update, *args, **kwargs): - - chat = update.effective_chat - update_chat_title = chat.title - message_chat_title = update.effective_message.chat.title + elif not user: + pass - if update_chat_title == message_chat_title: - cant_pin = f"I can't pin messages here!\nMake sure I'm admin and can pin messages." - else: - cant_pin = f"I can't pin messages in {update_chat_title}!\nMake sure I'm admin and can pin messages there." + elif DEL_CMDS and " " not in update.effective_message.text: + update.effective_message.delete() - if chat.get_member(bot.id).can_pin_messages: - return func(bot, update, *args, **kwargs) else: - update.effective_message.reply_text(cant_pin, parse_mode=ParseMode.HTML) + update.effective_message.reply_text("ഏതാണ് ഈ മനുഷ്യൻ ഞാൻ എന്ത് ചെയ്യണം എന്ന് പറയുന്നത്?") - return pin_rights + return is_admin -def can_promote(func): +def user_admin_no_reply(func): @wraps(func) - def promote_rights(bot: Bot, update: Update, *args, **kwargs): - - chat = update.effective_chat - update_chat_title = chat.title - message_chat_title = update.effective_message.chat.title - - if update_chat_title == message_chat_title: - cant_promote = f"I can't promote/demote people here!\nMake sure I'm admin and can appoint new admins." - else: - cant_promote = f"I can't promote/demote people in {update_chat_title}!\nMake sure I'm admin there and can appoint new admins." - - if chat.get_member(bot.id).can_promote_members: + def is_admin(bot: Bot, update: Update, *args, **kwargs): + user = update.effective_user # type: Optional[User] + if user and is_user_admin(update.effective_chat, user.id): return func(bot, update, *args, **kwargs) - else: - update.effective_message.reply_text(cant_promote, parse_mode=ParseMode.HTML) - - return promote_rights - - -def can_restrict(func): - @wraps(func) - def restrict_rights(bot: Bot, update: Update, *args, **kwargs): - chat = update.effective_chat - update_chat_title = chat.title - message_chat_title = update.effective_message.chat.title - - if update_chat_title == message_chat_title: - cant_restrict = f"I can't restrict people here!\nMake sure I'm admin and can restrict users." - else: - cant_restrict = f"I can't restrict people in {update_chat_title}!\nMake sure I'm admin there and can restrict users." + elif not user: + pass - if chat.get_member(bot.id).can_restrict_members: - return func(bot, update, *args, **kwargs) - else: - update.effective_message.reply_text(cant_restrict, parse_mode=ParseMode.HTML) + elif DEL_CMDS and " " not in update.effective_message.text: + update.effective_message.delete() - return restrict_rights + return is_admin -def connection_status(func): +def user_not_admin(func): @wraps(func) - def connected_status(bot: Bot, update: Update, *args, **kwargs): - - conn = connected(bot, update, update.effective_chat, update.effective_user.id, need_admin=False) - - if conn: - chat = dispatcher.bot.getChat(conn) - update.__setattr__("_effective_chat", chat) - return func(bot, update, *args, **kwargs) - else: - if update.effective_message.chat.type == "private": - update.effective_message.reply_text("Send /connect in a group that you and I have in common first.") - return connected_status - + def is_not_admin(bot: Bot, update: Update, *args, **kwargs): + user = update.effective_user # type: Optional[User] + if user and not is_user_admin(update.effective_chat, user.id): return func(bot, update, *args, **kwargs) - return connected_status - - -#Workaround for circular import with connection.py -from tg_bot.modules import connection -connected = connection.connected + return is_not_admin From 6ee4cb5a1d47a75c5d8cb4f256383fa342540bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:52:27 +0530 Subject: [PATCH 10/12] Update app.json --- app.json | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app.json b/app.json index 7c8fa14..a84d78a 100644 --- a/app.json +++ b/app.json @@ -91,5 +91,13 @@ "version": "12" } } - ] + ], + "stack": "heroku-18", + "buildpacks": [], + "formation": { + "web": { + "quantity": 1, + "size": "free" + } + } } From bf87934ced5bb9a56e1825943a10593f01204d0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Wed, 24 Mar 2021 23:53:18 +0530 Subject: [PATCH 11/12] Update app.json --- app.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.json b/app.json index a84d78a..0d26d56 100644 --- a/app.json +++ b/app.json @@ -92,7 +92,7 @@ } } ], - "stack": "heroku-18", + "stack": "heroku-20", "buildpacks": [], "formation": { "web": { From 2399614104ac4c1cc6d34c308d1c549245fc4113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=A8=EF=BD=89=EF=BD=94=EF=BD=93=EF=BD=95=EF=BD=8B?= =?UTF-8?q?=EF=BD=81=EF=BC=88=E8=8A=8B=E3=81=B2=E3=83=A5=EF=BC=89?= <61979182+No-OnE-Kn0wS-Me@users.noreply.github.com> Date: Thu, 25 Mar 2021 13:38:12 +0530 Subject: [PATCH 12/12] Update requirements.txt --- requirements.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index fc0cb9c..1f55eca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,12 +9,11 @@ pyowm bs4 lxml demjson -PyLyrics wget gTTS #telegram -zalgo_text pynewtonmath bs4 -telegraph psutil +pillow +numpy