def welcome(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat # type: Optional[Chat] # if no args, show current replies. if len(args) == 0 or args[0].lower() == "noformat": noformat = args and args[0].lower() == "noformat" pref, welcome_m, cust_content, welcome_type = sql.get_welc_pref( chat.id) prev_welc = sql.get_clean_pref(chat.id) prev_welc = bool(prev_welc) cleanserv = sql.clean_service(chat.id) getcur, cur_value, cust_text = sql.welcome_security(chat.id) if getcur: welcsec = "True " else: welcsec = "False " if cur_value[:1] == "0": welcsec += tld(chat.id, 'welcome_mute_time_short_none') else: welcsec += tld(chat.id, 'welcome_mute_time_short').format(cur_value) text = tld(chat.id, 'welcome_settings').format(pref, prev_welc, cleanserv, welcsec, cust_text) update.effective_message.reply_text(text, parse_mode=ParseMode.MARKDOWN) if welcome_type in (sql.Types.BUTTON_TEXT, sql.Types.TEXT): buttons = sql.get_welc_buttons(chat.id) if noformat: welcome_m += revert_buttons(buttons) update.effective_message.reply_text(welcome_m) else: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) send(update, welcome_m, keyboard, sql.DEFAULT_WELCOME) else: buttons = sql.get_welc_buttons(chat.id) if noformat: welcome_m += revert_buttons(buttons) ENUM_FUNC_MAP[welcome_type](chat.id, cust_content, caption=welcome_m) else: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) ENUM_FUNC_MAP[welcome_type](chat.id, cust_content, caption=welcome_m, reply_markup=keyboard, parse_mode=ParseMode.HTML, disable_web_page_preview=True) elif len(args) >= 1: if args[0].lower() in ("on", "yes"): sql.set_welc_preference(str(chat.id), True) update.effective_message.reply_text( tld(chat.id, 'welcome_greet_set_on')) elif args[0].lower() in ("off", "no"): sql.set_welc_preference(str(chat.id), False) update.effective_message.reply_text( tld(chat.id, 'welcome_greet_set_off')) else: # idek what you're writing, say yes or no update.effective_message.reply_text( tld(chat.id, 'common_invalid_arg'))
def goodbye(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat # type: Optional[Chat] if len(args) == 0 or args[0] == "noformat": noformat = args and args[0] == "noformat" pref, goodbye_m, cust_content, goodbye_type = sql.get_gdbye_pref( chat.id) if cust_content is None: cust_content = goodbye_m update.effective_message.reply_text(tld( chat.id, 'welcome_goodbye_settings').format(pref), parse_mode=ParseMode.MARKDOWN) if goodbye_type == sql.Types.BUTTON_TEXT: buttons = sql.get_gdbye_buttons(chat.id) if noformat: goodbye_m += revert_buttons(buttons) update.effective_message.reply_text(goodbye_m) else: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) send(update, goodbye_m, keyboard, sql.DEFAULT_GOODBYE) else: buttons = sql.get_gdbye_buttons(chat.id) if noformat: goodbye_m += revert_buttons(buttons) ENUM_FUNC_MAP[goodbye_type](chat.id, cust_content, caption=goodbye_m) else: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) ENUM_FUNC_MAP[goodbye_type](chat.id, cust_content, caption=goodbye_m, reply_markup=keyboard, parse_mode=ParseMode.HTML, disable_web_page_preview=True) elif len(args) >= 1: if args[0].lower() in ("on", "yes"): sql.set_gdbye_preference(str(chat.id), True) try: update.effective_message.reply_text( tld(chat.id, 'welcome_goodbye_set_on')) except Exception: print("Nut") elif args[0].lower() in ("off", "no"): sql.set_gdbye_preference(str(chat.id), False) update.effective_message.reply_text( tld(chat.id, 'welcome_goodbye_set_off')) else: # idek what you're writing, say yes or no update.effective_message.reply_text(tld(chat.id, 'common_invalid_arg'), parse_mode=ParseMode.MARKDOWN)
def new_member(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] should_welc, cust_welcome, cust_content, welc_type = sql.get_welc_pref( chat.id) cust_welcome = markdown_to_html(cust_welcome) if should_welc: sent = None new_members = update.effective_message.new_chat_members for new_mem in new_members: # Give start information when add bot to group if is_user_gbanned(new_mem.id): return if sw is not None: sw_ban = sw.get_ban(new_mem.id) if sw_ban: return if new_mem.id == bot.id: bot.send_message( MESSAGE_DUMP, "I have been added to {} with ID: <pre>{}</pre>".format( chat.title, chat.id), parse_mode=ParseMode.HTML) bot.send_message(chat.id, tld(chat.id, 'welcome_added_to_grp')) else: if is_user_gbanned(new_mem.id): return # If welcome message is media, send with appropriate function if welc_type not in (sql.Types.TEXT, sql.Types.BUTTON_TEXT): reply = update.message.message_id cleanserv = sql.clean_service(chat.id) # Clean service welcome if cleanserv: try: dispatcher.bot.delete_message( chat.id, update.message.message_id) except BadRequest: pass reply = False # Formatting text first_name = new_mem.first_name or "PersonWithNoName" # edge case of empty name - occurs for some bugs. if new_mem.last_name: fullname = "{} {}".format(first_name, new_mem.last_name) else: fullname = first_name count = chat.get_members_count() mention = mention_html(new_mem.id, first_name) if new_mem.username: username = "******" + escape(new_mem.username) else: username = mention formatted_text = cust_welcome.format( first=escape(first_name), last=escape(new_mem.last_name or first_name), fullname=escape(fullname), username=username, mention=mention, count=count, chatname=escape(chat.title), id=new_mem.id) # Build keyboard buttons = sql.get_welc_buttons(chat.id) keyb = build_keyboard(buttons) getsec, mutetime, custom_text = sql.welcome_security( chat.id) member = chat.get_member(new_mem.id) # If user ban protected don't apply security on him if is_user_ban_protected(chat, new_mem.id, chat.get_member(new_mem.id)): pass elif getsec: # If mute time is turned on if mutetime: if mutetime[:1] == "0": if member.can_send_messages is None or member.can_send_messages: try: bot.restrict_chat_member( chat.id, new_mem.id, can_send_messages=False) canrest = True except BadRequest: canrest = False else: canrest = False else: mutetime = extract_time( update.effective_message, mutetime) if member.can_send_messages is None or member.can_send_messages: try: bot.restrict_chat_member( chat.id, new_mem.id, until_date=mutetime, can_send_messages=False) canrest = True except BadRequest: canrest = False else: canrest = False # If security welcome is turned on if canrest: sql.add_to_userlist(chat.id, new_mem.id) keyb.append([ InlineKeyboardButton( text=str(custom_text), callback_data="check_bot_({})".format( new_mem.id)) ]) keyboard = InlineKeyboardMarkup(keyb) # Send message ENUM_FUNC_MAP[welc_type](chat.id, cust_content, caption=formatted_text, reply_markup=keyboard, parse_mode="html", reply_to_message_id=reply) return # else, move on first_name = new_mem.first_name or "PersonWithNoName" # edge case of empty name - occurs for some bugs. if cust_welcome: if new_mem.last_name: fullname = "{} {}".format(first_name, new_mem.last_name) else: fullname = first_name count = chat.get_members_count() mention = mention_html(new_mem.id, first_name) if new_mem.username: username = "******" + escape(new_mem.username) else: username = mention valid_format = escape_invalid_curly_brackets( cust_welcome, VALID_WELCOME_FORMATTERS) res = valid_format.format(first=escape(first_name), last=escape(new_mem.last_name or first_name), fullname=escape(fullname), username=username, mention=mention, count=count, chatname=escape(chat.title), id=new_mem.id) buttons = sql.get_welc_buttons(chat.id) keyb = build_keyboard(buttons) else: res = sql.DEFAULT_WELCOME.format(first=first_name) keyb = [] getsec, mutetime, custom_text = sql.welcome_security(chat.id) member = chat.get_member(new_mem.id) # If user ban protected don't apply security on him if is_user_ban_protected(chat, new_mem.id, chat.get_member(new_mem.id)): pass elif getsec: if mutetime: if mutetime[:1] == "0": if member.can_send_messages is None or member.can_send_messages: try: bot.restrict_chat_member( chat.id, new_mem.id, can_send_messages=False) canrest = True except BadRequest: canrest = False else: canrest = False else: mutetime = extract_time(update.effective_message, mutetime) if member.can_send_messages is None or member.can_send_messages: try: bot.restrict_chat_member( chat.id, new_mem.id, until_date=mutetime, can_send_messages=False) canrest = True except BadRequest: canrest = False else: canrest = False if canrest: sql.add_to_userlist(chat.id, new_mem.id) keyb.append([ InlineKeyboardButton( text=str(custom_text), callback_data="check_bot_({})".format( new_mem.id)) ]) keyboard = InlineKeyboardMarkup(keyb) sent = send(update, res, keyboard, sql.DEFAULT_WELCOME.format( first=first_name)) # type: Optional[Message] prev_welc = sql.get_clean_pref(chat.id) if prev_welc: try: bot.delete_message(chat.id, prev_welc) except BadRequest as excp: pass if sent: sql.set_clean_welcome(chat.id, sent.message_id)
def left_member(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] should_goodbye, cust_goodbye, cust_content, goodbye_type = sql.get_gdbye_pref( chat.id) cust_goodbye = markdown_to_html(cust_goodbye) if should_goodbye: left_mem = update.effective_message.left_chat_member if left_mem: if is_user_gbanned(left_mem.id): return if sw is not None: sw_ban = sw.get_ban(left_mem.id) if sw_ban: return # Ignore bot being kicked if left_mem.id == bot.id: return # Give the owner a special goodbye if left_mem.id == OWNER_ID: update.effective_message.reply_text( tld(chat.id, 'welcome_bot_owner_left')) return # if media goodbye, use appropriate function for it if goodbye_type not in (sql.Types.TEXT, sql.Types.BUTTON_TEXT): reply = update.message.message_id cleanserv = sql.clean_service(chat.id) # Clean service welcome if cleanserv: try: dispatcher.bot.delete_message( chat.id, update.message.message_id) except BadRequest: pass reply = False # Formatting text first_name = left_mem.first_name or "PersonWithNoName" # edge case of empty name - occurs for some bugs. if left_mem.last_name: fullname = "{} {}".format(first_name, left_mem.last_name) else: fullname = first_name count = chat.get_members_count() mention = mention_html(left_mem.id, first_name) if left_mem.username: username = "******" + escape(left_mem.username) else: username = mention formatted_text = cust_goodbye.format( first=escape(first_name), last=escape(left_mem.last_name or first_name), fullname=escape(fullname), username=username, mention=mention, count=count, chatname=escape(chat.title), id=left_mem.id) # Build keyboard buttons = sql.get_gdbye_buttons(chat.id) keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) # Send message ENUM_FUNC_MAP[goodbye_type](chat.id, cust_content, caption=formatted_text, reply_markup=keyboard, parse_mode="html", reply_to_message_id=reply) return first_name = left_mem.first_name or "PersonWithNoName" # edge case of empty name - occurs for some bugs. if cust_goodbye: if left_mem.last_name: fullname = "{} {}".format(first_name, left_mem.last_name) else: fullname = first_name count = chat.get_members_count() mention = mention_html(left_mem.id, first_name) if left_mem.username: username = "******" + escape(left_mem.username) else: username = mention valid_format = escape_invalid_curly_brackets( cust_goodbye, VALID_WELCOME_FORMATTERS) res = valid_format.format(first=escape(first_name), last=escape(left_mem.last_name or first_name), fullname=escape(fullname), username=username, mention=mention, count=count, chatname=escape(chat.title), id=left_mem.id) buttons = sql.get_gdbye_buttons(chat.id) keyb = build_keyboard(buttons) else: res = sql.DEFAULT_GOODBYE keyb = [] keyboard = InlineKeyboardMarkup(keyb) send(update, res, keyboard, sql.DEFAULT_GOODBYE)
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 get(bot, update, notename, show_none=True, no_format=False): chat = update.effective_chat user = update.effective_user conn = connected(bot, update, chat, user.id, need_admin=False) if conn: chat_id = conn send_id = user.id else: chat_id = update.effective_chat.id send_id = chat_id note = sql.get_note(chat_id, notename) message = update.effective_message if note: pass elif notename[0] == "#": hashnote = sql.get_note(chat_id, notename[1:]) if hashnote: note = hashnote elif show_none: message.reply_text(tld(chat.id, "note_not_existed")) return # If we're replying to a message, reply to that message (unless it's an error) if message.reply_to_message: reply_id = message.reply_to_message.message_id else: reply_id = message.message_id if note and note.is_reply: if MESSAGE_DUMP: try: bot.forward_message(chat_id=chat_id, from_chat_id=MESSAGE_DUMP, message_id=note.value) except BadRequest as excp: if excp.message == "Message to forward not found": message.reply_text(tld(chat.id, "note_lost")) sql.rm_note(chat_id, notename) else: raise else: try: bot.forward_message(chat_id=chat_id, from_chat_id=chat_id, message_id=note.value) except BadRequest as excp: if excp.message == "Message to forward not found": message.reply_text(tld(chat.id, "note_msg_del")) sql.rm_note(chat_id, notename) else: raise else: if note: text = note.value else: text = None keyb = [] parseMode = ParseMode.MARKDOWN buttons = sql.get_buttons(chat_id, notename) if no_format: parseMode = None text += revert_buttons(buttons) else: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) try: if note and note.msgtype in (sql.Types.BUTTON_TEXT, sql.Types.TEXT): try: bot.send_message(send_id, text, reply_to_message_id=reply_id, parse_mode=parseMode, disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Wrong http url": failtext = tld(chat.id, "note_url_invalid") failtext += "\n\n```\n{}```".format( note.value + revert_buttons(buttons)) message.reply_text(failtext, parse_mode="markdown") else: if note: ENUM_FUNC_MAP[note.msgtype](send_id, note.file, caption=text, reply_to_message_id=reply_id, parse_mode=parseMode, disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Entity_mention_user_invalid": message.reply_text(tld(chat.id, "note_mention_invalid")) elif FILE_MATCHER.match(note.value): message.reply_text(tld(chat.id, "note_incorrect_import")) sql.rm_note(chat_id, notename) else: message.reply_text(tld(chat.id, "note_cannot_send")) LOGGER.exception("Could not parse message #%s in chat %s", notename, str(chat_id)) LOGGER.warning("Message was: %s", str(note.value)) return
def get(bot, update, notename, show_none=True, no_format=False): 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 conn: chat_id = conn send_id = user.id else: chat_id = update.effective_chat.id send_id = chat_id note = sql.get_note(chat_id, notename) message = update.effective_message # type: Optional[Message] if note: # If we're replying to a message, reply to that message (unless it's an error) if message.reply_to_message: reply_id = message.reply_to_message.message_id else: reply_id = message.message_id if note.is_reply: if MESSAGE_DUMP: try: bot.forward_message(chat_id=chat_id, from_chat_id=MESSAGE_DUMP, message_id=note.value) except BadRequest as excp: if excp.message == "Message to forward not found": send_message(update.effective_message, (tld( chat.id, "This message appears to have disappeared - I will delete it" "from your note list."))) sql.rm_note(chat_id, notename) else: raise else: try: bot.forward_message(chat_id=chat_id, from_chat_id=chat_id, message_id=note.value) except BadRequest as excp: if excp.message == "Message to forward not found": send_message(update.effective_message, (tld( chat.id, "It looks like the original sender of this note has been deleted " "their message - sorry! Get your bot admin to start using " "dump messages to avoid this. I will delete this note from " "your saved notes."))) sql.rm_note(chat_id, notename) else: raise else: text = note.value keyb = [] parseMode = ParseMode.MARKDOWN buttons = sql.get_buttons(chat_id, notename) if no_format: parseMode = None text += revert_buttons(buttons) else: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) try: is_private, is_delete = sql.get_private_note(chat.id) if note.msgtype in (sql.Types.BUTTON_TEXT, sql.Types.TEXT): try: if is_delete: update.effective_message.delete() if is_private: bot.send_message(user.id, text, parse_mode=parseMode, disable_web_page_preview=True, reply_markup=keyboard) else: bot.send_message(send_id, text, reply_to_message_id=reply_id, parse_mode=parseMode, disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Wrong http url": failtext = (tld( chat.id, "Error: The URL on the button is invalid! Please update this note." )) failtext += "\n\n```\n{}```".format( note.value + revert_buttons(buttons)) send_message(update.effective_message, failtext, parse_mode="markdown") elif excp.message == "Button_url_invalid": failtext = (tld( chat.id, "Error: The URL on the button is invalid! Please update this note." )) failtext += "\n\n```\n{}```".format( note.value + revert_buttons(buttons)) send_message(update.effective_message, failtext, parse_mode="markdown") elif excp.message == "Message can't be deleted": pass elif excp.message == "Have no rights to send a message": pass except Unauthorized as excp: send_message( update.effective_message, (tld(chat.id, "Contact me in PM first to get this note.")), parse_mode="markdown") pass else: try: if is_delete: update.effective_message.delete() if is_private: ENUM_FUNC_MAP[note.msgtype]( user.id, note.file, caption=text, parse_mode=parseMode, disable_web_page_preview=True, reply_markup=keyboard) else: ENUM_FUNC_MAP[note.msgtype]( send_id, note.file, caption=text, reply_to_message_id=reply_id, parse_mode=parseMode, disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Message can't be deleted": pass elif excp.message == "Have no rights to send a message": pass except Unauthorized as excp: send_message( update.effective_message, (tld(chat.id, "Contact me in PM first to get this note.")), parse_mode="markdown") pass except BadRequest as excp: if excp.message == "Entity_mention_user_invalid": send_message(update.effective_message, (tld( chat.id, "Looks like you're trying to mention someone i've never seen before. " "If you really want to mention it, forward one of their messages to me, " "and I will be able to tag them!"))) elif FILE_MATCHER.match(note.value): send_message(update.effective_message, (tld( chat.id, "This note is wrong imported from another bot - I can't use it " "If you really need it, you should save it again. " "In the meantime, I will delete it from your record list." ))) sql.rm_note(chat_id, notename) else: send_message(update.effective_message, (tld( chat.id, "This note cannot be sent because it is in the wrong format." ))) LOGGER.exception("Could not parse message #%s in chat %s", notename, str(chat_id)) LOGGER.warning("The message: %s", str(note.value)) return elif show_none: send_message(update.effective_message, (tld(chat.id, "This note does not exist")))
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: try: message.reply_document(filt.reply) except: 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: 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( "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." ) 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( "This filter 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)) except: print("Nut") else: # LEGACY - all new filters will have has_markdown set to True. message.reply_text(filt.reply) break
if excp.message == "Message to forward not found": message.reply_text(tld(chat.id, "note_msg_del")) sql.rm_note(chat_id, notename) else: raise else: text = note.value if note else None keyb = [] parseMode = ParseMode.MARKDOWN buttons = sql.get_buttons(chat_id, notename) if no_format: parseMode = None text += revert_buttons(buttons) else: keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) try: if note and note.msgtype in (sql.Types.BUTTON_TEXT, sql.Types.TEXT): try: bot.send_message(send_id, text, reply_to_message_id=reply_id, parse_mode=parseMode, disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Wrong http url":