def log_action(update, context, *args, **kwargs): result = func(update, context, *args, **kwargs) chat = update.effective_chat message = update.effective_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 = db.get_chat_log_channel(chat.id) if log_chat: try: send_log(context.bot, log_chat, chat.id, result) except Unauthorized: db.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 get_exception(excp, filt, chat): if excp.message == "Unsupported url protocol": return "Anda tampaknya mencoba menggunakan protokol URL yang tidak didukung. Telegram tidak mendukung kunci 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 ("Data ini tidak dapat dikirim karena formatnya salah.")
def command_func(update, context, *args, **kwargs): try: context.bot.send_chat_action( chat_id=update.effective_chat.id, action=ChatAction.TYPING ) return func(update, context, *args, **kwargs) except error.BadRequest as err: if str(err) == "Have no rights to send a message": LOGGER.warning("Bot dibisukan {} {}".format( update.effective_message.chat.title, update.effective_message.chat.id ) )
def sed(update, context): 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 mengganti ... " " tidak ada apa-apa dengan sesuatu?") return try: # Protects bot from retarded geys -_- if infinite_checker(repl): return update.effective_message.reply_text( "Usaha yang bagus -_-") 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 kamu bahkan sed? Sepertinya tidak.") return # empty string errors -_- if len(text) >= telegram.MAX_MESSAGE_LENGTH: update.effective_message.reply_text( "Hasil dari perintah sed terlalu lama untuk \ telegram!") elif text: update.effective_message.reply_to_message.reply_text(text)
def snipe(update, context): args = context.args try: chat_id = str(args[0]) del args[0] except (TypeError, IndexError): update.effective_message.reply_text( "Tolong beri saya obrolan untuk digemakan!") 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( "Tidak dapat mengirim pesan. Mungkin saya bukan bagian dari grup itu?" ) else: update.effective_message.reply_text( "Kemana saya harus mengirim??\nBeri aku id obrolan!")
def broadcast(update, context): to_send = update.effective_message.text.split(None, 1) if len(to_send) >= 2: chats = users_db.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( "Siaran selesai. {} grup mungkin gagal menerima pesan " "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, "Saluran log ini telah dihapus - tidak disetel.", ) db.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\nPemformatan telah dinonaktifkan karena kesalahan yang tidak terduga.", )
def send_log(context: CallbackContext, log_chat_id: str, orig_chat_id: str, result: str): bot = context.bot try: bot.send_message( log_chat_id, result, parse_mode=ParseMode.HTML, disable_web_page_preview=True) except BadRequest as excp: if excp.message == "Chat not found": bot.send_message( orig_chat_id, "Saluran log ini telah dihapus - tidak disetel.") 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\nPemformatan telah dinonaktifkan karena kesalahan yang tidak terduga." )
def send_log(context: CallbackContext, log_chat_id: str, orig_chat_id: str, result: str): bot = context.bot try: bot.send_message( log_chat_id, result, parse_mode=ParseMode.HTML, disable_web_page_preview=True) 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 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.MARKDOWN, reply_markup=keyboard, 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 + "\nCatatan: pesan saat ini memiliki url yang tidak valid " "di salah satu tombolnya. Harap perbarui."), 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 + "\nCatatan: pesan saat ini memiliki tombol yang " "gunakan protokol url yang tidak didukung oleh " "telegram. Harap perbarui."), 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 + "\nCatatan: pesan saat ini memiliki beberapa url yang buruk. " "Harap perbarui."), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) LOGGER.warning(message) LOGGER.warning(keyboard) LOGGER.exception( "Tidak bisa mengurai! mendapat kesalahan host url yang tidak valid" ) elif excp.message == "Tidak punya hak untuk mengirim pesan": return else: msg = update.effective_message.reply_text( markdown_parser(backup_message + "\nCatatan: Terjadi kesalahan saat mengirim " "pesan khusus. Harap perbarui."), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) LOGGER.exception() return msg
def get(bot, update, notename, show_none=True, no_format=False): chat_id = update.effective_chat.id chat = update.effective_chat user = update.effective_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 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( "Pesan ini sepertinya telah hilang - Saya akan menghapusnya " "dari daftar catatan Anda.") 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( "Sepertinya pengirim asli catatan ini telah dihapus " "pesan mereka - maaf! Minta admin bot Anda untuk mulai menggunakan file " "pesan dump untuk menghindari ini. Saya akan menghapus catatan ini dari " "catatan Anda yang disimpan.") 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: if (ENUM_FUNC_MAP[note.msgtype] == dispatcher.bot.send_sticker): ENUM_FUNC_MAP[note.msgtype]( chat_id, note.file, reply_to_message_id=reply_id, 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, reply_markup=keyboard, ) except BadRequest as excp: if excp.message == "Entity_mention_user_invalid": message.reply_text( "Sepertinya Anda mencoba menyebut seseorang yang belum pernah saya lihat sebelumnya. Jika Anda benar-benar " "ingin menyebutkan mereka, meneruskan salah satu pesan mereka kepada saya, dan saya akan bisa " "untuk menandai mereka!") elif FILE_MATCHER.match(note.value): message.reply_text( "Catatan ini adalah file yang diimpor dengan tidak benar dari bot lain - Saya tidak dapat menggunakan " "Itu. Jika Anda benar-benar membutuhkannya, Anda harus menyimpannya lagi. Di " "sementara itu, saya akan menghapusnya dari daftar catatan Anda." ) sql.rm_note(chat_id, notename) else: message.reply_text( "Catatan ini tidak dapat dikirim, karena formatnya salah." ) 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("Catatan ini tidak ada")
def reply_filter(update, context): chat = update.effective_chat message = update.effective_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 == "harus ada balasan baru": 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, 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, 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: if (ENUM_FUNC_MAP[filt.file_type] == dispatcher.bot.send_sticker): ENUM_FUNC_MAP[filt.file_type]( chat.id, filt.file_id, reply_to_message_id=message.message_id, reply_markup=keyboard, ) 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, 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, reply_markup=keyboard, ) except BadRequest as excp: if excp.message == "Unsupported url protocol": try: send_message( 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("Error in filters: " + excp.message) elif excp.message == "Pesan balasan tidak ditemukan": try: context.bot.send_message( chat.id, filt.reply, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard, ) except BadRequest as excp: LOGGER.exception("Error in filters: " + excp.message) else: try: send_message( update.effective_message, "Pesan ini tidak dapat dikirim karena formatnya salah.", ) 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 ban(update, context): chat = update.effective_chat user = update.effective_user message = update.effective_message args = context.args if user_can_ban(chat, user, context.bot.id) is False: message.reply_text( "Anda tidak memiliki cukup hak untuk melarang pengguna!") return "" user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text( "Setidaknya merujuk beberapa pengguna untuk melarang!") return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text( "Saya tampaknya tidak dapat menemukan pengguna ini") return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text( "Saya tidak akan melarang admin, jangan mengejek diri sendiri!") return "" if user_id == context.bot.id: message.reply_text( "Saya tidak akan melarang diri saya sendiri, apakah Anda gila atau tidak?" ) 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, BAN_STICKER) # banhammer marie # sticker context.bot.sendMessage( chat.id, "biarkan {} berjalan di papan.".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("Sial, saya tidak bisa mencekal pengguna itu.") return ""
def temp_ban(update, context): chat = update.effective_chat user = update.effective_user message = update.effective_message args = context.args if user_can_ban(chat, user, context.bot.id) is False: message.reply_text( "Anda tidak memiliki cukup hak untuk memblokir seseorang untuk sementara!" ) return "" user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text( "Hai kawan! setidaknya rujuk beberapa pengguna untuk dicekal...") return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text( "Sepertinya saya tidak dapat menemukan pengguna ini") return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text("Wow! mari kita mulai memblokir Admin sendiri?...") return "" if user_id == context.bot.id: message.reply_text( "Saya tidak akan melarang diri saya sendiri, apakah Anda gila atau tidak?" ) return "" if not reason: message.reply_text( "Anda belum menentukan waktu untuk mencekal 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#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( "Dilarang! Pengguna akan diblokir {}.".format(time_val)) return log except BadRequest as excp: if excp.message == "Pesan balasan tidak ditemukan": # Do not reply message.reply_text( "Selamat tinggal .. kita akan bertemu setelah {}.".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("Sial, saya tidak bisa mencekal pengguna itu.") return ""
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( "Anda tidak memiliki cukup hak untuk membatasi seseorang agar tidak berbicara!" ) return "" user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("Anda sepertinya tidak mengacu pada pengguna.") return "" try: member = chat.get_member(user_id) except BadRequest as excp: if excp.message == "User not found": message.reply_text( "Sepertinya saya tidak dapat menemukan pengguna ini") return "" else: raise if is_user_admin(chat, user_id, member): message.reply_text( "Saya benar-benar berharap dapat menonaktifkan admin...") return "" if user_id == context.bot.id: message.reply_text( "Saya tidak akan BISUKAN diri saya sendiri, apakah Anda sudah gila?" ) return "" if not reason: message.reply_text( "Anda belum menentukan 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#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>Alasan:</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("diam! 🤐 Dibisukan untuk {}!".format(time_val)) return log else: message.reply_text("Pengguna ini telah dibungkam.") except BadRequest as excp: if excp.message == "Reply message not found": # Do not reply message.reply_text("diam! 🤐 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, ) message.reply_text( "Sial, saya tidak bisa menonaktifkan pengguna itu.") return ""