Esempio n. 1
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))
Esempio n. 2
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"))
Esempio n. 3
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 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 ValueError('A very specific bad thing happened')
    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
Esempio n. 4
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,
                            "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=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,
                            "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 = "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 = "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 as excp:
                        send_message(
                            update.effective_message,
                            "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,
                            "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,
                        "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):
                    send_message(
                        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,
                        "This note could not be sent, as it is incorrectly formatted."
                    )
                    LOGGER.exception("Unable to parse message #%s in chat %s",
                                     notename, str(chat_id))
                    LOGGER.warning("Message it: %s", str(note.value))
        return
    elif show_none:
        send_message(update.effective_message, "This note doesn't exist")
Esempio n. 5
0
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 = "Note in *{chat_name}* for `{note_name}` deleted".format(
                    chat_name=chat_name, note_name=", ".join(catatan))
            else:
                rtext = "Note `{note_name}` deleted".format(
                    note_name=", ".join(catatan))
            try:
                send_message(update.effective_message,
                             rtext,
                             parse_mode=ParseMode.MARKDOWN)
            except BadRequest:
                if conn:
                    rtext = "Note in <b>{chat_name}</b> for <code>{note_name}</code> deleted".format(
                        chat_name=chat_name, note_name=", ".join(catatan))
                else:
                    rtext = "Note <code>{note_name}</code> 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 = "Failed to delete note in *{chat_name}* for `{fnote_name}`!".format(
                    chat_name=chat_name, fnote_name=", ".join(catatangagal))
            else:
                rtext = "Failed to delete note `{fnote_name}`!".format(
                    fnote_name=", ".join(catatangagal))
            try:
                send_message(update.effective_message,
                             rtext,
                             parse_mode=ParseMode.MARKDOWN)
            except BadRequest:
                if conn:
                    rtext = "Failed to delete note in <b>{chat_name}</b> for <code>{fnote_name}</code>!".format(
                        chat_name=chat_name,
                        fnote_name=", ".join(catatangagal))
                else:
                    rtext = "Failed to delete note <code>{fnote_name}</code>!".format(
                        fnote_name=", ".join(catatangagal))
                send_message(update.effective_message,
                             rtext,
                             parse_mode=ParseMode.HTML)
        else:
            if conn:
                rtext = "Note `{note_name}` deleted in *{chat_name}*\nFailed to delete note `{fnote_name}`!".format(
                    chat_name=chat_name,
                    note_name=", ".join(catatan),
                    fnote_name=", ".join(catatangagal))
            else:
                rtext = "Note `{note_name}` deleted\nFailed to delete note `{fnote_name}`!".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 = "Note <code>{note_name}</code> deleted in <b>{chat_name}</b>\nFailed to delete note <code>{fnote_name}</code>!".format(
                        chat_name=chat_name,
                        note_name=", ".join(catatan),
                        fnote_name=", ".join(catatangagal))
                else:
                    rtext = "Note <code>{note_name}</code> deleted\nFailed to delete note <code>{fnote_name}</code>!".format(
                        note_name=", ".join(catatan),
                        fnote_name=", ".join(catatangagal))
                send_message(update.effective_message,
                             rtext,
                             parse_mode=ParseMode.HTML)

    else:
        send_message(update.effective_message, "What you want to delete?")