Example #1
0
def remove_all_notes(update, context):
    chat = update.effective_chat
    user = update.effective_user
    message = update.effective_message

    if chat.type == "private":
        chat.title = tl(chat.id, "local notes")
    else:
        owner = chat.get_member(user.id)
        chat.title = chat.title
        if owner.status != 'creator':
            message.reply_text(tl(chat.id, "You must be this chat creator."))
            return

    note_list = sql.get_all_chat_notes(chat.id)
    if not note_list:
        message.reply_text(tl(chat.id, "No notes in *{}*!").format(chat.title),
                           parse_mode=ParseMode.MARKDOWN)
        return

    x = 0
    a_note = []
    for notename in note_list:
        x += 1
        note = notename.name.lower()
        a_note.append(note)

    for note in a_note:
        sql.rm_note(chat.id, note)

    message.reply_text(
        tl(chat.id, "{} notes from this chat have been removed.").format(x))
Example #2
0
def remove_all_notes(bot: Bot, update: Update):
    chat = update.effective_chat
    user = update.effective_user
    message = update.effective_message

    if chat.type == "private":
        chat.title = tld(chat.id, "note_is_local")
    else:
        owner = chat.get_member(user.id)
        chat.title = chat.title
        if owner.status != 'creator':
            message.reply_text(tld(chat.id, "notes_must_be_creator"))
            return

    note_list = sql.get_all_chat_notes(chat.id)
    if not note_list:
        message.reply_text(tld(chat.id,
                               "note_none_in_chat").format(chat.title),
                           parse_mode=ParseMode.MARKDOWN)
        return

    x = 0
    a_note = []
    for notename in note_list:
        x += 1
        note = notename.name.lower()
        a_note.append(note)

    for note in a_note:
        sql.rm_note(chat.id, note)

    message.reply_text(tld(chat.id, "notes_cleanup_success").format(x))
Example #3
0
def clear(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat
    user = update.effective_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 = tld(chat.id, "note_is_local")
        else:
            chat_name = chat.title

    if len(args) >= 1:
        notename = args[0].lower()

        if sql.rm_note(chat_id, notename):
            update.effective_message.reply_text(tld(
                chat.id, "clear_success").format(chat_name),
                                                parse_mode=ParseMode.MARKDOWN)
        else:
            update.effective_message.reply_text(
                tld(chat.id, "note_not_existed"))
Example #4
0
def get(bot, update, notename, show_none=True, no_format=False):
    chat = update.effective_chat
    user = update.effective_user
    conn = connected(bot, update, chat, user.id, need_admin=False)
    if conn:
        chat_id = conn
        send_id = user.id
    else:
        chat_id = update.effective_chat.id
        send_id = chat_id

    note = sql.get_note(chat_id, notename)
    message = update.effective_message

    if note:
        pass
    elif notename[0] == "#":
        hashnote = sql.get_note(chat_id, notename[1:])
        if hashnote:
            note = hashnote
    elif show_none:
        message.reply_text(tld(chat.id, "note_not_existed"))
        return

    # If we're replying to a message, reply to that message (unless it's an error)
    if message.reply_to_message:
        reply_id = message.reply_to_message.message_id
    else:
        reply_id = message.message_id

    if note and note.is_reply:
        if MESSAGE_DUMP:
            try:
                bot.forward_message(chat_id=chat_id,
                                    from_chat_id=MESSAGE_DUMP,
                                    message_id=note.value)
            except BadRequest as excp:
                if excp.message == "Message to forward not found":
                    message.reply_text(tld(chat.id, "note_lost"))
                    sql.rm_note(chat_id, notename)
                else:
                    raise
        else:
            try:
                bot.forward_message(chat_id=chat_id,
                                    from_chat_id=chat_id,
                                    message_id=note.value)

            except BadRequest as excp:
                if excp.message == "Message to forward not found":
                    message.reply_text(tld(chat.id, "note_msg_del"))
                sql.rm_note(chat_id, notename)

            else:
                raise
    else:
        if note:
            text = note.value
        else:
            text = None

        keyb = []
        parseMode = ParseMode.MARKDOWN
        buttons = sql.get_buttons(chat_id, notename)
        if no_format:
            parseMode = None
            text += revert_buttons(buttons)
        else:
            keyb = build_keyboard(buttons)

        keyboard = InlineKeyboardMarkup(keyb)

        try:
            if note and note.msgtype in (sql.Types.BUTTON_TEXT,
                                         sql.Types.TEXT):
                try:
                    bot.send_message(send_id,
                                     text,
                                     reply_to_message_id=reply_id,
                                     parse_mode=parseMode,
                                     disable_web_page_preview=True,
                                     reply_markup=keyboard)
                except BadRequest as excp:
                    if excp.message == "Wrong http url":
                        failtext = tld(chat.id, "note_url_invalid")
                        failtext += "\n\n```\n{}```".format(
                            note.value + revert_buttons(buttons))
                        message.reply_text(failtext, parse_mode="markdown")

            else:
                if note:
                    ENUM_FUNC_MAP[note.msgtype](send_id,
                                                note.file,
                                                caption=text,
                                                reply_to_message_id=reply_id,
                                                parse_mode=parseMode,
                                                disable_web_page_preview=True,
                                                reply_markup=keyboard)

        except BadRequest as excp:
            if excp.message == "Entity_mention_user_invalid":
                message.reply_text(tld(chat.id, "note_mention_invalid"))

            elif FILE_MATCHER.match(note.value):
                message.reply_text(tld(chat.id, "note_incorrect_import"))
                sql.rm_note(chat_id, notename)
            else:
                message.reply_text(tld(chat.id, "note_cannot_send"))
                LOGGER.exception("Could not parse message #%s in chat %s",
                                 notename, str(chat_id))
                LOGGER.warning("Message was: %s", str(note.value))

    return
Example #5
0
def get(bot, update, notename, show_none=True, no_format=False):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    conn = connected(bot, update, chat, user.id, need_admin=False)
    if conn:
        chat_id = conn
        send_id = user.id
    else:
        chat_id = update.effective_chat.id
        send_id = chat_id

    note = sql.get_note(chat_id, notename)
    message = update.effective_message  # type: Optional[Message]

    if note:
        # If we're replying to a message, reply to that message (unless it's an error)
        if message.reply_to_message:
            reply_id = message.reply_to_message.message_id
        else:
            reply_id = message.message_id

        if note.is_reply:
            if MESSAGE_DUMP:
                try:
                    bot.forward_message(chat_id=chat_id,
                                        from_chat_id=MESSAGE_DUMP,
                                        message_id=note.value)
                except BadRequest as excp:
                    if excp.message == "Message to forward not found":
                        send_message(update.effective_message, (tld(
                            chat.id,
                            "This message appears to have disappeared - I will delete it"
                            "from your note list.")))
                        sql.rm_note(chat_id, notename)
                    else:
                        raise
            else:
                try:
                    bot.forward_message(chat_id=chat_id,
                                        from_chat_id=chat_id,
                                        message_id=note.value)
                except BadRequest as excp:
                    if excp.message == "Message to forward not found":
                        send_message(update.effective_message, (tld(
                            chat.id,
                            "It looks like the original sender of this note has been deleted "
                            "their message - sorry! Get your bot admin to start using "
                            "dump messages to avoid this. I will delete this note from "
                            "your saved notes.")))
                        sql.rm_note(chat_id, notename)
                    else:
                        raise
        else:
            text = note.value
            keyb = []
            parseMode = ParseMode.MARKDOWN
            buttons = sql.get_buttons(chat_id, notename)
            if no_format:
                parseMode = None
                text += revert_buttons(buttons)
            else:
                keyb = build_keyboard(buttons)

            keyboard = InlineKeyboardMarkup(keyb)

            try:
                is_private, is_delete = sql.get_private_note(chat.id)
                if note.msgtype in (sql.Types.BUTTON_TEXT, sql.Types.TEXT):
                    try:
                        if is_delete:
                            update.effective_message.delete()
                        if is_private:
                            bot.send_message(user.id,
                                             text,
                                             parse_mode=parseMode,
                                             disable_web_page_preview=True,
                                             reply_markup=keyboard)
                        else:
                            bot.send_message(send_id,
                                             text,
                                             reply_to_message_id=reply_id,
                                             parse_mode=parseMode,
                                             disable_web_page_preview=True,
                                             reply_markup=keyboard)
                    except BadRequest as excp:
                        if excp.message == "Wrong http url":
                            failtext = (tld(
                                chat.id,
                                "Error: The URL on the button is invalid! Please update this note."
                            ))
                            failtext += "\n\n```\n{}```".format(
                                note.value + revert_buttons(buttons))
                            send_message(update.effective_message,
                                         failtext,
                                         parse_mode="markdown")
                        elif excp.message == "Button_url_invalid":
                            failtext = (tld(
                                chat.id,
                                "Error: The URL on the button is invalid! Please update this note."
                            ))
                            failtext += "\n\n```\n{}```".format(
                                note.value + revert_buttons(buttons))
                            send_message(update.effective_message,
                                         failtext,
                                         parse_mode="markdown")
                        elif excp.message == "Message can't be deleted":
                            pass
                        elif excp.message == "Have no rights to send a message":
                            pass
                    except Unauthorized as excp:
                        send_message(
                            update.effective_message,
                            (tld(chat.id,
                                 "Contact me in PM first to get this note.")),
                            parse_mode="markdown")
                        pass
                else:
                    try:
                        if is_delete:
                            update.effective_message.delete()
                        if is_private:
                            ENUM_FUNC_MAP[note.msgtype](
                                user.id,
                                note.file,
                                caption=text,
                                parse_mode=parseMode,
                                disable_web_page_preview=True,
                                reply_markup=keyboard)
                        else:
                            ENUM_FUNC_MAP[note.msgtype](
                                send_id,
                                note.file,
                                caption=text,
                                reply_to_message_id=reply_id,
                                parse_mode=parseMode,
                                disable_web_page_preview=True,
                                reply_markup=keyboard)
                    except BadRequest as excp:
                        if excp.message == "Message can't be deleted":
                            pass
                        elif excp.message == "Have no rights to send a message":
                            pass
                    except Unauthorized as excp:
                        send_message(
                            update.effective_message,
                            (tld(chat.id,
                                 "Contact me in PM first to get this note.")),
                            parse_mode="markdown")
                        pass

            except BadRequest as excp:
                if excp.message == "Entity_mention_user_invalid":
                    send_message(update.effective_message, (tld(
                        chat.id,
                        "Looks like you're trying to mention someone i've never seen before. "
                        "If you really want to mention it, forward one of their messages to me, "
                        "and I will be able to tag them!")))
                elif FILE_MATCHER.match(note.value):
                    send_message(update.effective_message, (tld(
                        chat.id,
                        "This note is wrong imported from another bot - I can't use it "
                        "If you really need it, you should save it again. "
                        "In the meantime, I will delete it from your record list."
                    )))
                    sql.rm_note(chat_id, notename)
                else:
                    send_message(update.effective_message, (tld(
                        chat.id,
                        "This note cannot be sent because it is in the wrong format."
                    )))
                    LOGGER.exception("Could not parse message #%s in chat %s",
                                     notename, str(chat_id))
                    LOGGER.warning("The message: %s", str(note.value))
        return
    elif show_none:
        send_message(update.effective_message,
                     (tld(chat.id, "This note does not exist")))
Example #6
0
def clear(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 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 = (tld(
                    chat.id,
                    "Note at *{chat_name}* for `{note_name}` deleted 😁").
                         format(chat_name=chat_name,
                                note_name=", ".join(catatan)))
            else:
                rtext = (tld(chat.id,
                             "Note `{note_name}` is deleted 😁").format(
                                 note_name=", ".join(catatan)))
            try:
                send_message(update.effective_message,
                             rtext,
                             parse_mode=ParseMode.MARKDOWN)
            except BadRequest:
                if conn:
                    rtext = (tld(
                        chat.id,
                        "Note in <b>{chat_name}</b> for <code>{note_name}</code> is deleted 😁"
                    ).format(chat_name=chat_name,
                             note_name=", ".join(catatan)))
                else:
                    rtext = (tld(
                        chat.id,
                        "Note <code>{note_name}</code> is deleted 😁").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 = (tld(
                    chat.id,
                    "Note at *{chat_name}* for `{fnote_name}` failed to delete!"
                ).format(chat_name=chat_name,
                         fnote_name=", ".join(catatangagal)))
            else:
                rtext = (tld(chat.id,
                             "Note `{fnote_name}` failed to delete!").format(
                                 fnote_name=", ".join(catatangagal)))
            try:
                send_message(update.effective_message,
                             rtext,
                             parse_mode=ParseMode.MARKDOWN)
            except BadRequest:
                if conn:
                    rtext = (tld(
                        chat.id,
                        "Note at <b>{chat_name}</b> for <code>{fnote_name}</code> failed to delete!"
                    ).format(chat_name=chat_name,
                             fnote_name=", ".join(catatangagal)))
                else:
                    rtext = (tld(
                        chat.id,
                        "Note <code>{fnote_name}</code> failed to delete!").
                             format(fnote_name=", ".join(catatangagal)))
                send_message(update.effective_message,
                             tl(update.effective_message, rtext),
                             parse_mode=ParseMode.HTML)
        else:
            if conn:
                rtext = (tld(
                    chat.id,
                    "Note in *{chat_name}* for `{note_name}` deleted 😁\nNote `{fnote_name}` failed to delete!"
                ).format(chat_name=chat_name,
                         note_name=", ".join(catatan),
                         fnote_name=", ".join(catatangagal)))
            else:
                rtext = (tld(
                    chat.id,
                    "Note `{note_name}` deleted 😁\nNote `{fnote_name}` failed to delete!"
                ).format(note_name=", ".join(catatan),
                         fnote_name=", ".join(catatangagal)))
            try:
                send_message(chat.id, rtext, parse_mode=ParseMode.MARKDOWN)
            except BadRequest:
                if conn:
                    rtext = (tld(
                        chat.id,
                        "Note in <b>{chat_name}</b> for <code>{note_name}</code> deleted 😁\nNote <code>{fnote_name}</code> failed to delete!"
                    ).format(chat_name=chat_name,
                             note_name=", ".join(catatan),
                             fnote_name=", ".join(catatangagal)))
                else:
                    rtext = (tld(
                        chat.id,
                        "Note <code>{note_name}</code> deleted 😁\nNote <code>{fnote_name}</code> failed to delete!"
                    ).format(note_name=", ".join(catatan),
                             fnote_name=", ".join(catatangagal)))
                send_message(update.effective_message, (tld(chat.id, rtext)),
                             parse_mode=ParseMode.HTML)

    else:
        send_message(update.effective_message,
                     (tld(chat.id, "What do you want to delete?")))
Example #7
0
def get(bot, update, notename, show_none=True, no_format=False):
    chat = update.effective_chat
    user = update.effective_user
    conn = connected(bot, update, chat, user.id, need_admin=False)
    if conn:
        chat_id = conn
        send_id = user.id
    else:
        chat_id = update.effective_chat.id
        send_id = chat_id

    note = sql.get_note(chat_id, notename)
    message = update.effective_message  # 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 "
                                "from your list of notes."))
                        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,
                                "Error: URL on the button is invalid! Please update this note."
                            )
                            failtext += "\n\n```\n{}```".format(
                                note.value + revert_buttons(buttons))
                            send_message(update.effective_message,
                                         failtext,
                                         parse_mode="markdown")
                        elif excp.message == "Button_url_invalid":
                            failtext = tl(
                                update.effective_message,
                                "Error: URL on the button is invalid! Please update this note."
                            )
                            failtext += "\n\n```\n{}```".format(
                                note.value + revert_buttons(buttons))
                            send_message(update.effective_message,
                                         failtext,
                                         parse_mode="markdown")
                        elif excp.message == "Message can't be deleted":
                            pass
                        elif excp.message == "Have no rights to send a message":
                            pass
                    except Unauthorized:
                        send_message(update.effective_message,
                                     tl(update.effective_message,
                                        "Contact me at PM to get this notes."),
                                     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:
                        send_message(update.effective_message,
                                     tl(update.effective_message,
                                        "Contact me at PM to get this note."),
                                     parse_mode="markdown")
                        pass

            except BadRequest as excp:
                if excp.message == "Entity_mention_user_invalid":
                    send_message(
                        update.effective_message,
                        tl(
                            update.effective_message,
                            "It looks like you tried to mention someone who has never seen before. "
                            "If you really want to mention, forwarding one of their messages to me, "
                            "and I will be able to mark them!"))
                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"))
Example #8
0
def clear(update, context):
    args = context.args
    chat = update.effective_chat
    user = update.effective_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?"))
Example #9
0
    if message.reply_to_message:
        reply_id = message.reply_to_message.message_id
    else:
        reply_id = message.message_id

    if note and note.is_reply:
        if MESSAGE_DUMP:
            try:
                bot.forward_message(chat_id=chat_id,
                                    from_chat_id=MESSAGE_DUMP,
                                    message_id=note.value)
            except BadRequest as excp:
                if excp.message != "Message to forward not found":
                    raise
                message.reply_text(tld(chat.id, "note_lost"))
                sql.rm_note(chat_id, notename)
        else:
            try:
                bot.forward_message(chat_id=chat_id,
                                    from_chat_id=chat_id,
                                    message_id=note.value)

            except BadRequest as excp:
                if excp.message == "Message to forward not found":
                    message.reply_text(tld(chat.id, "note_msg_del"))
                sql.rm_note(chat_id, notename)

            else:
                raise
    else:
        text = note.value if note else None
Example #10
0
def get(bot, update, notename, show_none=True, no_format=False):
    chat = update.effective_chat
    user = update.effective_user
    conn = connected(bot, update, chat, user.id, need_admin=False)
    if conn:
        chat_id = conn
        send_id = user.id
    else:
        chat_id = update.effective_chat.id
        send_id = chat_id

    note = sql.get_note(chat_id, notename)
    message = update.effective_message  # 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,
                                                                  "This message seems to have been lost - I'll remove it "
                                                                  "from your list of notes."))
                        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,
                                                                  "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_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,
                                          "Error: URL on the button is invalid! Please update this note.")
                            failtext += "\n\n```\n{}```".format(note.value + revert_buttons(buttons))
                            send_message(update.effective_message, failtext, parse_mode="markdown")
                        elif excp.message == "Button_url_invalid":
                            failtext = tl(update.effective_message,
                                          "Error: URL on the button is invalid! Please update this note.")
                            failtext += "\n\n```\n{}```".format(note.value + revert_buttons(buttons))
                            send_message(update.effective_message, failtext, parse_mode="markdown")
                        elif excp.message == "Message can't be deleted":
                            pass
                        elif excp.message == "Have no rights to send a message":
                            pass
                    except Unauthorized:
                        send_message(update.effective_message,
                                     tl(update.effective_message, "Contact me at PM to get this notes."),
                                     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:
                        send_message(update.effective_message,
                                     tl(update.effective_message, "Contact me at PM to get this note."),
                                     parse_mode="markdown")
                        pass

            except BadRequest as excp:
                if excp.message == "Entity_mention_user_invalid":
                    send_message(update.effective_message, tl(update.effective_message,
                                                              "It looks like you tried to mention someone who has never seen before. "
                                                              "If you really want to mention, forwarding one of their messages to me, "
                                                              "and I will be able to mark them!"))
                elif FILE_MATCHER.match(note.value):
                    send_message(update.effective_message, tl(update.effective_message,
                                                              "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:
                    send_message(update.effective_message, tl(update.effective_message,
                                                              "This note could not be sent, as it is incorrectly formatted."))
                    LOGGER.exception("Could not parse message #%s in the chat %s", notename, str(chat_id))
                    LOGGER.warning("The message: %s", str(note.value))
        return
    elif show_none:
        send_message(update.effective_message, tl(update.effective_message, "This note doesn't exist"))