def clear(bot: Bot, update: Update, args: List[str]): spam = spamfilters(update.effective_message.text, update.effective_message.from_user.id, update.effective_chat.id) if spam == True: return update.effective_message.reply_text("Saya kecewa dengan anda, saya tidak akan mendengar kata-kata anda sekarang!") chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] conn = connected(bot, update, chat, user.id) if conn: 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) >= 1: catatan = "" catatangagal = "" for x in range(len(args)): notename = args[x] if sql.rm_note(chat_id, notename): catatan += "{}, ".format(notename) else: catatangagal += "{}, ".format(notename) if catatan != "" and catatangagal == "": update.effective_message.reply_text("Catatan di *{}* untuk {}berhasil dihapus π".format(chat_name, catatan), parse_mode=ParseMode.MARKDOWN) elif catatangagal != "" and catatan == "": update.effective_message.reply_text("Catatan di *{}* untuk {}gagal dihapus!".format(chat_name, catatangagal), parse_mode=ParseMode.MARKDOWN) else: update.effective_message.reply_text("Catatan di *{}* untuk {}berhasil dihapus π\nCatatan {}gagal dihapus!".format(chat_name, catatan, catatangagal), parse_mode=ParseMode.MARKDOWN)
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 clear(update, context): args = context.args chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] conn = connected(context.bot, update, chat, user.id) if conn: 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) >= 1: catatan = [] catatangagal = [] for x in range(len(args)): notename = args[x] if sql.rm_note(chat_id, notename): catatan.append(notename) else: catatangagal.append(notename) if len(catatan) >= 1 and len(catatangagal) == 0: if conn: rtext = tl( update.effective_message, "Catatan di *{chat_name}* untuk `{note_name}` dihapus π" ).format(chat_name=chat_name, note_name=", ".join(catatan)) else: rtext = tl(update.effective_message, "Catatan `{note_name}` dihapus π").format( note_name=", ".join(catatan)) try: send_message(update.effective_message, rtext, parse_mode=ParseMode.MARKDOWN) except BadRequest: if conn: rtext = tl( update.effective_message, "Catatan di <b>{chat_name}</b> untuk <code>{note_name}</code> dihapus π" ).format(chat_name=chat_name, note_name=", ".join(catatan)) else: rtext = tl( update.effective_message, "Catatan <code>{note_name}</code> dihapus π").format( note_name=", ".join(catatan)) send_message(update.effective_message, rtext, parse_mode=ParseMode.HTML) elif len(catatangagal) >= 0 and len(catatan) == 0: if conn: rtext = tl( update.effective_message, "Catatan di *{chat_name}* untuk `{fnote_name}` gagal dihapus!" ).format(chat_name=chat_name, fnote_name=", ".join(catatangagal)) else: rtext = tl(update.effective_message, "Catatan `{fnote_name}` gagal dihapus!").format( fnote_name=", ".join(catatangagal)) try: send_message(update.effective_message, rtext, parse_mode=ParseMode.MARKDOWN) except BadRequest: if conn: rtext = tl( update.effective_message, "Catatan di <b>{chat_name}</b> untuk <code>{fnote_name}</code> gagal dihapus!" ).format(chat_name=chat_name, fnote_name=", ".join(catatangagal)) else: rtext = tl( update.effective_message, "Catatan <code>{fnote_name}</code> gagal dihapus!" ).format(fnote_name=", ".join(catatangagal)) send_message(update.effective_message, tl(update.effective_message, rtext), parse_mode=ParseMode.HTML) else: if conn: rtext = tl( update.effective_message, "Catatan di *{chat_name}* untuk `{note_name}` dihapus π\nCatatan `{fnote_name}` gagal dihapus!" ).format(chat_name=chat_name, note_name=", ".join(catatan), fnote_name=", ".join(catatangagal)) else: rtext = tl( update.effective_message, "Catatan `{note_name}` dihapus π\nCatatan `{fnote_name}` gagal dihapus!" ).format(note_name=", ".join(catatan), fnote_name=", ".join(catatangagal)) try: send_message(update.effective_message, rtext, parse_mode=ParseMode.MARKDOWN) except BadRequest: if conn: rtext = tl( update.effective_message, "Catatan di <b>{chat_name}</b> untuk <code>{note_name}</code> dihapus π\nCatatan <code>{fnote_name}</code> gagal dihapus!" ).format(chat_name=chat_name, note_name=", ".join(catatan), fnote_name=", ".join(catatangagal)) else: rtext = tl( update.effective_message, "Catatan <code>{note_name}</code> dihapus π\nCatatan <code>{fnote_name}</code> gagal dihapus!" ).format(note_name=", ".join(catatan), fnote_name=", ".join(catatangagal)) send_message(update.effective_message, tl(update.effective_message, rtext), parse_mode=ParseMode.HTML) else: send_message(update.effective_message, tl(update.effective_message, "Apa yang ingin dihapus?"))
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": message.reply_text("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": message.reply_text("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: 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: if 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 = "Kesalahan: URL pada tombol tidak valid! Harap perbaruhi catatan ini." failtext += "\n\n```\n{}```".format(note.value + revert_buttons(buttons)) message.reply_text(failtext, parse_mode="markdown") print("Gagal mengirim catatan: " + excp.message) pass 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 == "Entity_mention_user_invalid": message.reply_text("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): message.reply_text("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: message.reply_text("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: message.reply_text("Catatan ini tidak ada")