def log_action(update, context, *args, **kwargs): result = func(update, context, *args, **kwargs) chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] if result: if chat.type == chat.SUPERGROUP and chat.username: result += ( "\n<b>Link:</b> " '<a href="http://telegram.me/{}/{}">click here</a>'. format(chat.username, message.message_id)) log_chat = sql.get_chat_log_channel(chat.id) if log_chat: try: send_log(context.bot, log_chat, chat.id, result) except Unauthorized: sql.stop_chat_logging(chat.id) elif result == "": pass else: LOGGER.warning( "%s was set as loggable, but had no return statement.", func) return result
def tl(message, text): if type(message) == int or type(message) == str and message[1:].isdigit(): getlang = sql.get_lang(message) if getlang == 'None' or not getlang: getlang = 'en' else: getlang = sql.get_lang(message.chat.id) if getlang == 'None' or not getlang: if message.from_user.language_code: if message.from_user.language_code in LOADED_LANGS_ID: sql.set_lang(message.chat.id, message.from_user.language_code) getlang = message.from_user.language_code else: sql.set_lang(message.chat.id, 'en') getlang = 'en' else: sql.set_lang(message.chat.id, 'en') getlang = 'en' getlangid = {} for x in LOADED_LANGS_ID: getlangid[x] = x if str(getlang) == 'id': get = getattr(FUNC_LANG['id'], 'id') if text in tuple(get): return get.get(text) if text in ("RUN_STRINGS", "SLAP_TEMPLATES", "ITEMS", "THROW", "HIT", "RAMALAN_STRINGS", "RAMALAN_FIRST"): runstr = getattr(FUNC_LANG['id'], text) return runstr return text elif str(getlang) in LOADED_LANGS_ID: func = getattr(FUNC_LANG[getlang], getlang) if text in ("RUN_STRINGS", "SLAP_TEMPLATES", "ITEMS", "THROW", "HIT", "RAMALAN_STRINGS", "RAMALAN_FIRST"): runstr = getattr(FUNC_LANG[getlang], text) return runstr langtxt = func.get(text) if not langtxt: LOGGER.warning( "Can't get translated string for lang '{}' ('{}')".format( str(getlang), text)) langtxt = text return langtxt else: sql.set_lang(message.chat.id, 'en') get = getattr(FUNC_LANG['en'], 'en') if text in tuple(get): return get.get(text) if text in ("RUN_STRINGS", "SLAP_TEMPLATES", "ITEMS", "THROW", "HIT", "RAMALAN_STRINGS", "RAMALAN_FIRST"): runstr = getattr(FUNC_LANG['en'], text) return runstr return text
def get_exception(excp, filt, chat): if excp.message == "Unsupported url protocol": return "Anda tampaknya mencoba menggunakan protokol url yang tidak didukung. Telegram tidak mendukung tombol untuk beberapa protokol, seperti tg://. Silakan coba lagi." elif excp.message == "Reply message not found": return "noreply" else: 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)) return "Catatan ini tidak dapat dikirim karena formatnya salah."
def get_exception(excp, filt, chat): if excp.message == "Unsupported url protocol": return "You seem to be trying to use the URL protocol which is not supported. Telegram does not support key for multiple protocols, such as tg: //. Please try again!" elif excp.message == "Reply message not found": return "noreply" else: 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)) return "This data could not be sent because it is incorrectly formatted."
def snipe(update, context): args = context.args try: chat_id = str(args[0]) del args[0] except TypeError: update.effective_message.reply_text( "Please give me a chat to echo to!") to_send = " ".join(args) if len(to_send) >= 2: try: context.bot.sendMessage(int(chat_id), str(to_send)) except TelegramError: LOGGER.warning("Couldn't send to group %s", str(chat_id)) update.effective_message.reply_text( "Couldn't send the message. Perhaps I'm not part of that group?" )
def broadcast(update, context): to_send = update.effective_message.text.split(None, 1) if len(to_send) >= 2: chats = sql.get_all_chats() or [] failed = 0 for chat in chats: try: context.bot.sendMessage(int(chat.chat_id), to_send[1]) sleep(0.1) except TelegramError: failed += 1 LOGGER.warning("Couldn't send broadcast to %s, group name %s", str(chat.chat_id), str(chat.chat_name)) send_message( update.effective_message, "Siaran selesai. {} grup gagal menerima pesan, mungkin " "karena ditendang.".format(failed))
def send_log(bot: Bot, log_chat_id: str, orig_chat_id: str, result: str): try: bot.send_message(log_chat_id, result, parse_mode=ParseMode.HTML) except BadRequest as excp: if excp.message == "Chat not found": bot.send_message( orig_chat_id, "This log channel has been deleted - unsetting.") sql.stop_chat_logging(orig_chat_id) else: LOGGER.warning(excp.message) LOGGER.warning(result) LOGGER.exception("Could not parse") bot.send_message( log_chat_id, result + "\n\nFormatting has been disabled due to an unexpected error.", )
def broadcast(update, context): to_send = update.effective_message.text.split(None, 1) if len(to_send) >= 2: chats = sql.get_all_chats() or [] failed = 0 for chat in chats: try: context.bot.sendMessage(int(chat.chat_id), to_send[1]) sleep(0.1) except TelegramError: failed += 1 LOGGER.warning( "Couldn't send broadcast to %s, group name %s", str(chat.chat_id), str(chat.chat_name), ) update.effective_message.reply_text( "Broadcast complete. {} groups failed to receive the message, probably " "due to being kicked.".format(failed) )
def reply_filter(update, context): 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.reply == "there is should be a new reply": buttons = sql.get_buttons(chat.id, filt.keyword) keyb = build_keyboard_parser(context.bot, chat.id, buttons) keyboard = InlineKeyboardMarkup(keyb) VALID_WELCOME_FORMATTERS = [ "first", "last", "fullname", "username", "id", "chatname", "mention", ] if filt.reply_text: valid_format = escape_invalid_curly_brackets( filt.reply_text, VALID_WELCOME_FORMATTERS) if valid_format: filtext = valid_format.format( first=escape(message.from_user.first_name), last=escape(message.from_user.last_name or message.from_user.first_name), fullname=" ".join( [ escape(message.from_user.first_name), escape(message.from_user.last_name), ] if message.from_user.last_name else [escape(message.from_user.first_name)]), username="******" + escape(message.from_user.username) if message.from_user.username else mention_html( message.from_user.id, message.from_user.first_name), mention=mention_html(message.from_user.id, message.from_user.first_name), chatname=escape(message.chat.title) if message.chat.type != "private" else escape( message.from_user.first_name), id=message.from_user.id, ) else: filtext = "" else: filtext = "" if filt.file_type in (sql.Types.BUTTON_TEXT, sql.Types.TEXT): try: context.bot.send_message( chat.id, markdown_to_html(filtext), reply_to_message_id=message.message_id, parse_mode=ParseMode.HTML, disable_web_page_preview=True, reply_markup=keyboard, ) except BadRequest as excp: error_catch = get_exception(excp, filt, chat) if error_catch == "noreply": try: context.bot.send_message( chat.id, markdown_to_html(filtext), parse_mode=ParseMode.HTML, disable_web_page_preview=True, reply_markup=keyboard, ) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) send_message( update.effective_message, get_exception(excp, filt, chat), ) else: try: send_message( update.effective_message, get_exception(excp, filt, chat), ) except BadRequest as excp: LOGGER.exception("Failed to send message: " + excp.message) else: ENUM_FUNC_MAP[filt.file_type]( chat.id, filt.file_id, caption=markdown_to_html(filtext), reply_to_message_id=message.message_id, parse_mode=ParseMode.HTML, disable_web_page_preview=True, reply_markup=keyboard, ) break else: 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_parser(context.bot, chat.id, buttons) keyboard = InlineKeyboardMarkup(keyb) try: send_message( update.effective_message, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard, ) except BadRequest as excp: if excp.message == "Unsupported url protocol": try: send_message( update.effective_message, "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...", ) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) elif excp.message == "Reply message not found": try: context.bot.send_message( chat.id, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard, ) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) else: try: send_message( update.effective_message, "This message couldn't be sent as it's incorrectly formatted.", ) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) 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. try: send_message(update.effective_message, filt.reply) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) break
def reply_filter(update, context): 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.reply == "there is should be a new reply": buttons = sql.get_buttons(chat.id, filt.keyword) keyb = build_keyboard_parser(context.bot, chat.id, buttons) keyboard = InlineKeyboardMarkup(keyb) VALID_WELCOME_FORMATTERS = [ 'first', 'last', 'fullname', 'username', 'id', 'chatname', 'mention' ] if filt.reply_text: valid_format = escape_invalid_curly_brackets( filt.reply_text, VALID_WELCOME_FORMATTERS) if valid_format: filtext = valid_format.format( first=escape_markdown( message.from_user.first_name), last=escape_markdown( message.from_user.last_name or message.from_user.first_name), fullname=escape_markdown( " ".join([ message.from_user.first_name, message. from_user.last_name ] if message.from_user.last_name else [message.from_user.first_name])), username="******" + message.from_user.username if message.from_user.username else mention_markdown( message.from_user.id, message.from_user.first_name), mention=mention_markdown( message.from_user.id, message.from_user.first_name), chatname=escape_markdown( message.chat.title if message.chat.type != "private" else message.from_user.first_name), id=message.from_user.id) else: filtext = "" else: filtext = "" if filt.file_type in (sql.Types.BUTTON_TEXT, sql.Types.TEXT): try: context.bot.send_message( chat.id, filtext, reply_to_message_id=message.message_id, parse_mode="markdown", disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: error_catch = get_exception(excp, filt, chat) if error_catch == "noreply": try: context.bot.send_message( chat.id, filtext, parse_mode="markdown", disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: LOGGER.exception("Gagal mengirim pesan: " + excp.message) send_message( update.effective_message, tl(update.effective_message, get_exception(excp, filt, chat))) pass else: try: send_message( update.effective_message, tl(update.effective_message, get_exception(excp, filt, chat))) except BadRequest as excp: LOGGER.exception("Gagal mengirim pesan: " + excp.message) pass else: ENUM_FUNC_MAP[filt.file_type]( chat.id, filt.file_id, caption=filtext, reply_to_message_id=message.message_id, parse_mode="markdown", disable_web_page_preview=True, reply_markup=keyboard) break else: 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_parser(context.bot, chat.id, buttons) keyboard = InlineKeyboardMarkup(keyb) try: send_message(update.effective_message, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: if excp.message == "Unsupported url protocol": try: send_message( update.effective_message, tl( update.effective_message, "Anda tampaknya mencoba menggunakan protokol url yang tidak didukung. Telegram " "tidak mendukung tombol untuk beberapa protokol, seperti tg://. Silakan coba " "lagi.")) except BadRequest as excp: LOGGER.exception("Gagal mengirim pesan: " + excp.message) pass elif excp.message == "Reply message not found": try: context.bot.send_message( chat.id, filt.reply, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True, reply_markup=keyboard) except BadRequest as excp: LOGGER.exception("Gagal mengirim pesan: " + excp.message) pass else: try: send_message( update.effective_message, tl( update.effective_message, "Catatan ini tidak dapat dikirim karena formatnya salah." )) except BadRequest as excp: LOGGER.exception("Gagal mengirim pesan: " + excp.message) pass 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. try: send_message(update.effective_message, filt.reply) except BadRequest as excp: LOGGER.exception("Gagal mengirim pesan: " + excp.message) pass break
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, tl(update.effective_message, "Pesan ini tampaknya telah hilang - saya akan menghapusnya " "dari daftar catatan Anda.")) 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, tl(update.effective_message, "Sepertinya pengirim asli dari catatan ini telah dihapus " "pesan mereka - maaf! Dapatkan admin bot Anda untuk mulai menggunakan " "pesan dump untuk menghindari ini. Saya akan menghapus catatan ini dari " "catatan tersimpan Anda.")) sql.rm_note(chat_id, notename) else: raise else: VALID_WELCOME_FORMATTERS = ['first', 'last', 'fullname', 'username', 'id', 'chatname', 'mention', 'rules'] valid_format = escape_invalid_curly_brackets(note.value, VALID_WELCOME_FORMATTERS) if valid_format: text = valid_format.format(first=escape_markdown(message.from_user.first_name), last=escape_markdown(message.from_user.last_name or message.from_user.first_name), fullname=escape_markdown(" ".join([message.from_user.first_name, message.from_user.last_name] if message.from_user.last_name else [message.from_user.first_name])), username="******" + message.from_user.username if message.from_user.username else mention_markdown(message.from_user.id, message.from_user.first_name), mention=mention_markdown(message.from_user.id, message.from_user.first_name), chatname=escape_markdown(message.chat.title if message.chat.type != "private" else message.from_user.first_name), id=message.from_user.id, rules="http://t.me/{}?start={}".format(bot.username, chat_id)) else: text = "" keyb = [] parseMode = ParseMode.MARKDOWN buttons = sql.get_buttons(chat_id, notename) if no_format: parseMode = None text += revert_buttons(buttons) else: keyb = build_keyboard_parser(bot, chat_id, 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 = tl(update.effective_message, "Kesalahan: URL pada tombol tidak valid! Harap perbaruhi catatan ini.") 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 = tl(update.effective_message, "Kesalahan: URL pada tombol tidak valid! Harap perbaruhi catatan ini.") 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, tl(update.effective_message, "Hubungi saya di PM dulu untuk mendapatkan catatan ini."), 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, tl(update.effective_message, "Hubungi saya di PM dulu untuk mendapatkan catatan ini."), parse_mode="markdown") pass except BadRequest as excp: if excp.message == "Entity_mention_user_invalid": send_message(update.effective_message, tl(update.effective_message, "Sepertinya Anda mencoba menyebutkan seseorang yang belum pernah saya lihat sebelumnya. " "Jika kamu benar-benar ingin menyebutkannya, meneruskan salah satu pesan mereka kepada saya, " "dan saya akan dapat untuk menandai mereka!")) elif FILE_MATCHER.match(note.value): send_message(update.effective_message, tl(update.effective_message, "Catatan ini adalah file yang salah diimpor dari bot lain - saya tidak bisa menggunakan " "ini. Jika Anda benar-benar membutuhkannya, Anda harus menyimpannya lagi. " "Sementara itu, saya akan menghapusnya dari daftar catatan Anda.")) sql.rm_note(chat_id, notename) else: send_message(update.effective_message, tl(update.effective_message, "Catatan ini tidak dapat dikirim karena formatnya salah.")) LOGGER.exception("Tidak dapat menguraikan pesan #%s di obrolan %s", notename, str(chat_id)) LOGGER.warning("Pesan itu: %s", str(note.value)) return elif show_none: send_message(update.effective_message, tl(update.effective_message, "Catatan ini tidak ada"))
def temp_mute(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args if user_can_ban(chat, user, context.bot.id) == False: message.reply_text( "You don't have enough rights to restrict someone from talking!") return "" user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text("I can't seem to find this user") return "" else: raise if is_user_admin(chat, user_id, member): message.reply_text("I really wish I could mute admins...") return "" if user_id == context.bot.id: message.reply_text("I'm not gonna MUTE myself, are you crazy?") return "" if not reason: message.reply_text( "You haven't specified a time to mute this user for!") return "" split_reason = reason.split(None, 1) time_val = split_reason[0].lower() if len(split_reason) > 1: reason = split_reason[1] else: reason = "" mutetime = extract_time(message, time_val) if not mutetime: return "" log = ("<b>{}:</b>" "\n#TEMP MUTED" "\n<b>Admin:</b> {}" "\n<b>User:</b> {}" "\n<b>Time:</b> {}".format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), time_val, )) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: if member.can_send_messages is None or member.can_send_messages: context.bot.restrict_chat_member( chat.id, user_id, until_date=mutetime, permissions=ChatPermissions(can_send_messages=False), ) message.reply_text("shut up! 🤐 Taped for {}!".format(time_val)) return log else: message.reply_text("This user is already muted.") except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text("shut up! 🤐 Taped for {}!".format(time_val), quote=False) return log else: LOGGER.warning(update) LOGGER.exception( "ERROR muting user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message, ) message.reply_text("Well damn, I can't mute that user.") return ""
def temp_ban(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args if user_can_ban(chat, user, context.bot.id) is False: message.reply_text( "You don't have enough rights to temporarily ban someone!") return "" user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("Hey ! Please atleast refer some user to ban...") return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text("I can't seem to find this user") return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text("Wew! let's start banning Admins themselves?...") return "" if user_id == context.bot.id: message.reply_text("I'm not gonna Ban myself, are you crazy ?") return "" if not reason: message.reply_text( "You haven't specified a time to ban this user for!") return "" split_reason = reason.split(None, 1) time_val = split_reason[0].lower() if len(split_reason) > 1: reason = split_reason[1] else: reason = "" bantime = extract_time(message, time_val) if not bantime: return "" log = ("<b>{}:</b>" "\n#TEMP BANNED" "\n<b>Admin:</b> {}" "\n<b>User:</b> {} (<code>{}</code>)" "\n<b>Time:</b> {}".format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), member.user.id, time_val, )) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: chat.kick_member(user_id, until_date=bantime) # context.bot.send_sticker(chat.id, BAN_STICKER) # banhammer marie sticker message.reply_text( "Banned! User will be banned for {}.".format(time_val)) return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text("Bye.. we'll meet after {} !".format(time_val), quote=False) return log else: LOGGER.warning(update) LOGGER.exception( "ERROR banning user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message, ) message.reply_text("Well damn, I can't ban that user.") return ""
def sed(bot: Bot, update: Update): start = time.time() spam = spamfilters(update.effective_message.text, update.effective_message.from_user.id, update.effective_chat.id, update.effective_message) if spam == True: return if update.effective_message.from_user.id != 388576209: return while elapsed_time() < max_time: sed_result = separate_sed(update.effective_message.text) if sed_result and update.effective_message.reply_to_message: if update.effective_message.reply_to_message.text: to_fix = update.effective_message.reply_to_message.text elif update.effective_message.reply_to_message.caption: to_fix = update.effective_message.reply_to_message.caption else: return repl, repl_with, flags = sed_result if not repl: update.effective_message.reply_to_message.reply_text( "Anda mencoba untuk mengganti... " "tidak ada apa-apa dengan sesuatu?") return try: check = re.match(repl, to_fix, flags=re.IGNORECASE) if check and check.group(0).lower() == to_fix.lower(): update.effective_message.reply_to_message.reply_text( "Hai semuanya, {} sedang mencoba untuk membuat " "saya mengatakan hal-hal yang saya tidak mau " "katakan!".format(update.effective_user.first_name)) return if 'i' in flags and 'g' in flags: text = re.sub(repl, repl_with, to_fix, flags=re.I).strip() elif 'i' in flags: text = re.sub(repl, repl_with, to_fix, count=1, flags=re.I).strip() elif 'g' in flags: text = re.sub(repl, repl_with, to_fix).strip() else: text = re.sub(repl, repl_with, to_fix, count=1).strip() except sre_constants.error: LOGGER.warning(update.effective_message.text) LOGGER.exception("SRE constant error") update.effective_message.reply_text( "Apakah itu sed? Sepertinya tidak.") return # empty string errors -_- if len(text) >= telegram.MAX_MESSAGE_LENGTH: return update.effective_message.reply_text( "Hasil dari perintah sed terlalu lama untuk \ telegram!") elif text: return update.effective_message.reply_to_message.reply_text( text) return update.effective_message.reply_to_message.reply_text( "Hasil terlalu lama untuk di proses!")
def get(bot, update, notename, show_none=True, no_format=False): chat_id = update.effective_chat.id 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 user.id else: chat_id = update.effective_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=update.effective_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( "This message seems to have been lost - I'll remove it " "from your notes list.") sql.rm_note(chat_id, notename) else: raise else: try: bot.forward_message( chat_id=update.effective_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( "Looks like the original sender of this note has deleted " "their message - sorry! Get your bot admin to start using a " "message dump to avoid this. I'll remove this note from " "your saved notes.") sql.rm_note(chat_id, notename) else: raise else: VALID_NOTE_FORMATTERS = [ "first", "last", "fullname", "username", "id", "chatname", "mention", ] valid_format = escape_invalid_curly_brackets( note.value, VALID_NOTE_FORMATTERS) if valid_format: text = valid_format.format( first=escape(message.from_user.first_name), last=escape(message.from_user.last_name or message.from_user.first_name), fullname=" ".join([ escape(message.from_user.first_name), escape(message.from_user.last_name), ] if message.from_user.last_name else [escape(message.from_user.first_name)]), username="******" + escape(message.from_user.username) if message.from_user.username else mention_html( message.from_user.id, message.from_user.first_name), mention=mention_html(message.from_user.id, message.from_user.first_name), chatname=escape(message.chat.title) if message.chat.type != "private" else escape( message.from_user.first_name), id=message.from_user.id, ) else: text = "" keyb = [] parseMode = ParseMode.HTML buttons = sql.get_buttons(chat_id, notename) if no_format: parseMode = None text += revert_buttons(buttons) else: text = markdown_to_html(text) keyb = build_keyboard(buttons) keyboard = InlineKeyboardMarkup(keyb) try: if note.msgtype in (sql.Types.BUTTON_TEXT, sql.Types.TEXT): bot.send_message( update.effective_chat.id, text, reply_to_message_id=reply_id, parse_mode=parseMode, disable_web_page_preview=True, reply_markup=keyboard, ) else: ENUM_FUNC_MAP[note.msgtype]( update.effective_chat.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( "Looks like you tried to mention someone I've never seen before. If you really " "want to mention them, forward one of their messages to me, and I'll be able " "to tag them!") elif FILE_MATCHER.match(note.value): message.reply_text( "This note was an incorrectly imported file from another bot - I can't use " "it. If you really need it, you'll have to save it again. In " "the meantime, I'll remove it from your notes list.") sql.rm_note(chat_id, notename) else: message.reply_text( "This note could not be sent, as it is incorrectly formatted." ) LOGGER.exception("Could not parse message #%s in chat %s", notename, str(chat_id)) LOGGER.warning("Message was: %s", str(note.value)) return elif show_none: message.reply_text("This note doesn't exist")
def temp_mute(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args user_id, reason = extract_user_and_text(message, args) if user_id == "error": send_message(update.effective_message, tl(update.effective_message, reason)) return "" if not user_id: send_message( update.effective_message, tl(update.effective_message, "Anda sepertinya tidak mengacu pada pengguna.")) return "" conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": update.effective_send_message( update.effective_message, tl(update.effective_message, "Anda bisa lakukan command ini pada grup, bukan pada PM")) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": send_message( update.effective_message, tl(update.effective_message, "Saya tidak dapat menemukan pengguna ini")) return "" else: raise if is_user_admin(chat, user_id, member): send_message( update.effective_message, tl(update.effective_message, "Saya benar-benar berharap dapat membisukan admin...")) return "" if user_id == context.bot.id: send_message( update.effective_message, tl( update.effective_message, "Saya tidak akan membisukan diri saya sendiri, apakah kamu gila?" )) return "" check = context.bot.getChatMember(chat.id, user.id) if check['can_restrict_members'] == False: send_message( update.effective_message, tl(update.effective_message, "Anda tidak punya hak untuk membatasi seseorang.")) return "" if not reason: send_message( update.effective_message, tl( update.effective_message, "Anda belum menetapkan waktu untuk menonaktifkan pengguna ini!" )) return "" split_reason = reason.split(None, 1) time_val = split_reason[0].lower() if len(split_reason) > 1: reason = split_reason[1] else: reason = "" mutetime = extract_time(message, time_val) if not mutetime: return "" log = "<b>{}:</b>" \ "\n#TMUTE" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {}" \ "\n<b>Time:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), time_val) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: if member.can_send_messages is None or member.can_send_messages: context.bot.restrict_chat_member( chat.id, user_id, until_date=mutetime, permissions=ChatPermissions(can_send_messages=False)) if conn: text = tl(update.effective_message, "Dibisukan untuk *{}* pada *{}*!").format( time_val, chat_name) else: text = tl(update.effective_message, "Dibisukan untuk *{}*!").format(time_val) send_message(update.effective_message, text, parse_mode="markdown") return log else: send_message( update.effective_message, tl(update.effective_message, "Pengguna ini sudah dibungkam.")) except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply send_message(update.effective_message, tl(update.effective_message, "Dibisukan untuk *{}*!").format(time_val), quote=False) return log else: LOGGER.warning(update) LOGGER.exception("ERROR muting user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message) send_message( update.effective_message, tl(update.effective_message, "Yah sial, aku tidak bisa membisukan pengguna itu.")) return ""
def ban(update, context): currentchat = update.effective_chat # type: Optional[Chat] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args user_id, reason = extract_user_and_text(message, args) if user_id == "error": send_message(update.effective_message, tl(update.effective_message, reason)) return "" conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, tl(update.effective_message, "Anda bisa lakukan command ini pada grup, bukan pada PM")) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title check = context.bot.getChatMember(chat_id, context.bot.id) if check.status == 'member' or check['can_restrict_members'] == False: if conn: text = tl(update.effective_message, "Saya tidak bisa membatasi orang di {}! Pastikan saya admin dan dapat menunjuk admin baru.").format(chat_name) else: text = tl(update.effective_message, "Saya tidak bisa membatasi orang di sini! Pastikan saya admin dan dapat menunjuk admin baru.") send_message(update.effective_message, text, parse_mode="markdown") return "" if not user_id: send_message(update.effective_message, tl(update.effective_message, "Anda sepertinya tidak mengacu pada pengguna.")) return "" try: if conn: member = context.bot.getChatMember(chat_id, user_id) else: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": if conn: text = tl(update.effective_message, "Saya tidak dapat menemukan pengguna ini pada *{}* 😣").format(chat_name) else: text = tl(update.effective_message, "Saya tidak dapat menemukan pengguna ini 😣") send_message(update.effective_message, text, parse_mode="markdown") return "" else: raise if user_id == context.bot.id: send_message(update.effective_message, tl(update.effective_message, "Saya tidak akan BAN diri saya sendiri, apakah kamu gila? 😠")) return "" if is_user_ban_protected(chat, user_id, member): send_message(update.effective_message, tl(update.effective_message, "Saya tidak bisa banned orang ini karena dia adalah admin 😒")) return "" if member['can_restrict_members'] == False: if conn: text = tl(update.effective_message, "Anda tidak punya hak untuk membatasi seseorang pada *{}*.").format(chat_name) else: text = tl(update.effective_message, "Anda tidak punya hak untuk membatasi seseorang.") send_message(update.effective_message, text, parse_mode="markdown") return "" log = "<b>{}:</b>" \ "\n#BANNED" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {} (<code>{}</code>)".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), member.user.id) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: if conn: context.bot.kickChatMember(chat_id, user_id) context.bot.send_sticker(currentchat.id, BAN_STICKER) # banhammer marie sticker send_message(update.effective_message, tl(update.effective_message, "Terbanned pada *{}*! 😝").format(chat_name), parse_mode="markdown") else: chat.kick_member(user_id) if message.text.split(None, 1)[0][1:] == "sban": update.effective_message.delete() else: context.bot.send_sticker(chat.id, BAN_STICKER) # banhammer marie sticker send_message(update.effective_message, tl(update.effective_message, "Terbanned! 😝")) return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply send_message(update.effective_message, tl(update.effective_message, "Terbanned! 😝"), quote=False) return log elif excp.message == "Message can't be deleted": pass else: LOGGER.warning(update) LOGGER.exception("ERROR membanned pengguna %s di obrolan %s (%s) disebabkan oleh %s", user_id, chat.title, chat.id, excp.message) send_message(update.effective_message, tl(update.effective_message, "Yah sial, aku tidak bisa banned pengguna itu 😒")) return ""
def temp_ban(update, context): currentchat = update.effective_chat # type: Optional[Chat] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args user_id, reason = extract_user_and_text(message, args) if user_id == "error": send_message(update.effective_message, tl(update.effective_message, reason)) return "" conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, tl(update.effective_message, "Anda bisa lakukan command ini pada grup, bukan pada PM")) return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title check = context.bot.getChatMember(chat_id, context.bot.id) if check.status == 'member': if conn: text = tl(update.effective_message, "Saya tidak bisa membatasi orang di *{}*! Pastikan saya admin dan dapat menunjuk admin baru.").format(chat_name) else: text = tl(update.effective_message, "Saya tidak bisa membatasi orang di sini! Pastikan saya admin dan dapat menunjuk admin baru.") send_message(update.effective_message, text, parse_mode="markdown") return "" else: if check['can_restrict_members'] == False: if conn: text = tl(update.effective_message, "Saya tidak bisa membatasi orang di *{}*! Pastikan saya admin dan dapat menunjuk admin baru.").format(chat_name) else: text = tl(update.effective_message, "Saya tidak bisa membatasi orang di sini! Pastikan saya admin dan dapat menunjuk admin baru.") send_message(update.effective_message, text, parse_mode="markdown") return "" if not user_id: send_message(update.effective_message, tl(update.effective_message, "Anda sepertinya tidak mengacu pada pengguna.")) return "" try: if conn: member = context.bot.getChatMember(chat_id, user_id) else: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": send_message(update.effective_message, tl(update.effective_message, "Saya tidak dapat menemukan pengguna ini 😣")) return "" else: raise if user_id == context.bot.id: send_message(update.effective_message, tl(update.effective_message, "Saya tidak akan BAN diri saya sendiri, apakah kamu gila? 😠")) return "" if is_user_ban_protected(chat, user_id, member): send_message(update.effective_message, tl(update.effective_message, "Saya tidak bisa banned orang ini karena dia adalah admin 😒")) return "" if member['can_restrict_members'] == False: send_message(update.effective_message, tl(update.effective_message, "Anda tidak punya hak untuk membatasi seseorang.")) return "" if not reason: send_message(update.effective_message, tl(update.effective_message, "Anda belum menetapkan waktu untuk banned pengguna ini!")) return "" split_reason = reason.split(None, 1) time_val = split_reason[0].lower() if len(split_reason) > 1: reason = split_reason[1] else: reason = "" bantime = extract_time(message, time_val) if not bantime: return "" log = "<b>{}:</b>" \ "\n#TEMPBAN" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {} (<code>{}</code>)" \ "\n<b>Time:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), member.user.id, time_val) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: if conn: context.bot.kickChatMember(chat_id, user_id, until_date=bantime) context.bot.send_sticker(currentchat.id, BAN_STICKER) # banhammer marie sticker send_message(update.effective_message, tl(update.effective_message, "Banned! Pengguna diblokir untuk *{}* pada *{}*.").format(time_val, chat_name), parse_mode="markdown") else: chat.kick_member(user_id, until_date=bantime) context.bot.send_sticker(chat.id, BAN_STICKER) # banhammer marie sticker send_message(update.effective_message, tl(update.effective_message, "Banned! Pengguna diblokir untuk {}.").format(time_val)) return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply send_message(update.effective_message, tl(update.effective_message, "Banned! Pengguna diblokir untuk {}.").format(time_val), quote=False) return log else: LOGGER.warning(update) LOGGER.exception("ERROR banning user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message) send_message(update.effective_message, tl(update.effective_message, "Yah sial, aku tidak bisa menendang pengguna itu 😒")) return ""
def ban(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args if user_can_ban(chat, user, context.bot.id) is False: message.reply_text("You don't have enough rights to ban users!") return "" user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("Hey ! Please atleast refer some user to ban!") return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text("I can't seem to find this user") return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text("I'm not gonna ban an admin ! Lol ") return "" if user_id == context.bot.id: message.reply_text("I'm not gonna Ban myself, are you crazy ?") return "" log = ("<b>{}:</b>" "\n#BANNED" "\n<b>Admin:</b> {}" "\n<b>User:</b> {} (<code>{}</code>)".format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), member.user.id, )) if reason: log += "\n<b>Reason:</b> {}".format(reason) try: chat.kick_member(user_id) context.bot.send_sticker(chat.id, STICKERID) # banhammer marie sticker context.bot.sendMessage( chat.id, "Stars Falls From The Skies \nBanned {}.".format( mention_html(member.user.id, member.user.first_name)), parse_mode=ParseMode.HTML, ) return log except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text("Banned!", quote=False) return log else: LOGGER.warning(update) LOGGER.exception( "ERROR banning user %s in chat %s (%s) due to %s", user_id, chat.title, chat.id, excp.message, ) message.reply_text("Well damn, I can't ban that user.") return ""
def send(update, message, keyboard, backup_message): chat = update.effective_chat cleanserv = sql.clean_service(chat.id) reply = update.message.message_id # Clean service welcome if cleanserv: try: dispatcher.bot.delete_message(chat.id, update.message.message_id) except BadRequest: pass reply = False try: msg = update.effective_message.reply_text( message, parse_mode=ParseMode.HTML, reply_markup=keyboard, reply_to_message_id=reply, disable_web_page_preview=True, ) except IndexError: msg = update.effective_message.reply_text( markdown_parser(backup_message + "\nNote: the current message was " "invalid due to markdown issues. Could be " "due to the user's name."), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) except KeyError: msg = update.effective_message.reply_text( markdown_parser(backup_message + "\nNote: the current message is " "invalid due to an issue with some misplaced " "curly brackets. Please update"), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) except BadRequest as excp: if excp.message == "Button_url_invalid": msg = update.effective_message.reply_text( markdown_parser( backup_message + "\nNote: the current message has an invalid url " "in one of its buttons. Please update."), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) elif excp.message == "Unsupported url protocol": msg = update.effective_message.reply_text( markdown_parser( backup_message + "\nNote: the current message has buttons which " "use url protocols that are unsupported by " "telegram. Please update."), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) elif excp.message == "Wrong url host": msg = update.effective_message.reply_text( markdown_parser( backup_message + "\nNote: the current message has some bad urls. " "Please update."), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) LOGGER.warning(message) LOGGER.warning(keyboard) LOGGER.exception("Could not parse! got invalid url host errors") else: msg = update.effective_message.reply_text( markdown_parser(backup_message + "\nNote: An error occured when sending the " "custom message. Please update."), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) LOGGER.exception() return msg