def adminlist(bot, 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: chatP = dispatcher.bot.getChat(conn) else: chatP = update.effective_chat if chat.type == "private": exit(1) administrators = chatP.get_administrators() text = tld(chat.id, "Admins in") + " *{}*:".format( chatP.title or tld(chat.id, "this chat")) for admin in administrators: user = admin.user status = admin.status if status == "creator": name = user.first_name + (user.last_name or "") + tld( chat.id, " (Creator)") else: name = user.first_name + (user.last_name or "") text += "\n• {}".format(name) update.effective_message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
def invite(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: chatP = dispatcher.bot.getChat(conn) else: chatP = update.effective_chat if chat.type == "private": exit(1) if chatP.username: update.effective_message.reply_text(chatP.username) elif chatP.type == chatP.SUPERGROUP or chatP.type == chatP.CHANNEL: bot_member = chatP.get_member(bot.id) if bot_member.can_invite_users: invitelink = chatP.invite_link #print(invitelink) if not invitelink: invitelink = bot.exportChatInviteLink(chatP.id) update.effective_message.reply_text(invitelink) else: update.effective_message.reply_text( tld( chat.id, "I don't have access to the invite link, try changing my permissions!" )) else: update.effective_message.reply_text( tld( chat.id, "I can only give you invite links for supergroups and channels, sorry!" ))
def commands(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user # type: Optional[User] conn = connected(bot, update, chat, user.id, need_admin=False) if not conn == False: chatD = dispatcher.bot.getChat(conn) else: if chat.type == "private": exit(1) else: chatD = update.effective_chat disabled = sql.get_all_disabled(chatD.id) if not disabled: update.effective_message.reply_text( tld(chat.id, "No commands are disabled! in *{}*!").format(chatD.title)) text = build_curr_disabled(chatD.id, chat.id) update.effective_message.reply_text(tld( chat.id, "The following commands are currently restricted in *{}*:\n{}"). format(chatD.title, text), parse_mode=ParseMode.MARKDOWN)
def disable(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] conn = connected(bot, update, chat, user.id) if not conn == False: chatD = dispatcher.bot.getChat(conn) else: if chat.type == "private": exit(1) else: chatD = update.effective_chat if len(args) >= 1: disable_cmd = args[0] if disable_cmd.startswith(CMD_STARTERS): disable_cmd = disable_cmd[1:] if disable_cmd in set(DISABLE_CMDS + DISABLE_OTHER): sql.disable_command(chatD.id, disable_cmd) update.effective_message.reply_text( tld(chat.id, "Disabled the use of `{}` in *{}*").format( disable_cmd, chatD.title), parse_mode=ParseMode.MARKDOWN) else: update.effective_message.reply_text( tld(chat.id, "That command can't be disabled")) else: update.effective_message.reply_text( tld(chat.id, "What should I disable?"))
def enable(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] conn = connected(bot, update, chat, user.id) if not conn == False: chatD = dispatcher.bot.getChat(conn) else: if chat.type == "private": exit(1) else: chatD = update.effective_chat if len(args) >= 1: enable_cmd = args[0] if enable_cmd.startswith(CMD_STARTERS): enable_cmd = enable_cmd[1:] if sql.enable_command(chatD.id, enable_cmd): update.effective_message.reply_text( tld(chat.id, "Enabled the use of `{}` in *{}*").format( enable_cmd, chatD.title), parse_mode=ParseMode.MARKDOWN) else: update.effective_message.reply_text( tld(chat.id, "Is that even disabled?")) else: update.effective_message.reply_text( tld(chat.id, "What should I enable?"))
def locale_button(bot, update): chat = update.effective_chat user = update.effective_user # type: Optional[User] query = update.callback_query lang_match = re.findall(r"en|ru|ua|es|tr|id|it", query.data) if lang_match: if lang_match[0]: switch_to_locale(chat.id, lang_match[0]) query.answer(text="Language changed!") else: query.answer(text="Error!", show_alert=True) try: LANGUAGE = prev_locale(chat.id) locale = LANGUAGE.locale_name curr_lang = list_locales[locale] except: curr_lang = "English" text = "*Select language* \n" text += "User language : `{}`".format(curr_lang) conn = connected(bot, update, chat, user.id, need_admin=False) if not conn == False: try: chatlng = prev_locale(conn).locale_name chatlng = list_locales[chatlng] text += "\nConnected chat language : `{}`".format(chatlng) except: chatlng = "English" text += "*\n\nSelect new user language:*" query.message.reply_text( text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton("English 🇺🇸", callback_data="set_lang_en") ]] + [[ InlineKeyboardButton("Russian 🇷🇺", callback_data="set_lang_ru"), InlineKeyboardButton("Ukrainian 🇺🇦", callback_data="set_lang_ua") ]] + [[ InlineKeyboardButton("Spanish 🇪🇸", callback_data="set_lang_es"), InlineKeyboardButton("Turkish 🇹🇷", callback_data="set_lang_tr") ]] + [[ InlineKeyboardButton("Indonesian 🇮🇩", callback_data="set_lang_id"), InlineKeyboardButton("Italian 🇮🇹", callback_data="set_lang_it") ]] + [[InlineKeyboardButton("⬅️ Back", callback_data="bot_start")]])) print(lang_match) query.message.delete() bot.answer_callback_query(query.id)
def stop_filter(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] args = update.effective_message.text.split(None, 1) conn = connected(bot, update, chat, user.id) if not conn == False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = "local notes" else: chat_name = chat.title if len(args) < 2: return chat_filters = sql.get_chat_triggers(chat_id) if not chat_filters: update.effective_message.reply_text( tld(chat.id, "No filters are active in {}!").format(chat_name)) return for keyword in chat_filters: if keyword == args[1]: sql.remove_filter(chat_id, args[1]) update.effective_message.reply_text( tld(chat.id, "Yep, I'll stop replying to that in *{}*.").format( chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop update.effective_message.reply_text( tld( chat.id, "That's not a current filter - run /filters for all active filters." ))
def list_handlers(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] 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 demote(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] user = update.effective_user # type: Optional[User] conn = connected(bot, update, chat, user.id) if not conn == False: chatD = dispatcher.bot.getChat(conn) else: chatD = update.effective_chat if chat.type == "private": exit(1) if not chatD.get_member(bot.id).can_promote_members: update.effective_message.reply_text( "I can't promote/demote people here! " "Make sure I'm admin and can appoint new admins.") exit(1) user_id = extract_user(message, args) if not user_id: message.reply_text( tld(chat.id, "You don't seem to be referring to a user.")) return "" user_member = chatD.get_member(user_id) if user_member.status == 'creator': message.reply_text( tld(chat.id, "This person CREATED the chat, how would I demote them?")) return "" if not user_member.status == 'administrator': message.reply_text(tld(chat.id, "Can't demote what wasn't promoted!")) return "" if user_id == bot.id: message.reply_text(tld(chat.id, "I can't demote myself!")) return "" try: bot.promoteChatMember(int(chatD.id), int(user_id), can_change_info=False, can_post_messages=False, can_edit_messages=False, can_delete_messages=False, can_invite_users=False, can_restrict_members=False, can_pin_messages=False, can_promote_members=False) message.reply_text(tld( chat.id, "Successfully Demoted in *{}*!").format(chatD.title), parse_mode=ParseMode.MARKDOWN) return "<b>{}:</b>" \ "\n#DEMOTED" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {}".format(html.escape(chatD.title), mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name)) except BadRequest: message.reply_text( tld( chat.id, "Could not demote. I might not be admin, or the admin status was appointed by another user, so I can't act upon them!" )) return ""
def promote(bot: Bot, update: Update, args: List[str]) -> str: message = update.effective_message # type: Optional[Message] user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] conn = connected(bot, update, chat, user.id) if not conn == False: chatD = dispatcher.bot.getChat(conn) else: chatD = update.effective_chat if chat.type == "private": exit(1) if not chatD.get_member(bot.id).can_promote_members: update.effective_message.reply_text( "I can't promote/demote people here! " "Make sure I'm admin and can appoint new admins.") exit(1) user_id = extract_user(message, args) if not user_id: message.reply_text( tld(chat.id, "You don't seem to be referring to a user.")) return "" user_member = chatD.get_member(user_id) if user_member.status == 'administrator' or user_member.status == 'creator': message.reply_text( tld(chat.id, "How am I meant to promote someone that's already an admin?")) return "" if user_id == bot.id: message.reply_text( tld(chat.id, "I can't promote myself! Get an admin to do it for me.")) return "" # set same perms as bot - bot can't assign higher perms than itself! bot_member = chatD.get_member(bot.id) bot.promoteChatMember( chatD.id, user_id, can_change_info=bot_member.can_change_info, can_post_messages=bot_member.can_post_messages, can_edit_messages=bot_member.can_edit_messages, can_delete_messages=bot_member.can_delete_messages, #can_invite_users=bot_member.can_invite_users, can_restrict_members=bot_member.can_restrict_members, can_pin_messages=bot_member.can_pin_messages, can_promote_members=bot_member.can_promote_members) message.reply_text(tld(chat.id, "Successfully promoted in *{}*!").format( chatD.title), parse_mode=ParseMode.MARKDOWN) return "<b>{}:</b>" \ "\n#PROMOTED" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {}".format(html.escape(chatD.title), mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name))
def control_panel(bot, update): LOGGER.info("Control panel") chat = update.effective_chat user = update.effective_user # ONLY send help in PM if chat.type != chat.PRIVATE: update.effective_message.reply_text( "Contact me in PM to access the control panel.", reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton( text="Control Panel", url=f"t.me/{bot.username}?start=controlpanel") ]])) return #Support to run from command handler query = update.callback_query if query: query.message.delete() M_match = re.match(r"cntrl_panel_M", query.data) U_match = re.match(r"cntrl_panel_U", query.data) G_match = re.match(r"cntrl_panel_G", query.data) back_match = re.match(r"help_back", query.data) LOGGER.info(query.data) else: M_match = "Haruka Aya is best bot" #LMAO, don't uncomment if M_match: text = "*Control panel* 🛠" keyboard = [[ InlineKeyboardButton(text="👤 My settings", callback_data="cntrl_panel_U(1)") ]] #Show connected chat and add chat settings button conn = connected(bot, update, chat, user.id, need_admin=False) if conn: chatG = bot.getChat(conn) #admin_list = chatG.get_administrators() #Unused variable #If user admin member = chatG.get_member(user.id) if member.status in ('administrator', 'creator'): text += f"\nConnected chat - *{chatG.title}* (you {member.status})" keyboard += [[ InlineKeyboardButton(text="👥 Group settings", callback_data="cntrl_panel_G_back") ]] elif user.id in SUDO_USERS: text += f"\nConnected chat - *{chatG.title}* (you sudo)" keyboard += [[ InlineKeyboardButton(text="👥 Group settings (SUDO)", callback_data="cntrl_panel_G_back") ]] else: text += f"\nConnected chat - *{chatG.title}* (you aren't an admin!)" else: text += "\nNo chat connected!" keyboard += [[ InlineKeyboardButton(text="⬅️ Back", callback_data="bot_start") ]] update.effective_message.reply_text( text, reply_markup=InlineKeyboardMarkup(keyboard), parse_mode=ParseMode.MARKDOWN) elif U_match: mod_match = re.match(r"cntrl_panel_U_module\((.+?)\)", query.data) back_match = re.match(r"cntrl_panel_U\((.+?)\)", query.data) chatP = update.effective_chat # type: Optional[Chat] if mod_match: module = mod_match.group(1) R = CHAT_SETTINGS[module].__user_settings__(bot, update, user) text = "You has the following settings for the *{}* module:\n\n".format( CHAT_SETTINGS[module].__mod_name__) + R[0] keyboard = R[1] keyboard += [[ InlineKeyboardButton(text="⬅️ Back", callback_data="cntrl_panel_U(1)") ]] query.message.reply_text( text=text, arse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(keyboard)) elif back_match: text = "*User control panel* 🛠" query.message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( paginate_modules( user.id, 0, USER_SETTINGS, "cntrl_panel_U"))) elif G_match: mod_match = re.match(r"cntrl_panel_G_module\((.+?)\)", query.data) prev_match = re.match(r"cntrl_panel_G_prev\((.+?)\)", query.data) next_match = re.match(r"cntrl_panel_G_next\((.+?)\)", query.data) back_match = re.match(r"cntrl_panel_G_back", query.data) chatP = chat conn = connected(bot, update, chat, user.id) if not conn == False: chat = bot.getChat(conn) else: query.message.reply_text(text="Error with connection to chat") exit(1) if mod_match: module = mod_match.group(1) R = CHAT_SETTINGS[module].__chat_settings__( bot, update, chat, chatP, user) if type(R) is list: text = R[0] keyboard = R[1] else: text = R keyboard = [] text = "*{}* has the following settings for the *{}* module:\n\n".format( escape_markdown(chat.title), CHAT_SETTINGS[module].__mod_name__) + text keyboard += [[ InlineKeyboardButton(text="Back", callback_data="cntrl_panel_G_back") ]] query.message.reply_text( text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(keyboard)) elif prev_match: chat_id = prev_match.group(1) curr_page = int(prev_match.group(2)) chat = bot.get_chat(chat_id) query.message.reply_text(tld( user.id, "send-group-settings").format(chat.title), reply_markup=InlineKeyboardMarkup( paginate_modules(curr_page - 1, 0, CHAT_SETTINGS, "cntrl_panel_G", chat=chat_id))) elif next_match: chat_id = next_match.group(1) next_page = int(next_match.group(2)) chat = bot.get_chat(chat_id) query.message.reply_text(tld( user.id, "send-group-settings").format(chat.title), reply_markup=InlineKeyboardMarkup( paginate_modules(next_page + 1, 0, CHAT_SETTINGS, "cntrl_panel_G", chat=chat_id))) elif back_match: text = "Test" query.message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( paginate_modules( user.id, 0, CHAT_SETTINGS, "cntrl_panel_G")))
def filters(bot: Bot, update: Update): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] args = msg.text.split( None, 1) # use python's maxsplit to separate Cmd, keyword, and reply_text 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 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() is_sticker = False is_document = False is_image = False is_voice = False is_audio = False is_video = False buttons = [] # determine what the contents of the filter are - text, image, sticker, etc if len(extracted) >= 2: 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( tld( chat.id, "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 msg.reply_to_message.sticker: content = msg.reply_to_message.sticker.file_id is_sticker = True elif msg.reply_to_message and msg.reply_to_message.document: content = msg.reply_to_message.document.file_id is_document = True elif msg.reply_to_message and msg.reply_to_message.photo: content = msg.reply_to_message.photo[ -1].file_id # last elem = best quality is_image = True elif msg.reply_to_message and msg.reply_to_message.audio: content = msg.reply_to_message.audio.file_id is_audio = True elif msg.reply_to_message and msg.reply_to_message.voice: content = msg.reply_to_message.voice.file_id is_voice = True elif msg.reply_to_message and msg.reply_to_message.video: content = msg.reply_to_message.video.file_id is_video = True else: msg.reply_text(tld(chat.id, "You didn't specify what to reply with!")) return # 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) sql.add_filter(chat_id, keyword, content, is_sticker, is_document, is_image, is_audio, is_voice, is_video, buttons) msg.reply_text(tld(chat.id, "Handler '{}' added in *{}*!").format( keyword, chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop