Ejemplo n.º 1
0
def save(update, context):
    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

    msg = update.effective_message  # type: Optional[Message]

    checktext = msg.text.split()
    if msg.reply_to_message:
        if len(checktext) <= 1:
            send_message(update.effective_message,
                         tl(update.effective_message, "Anda harus memberi nama untuk catatan ini!"))
            return
    else:
        if len(checktext) <= 2:
            send_message(update.effective_message,
                         tl(update.effective_message, "Anda harus memberi nama untuk catatan ini!"))
            return

    note_name, text, data_type, content, buttons = get_note_type(msg)

    if data_type is None:
        send_message(update.effective_message, tl(update.effective_message, "Tidak ada catatan!"))
        return

    if len(text.strip()) == 0:
        text = "`" + note_name + "`"

    sql.add_note_to_db(chat_id, note_name, text, data_type, buttons=buttons, file=content)
    if conn:
        savedtext = tl(update.effective_message, "Ok, catatan `{note_name}` disimpan di *{chat_name}*.").format(
            note_name=note_name, chat_name=chat_name)
    else:
        savedtext = tl(update.effective_message, "Ok, catatan `{note_name}` disimpan.").format(note_name=note_name)
    try:
        send_message(update.effective_message, savedtext, parse_mode=ParseMode.MARKDOWN)
    except BadRequest:
        if conn:
            savedtext = tl(update.effective_message,
                           "Ok, catatan <code>{note_name}</code> disimpan di <b>{chat_name}</b>.").format(
                note_name=note_name, chat_name=chat_name)
        else:
            savedtext = tl(update.effective_message, "Ok, catatan <code>{note_name}</code> disimpan.").format(
                note_name=note_name)
        send_message(update.effective_message, savedtext, parse_mode=ParseMode.HTML)
Ejemplo n.º 2
0
def save(bot: Bot, update: Update):
    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

    msg = update.effective_message

    note_name, text, data_type, content, buttons = get_note_type(msg)
    note_name = note_name.lower()

    if data_type is None:
        msg.reply_text(tld(chat.id, "save_invalid"))
        return

    if not sql.get_note(chat_id, note_name):
        sql.add_note_to_db(chat_id,
                           note_name,
                           text,
                           data_type,
                           buttons=buttons,
                           file=content)
        msg.reply_text(tld(chat.id,
                           "save_success").format(note_name, chat_name,
                                                  note_name, note_name),
                       parse_mode=ParseMode.MARKDOWN)
    else:
        sql.add_note_to_db(chat_id,
                           note_name,
                           text,
                           data_type,
                           buttons=buttons,
                           file=content)
        msg.reply_text(tld(chat.id,
                           "save_updated").format(note_name, chat_name,
                                                  note_name, note_name),
                       parse_mode=ParseMode.MARKDOWN)
Ejemplo n.º 3
0
def __import_data__(chat_id, data):
    failures = []
    for notename, notedata in data.get('extra', {}).items():
        match = FILE_MATCHER.match(notedata)
        matchsticker = STICKER_MATCHER.match(notedata)
        matchbtn = BUTTON_MATCHER.match(notedata)
        matchfile = MYFILE_MATCHER.match(notedata)
        matchphoto = MYPHOTO_MATCHER.match(notedata)
        matchaudio = MYAUDIO_MATCHER.match(notedata)
        matchvoice = MYVOICE_MATCHER.match(notedata)
        matchvideo = MYVIDEO_MATCHER.match(notedata)
        matchvn = MYVIDEONOTE_MATCHER.match(notedata)

        if match:
            failures.append(notename)
            notedata = notedata[match.end():].strip()
            if notedata:
                sql.add_note_to_db(chat_id, notename[1:], notedata,
                                   sql.Types.TEXT)
        elif matchsticker:
            content = notedata[matchsticker.end():].strip()
            if content:
                sql.add_note_to_db(chat_id,
                                   notename[1:],
                                   notedata,
                                   sql.Types.STICKER,
                                   file=content)
        elif matchbtn:
            parse = notedata[matchbtn.end():].strip()
            notedata = parse.split("<###button###>")[0]
            buttons = parse.split("<###button###>")[1]
            buttons = ast.literal_eval(buttons)
            if buttons:
                sql.add_note_to_db(chat_id,
                                   notename[1:],
                                   notedata,
                                   sql.Types.BUTTON_TEXT,
                                   buttons=buttons)
        elif matchfile:
            file = notedata[matchfile.end():].strip()
            file = file.split("<###TYPESPLIT###>")
            notedata = file[1]
            content = file[0]
            if content:
                sql.add_note_to_db(chat_id,
                                   notename[1:],
                                   notedata,
                                   sql.Types.DOCUMENT,
                                   file=content)
        elif matchphoto:
            photo = notedata[matchphoto.end():].strip()
            photo = photo.split("<###TYPESPLIT###>")
            notedata = photo[1]
            content = photo[0]
            if content:
                sql.add_note_to_db(chat_id,
                                   notename[1:],
                                   notedata,
                                   sql.Types.PHOTO,
                                   file=content)
        elif matchaudio:
            audio = notedata[matchaudio.end():].strip()
            audio = audio.split("<###TYPESPLIT###>")
            notedata = audio[1]
            content = audio[0]
            if content:
                sql.add_note_to_db(chat_id,
                                   notename[1:],
                                   notedata,
                                   sql.Types.AUDIO,
                                   file=content)
        elif matchvoice:
            voice = notedata[matchvoice.end():].strip()
            voice = voice.split("<###TYPESPLIT###>")
            notedata = voice[1]
            content = voice[0]
            if content:
                sql.add_note_to_db(chat_id,
                                   notename[1:],
                                   notedata,
                                   sql.Types.VOICE,
                                   file=content)
        elif matchvideo:
            video = notedata[matchvideo.end():].strip()
            video = video.split("<###TYPESPLIT###>")
            notedata = video[1]
            content = video[0]
            if content:
                sql.add_note_to_db(chat_id,
                                   notename[1:],
                                   notedata,
                                   sql.Types.VIDEO,
                                   file=content)
        elif matchvn:
            video_note = notedata[matchvn.end():].strip()
            video_note = video_note.split("<###TYPESPLIT###>")
            notedata = video_note[1]
            content = video_note[0]
            if content:
                sql.add_note_to_db(chat_id,
                                   notename[1:],
                                   notedata,
                                   sql.Types.VIDEO_NOTE,
                                   file=content)
        else:
            sql.add_note_to_db(chat_id, notename[1:], notedata, sql.Types.TEXT)

    if failures:
        with BytesIO(str.encode("\n".join(failures))) as output:
            output.name = "failed_imports.txt"
            dispatcher.bot.send_document(
                chat_id,
                document=output,
                filename="failed_imports.txt",
                caption=(tld(
                    chat.id,
                    "This file/photo failed to import because it originated "
                    "from another bot. This is a telegram API restriction, and cannot "
                    "avoided. Sorry for the inconvenience!")))
Ejemplo n.º 4
0
def save(bot: Bot, update: Update):
    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 note"
        else:
            chat_name = chat.title

    msg = update.effective_message  # type: Optional[Message]

    checktext = msg.text.split()
    if msg.reply_to_message:
        if len(checktext) <= 1:
            send_message(update.effective_message,
                         (tld(chat.id, "You must give a name to this note!")))
            return
    else:
        if len(checktext) <= 2:
            send_message(update.effective_message,
                         (tld(chat.id, "You must give a name to this note!")))
            return

    note_name, text, data_type, content, buttons = get_note_type(msg)

    if data_type is None:
        send_message(update.effective_message, (tld(chat.id, "No note!")))
        return

    if len(text.strip()) == 0:
        text = "`" + note_name + "`"

    sql.add_note_to_db(chat_id,
                       note_name,
                       text,
                       data_type,
                       buttons=buttons,
                       file=content)
    if conn:
        savedtext = (tld(
            chat.id,
            "Ok, the note `{note_name}` is saved in *{chat_name}*.").format(
                note_name=note_name, chat_name=chat_name))
    else:
        savedtext = (tld(chat.id,
                         "Ok, the note `{note_name}` is saved.").format(
                             note_name=note_name))
    try:
        send_message(update.effective_message,
                     savedtext,
                     parse_mode=ParseMode.MARKDOWN)
    except BadRequest:
        if conn:
            savedtext = (tld(
                chat.id,
                "Ok, note <code>{note_name}</code> is stored in <b>{chat_name}</b>."
            ).format(note_name=note_name, chat_name=chat_name))
        else:
            savedtext = (tld(
                chat.id,
                "Ok, the <code>{note_name}</code> note is saved.").format(
                    note_name=note_name))
        send_message(update.effective_message,
                     savedtext,
                     parse_mode=ParseMode.HTML)
Ejemplo n.º 5
0
        else:
            chat_name = chat.title

    msg = update.effective_message

    note_name, text, data_type, content, buttons = get_note_type(msg)
    note_name = note_name.lower()

    if data_type is None:
        msg.reply_text(tld(chat.id, "save_invalid"))
        return

    if not sql.get_note(chat_id, note_name):
        sql.add_note_to_db(chat_id,
                           note_name,
                           text,
                           data_type,
                           buttons=buttons,
                           file=content)
        msg.reply_text(tld(chat.id,
                           "save_success").format(note_name, chat_name,
                                                  note_name, note_name),
                       parse_mode=ParseMode.MARKDOWN)
    else:
        sql.add_note_to_db(chat_id,
                           note_name,
                           text,
                           data_type,
                           buttons=buttons,
                           file=content)
        msg.reply_text(tld(chat.id,
                           "save_updated").format(note_name, chat_name,