Beispiel #1
0
def tags(update, context):
    db = AnkiGenDB()
    state = State(db.get_state(update.message.chat_id))
    if state in dict_state_to_lang_tags:
        db.update_tags(update.message.chat_id,
                       tags="",
                       lang=dict_state_to_lang_tags[state])
        update.message.reply_text("Ok! I deleted all the tags for {}.".format(
            dict_state_to_lang_tags[state].name),
                                  parse_mode='Markdown')
    else:
        langs = sorted(list(dict_state_to_lang.values()),
                       key=lambda x: x.value)
        keyboard = [[
            InlineKeyboardButton(lang.name,
                                 callback_data="tags{}".format(lang.value))
        ] for lang in langs]
        keyboard.append([
            InlineKeyboardButton('Cancel', callback_data="tags{}".format(-1))
        ])
        reply_markup = InlineKeyboardMarkup(keyboard)

        tags = db.get_all_tags(update.message.chat_id)
        if tags is None:
            tags = ''
        else:
            tags = '\nCurrent tags are:\n' + \
                         ''.join(['\n- *{}*: {}'.format(Languages(language).name, escape_markdown(lang_tags) if tags else "") for lang_tags, language in tags])
        text = 'Select a language to set tags for that language.{}'.format(
            tags)
        update.message.reply_text(text,
                                  reply_markup=reply_markup,
                                  parse_mode='Markdown')
Beispiel #2
0
def word_th(update, context):
    db = AnkiGenDB()
    try:
        state = State(db.get_state(update.message.chat_id))
    except ValueError as e:
        if db.get_state(update.message.chat_id) is None:
            db.insert_new_user(id_chat=update.message.chat_id)
        state = State(db.get_state(update.message.chat_id))

    if state == State.normal:
        concept = update.message.text.strip()
        lang = Languages(db.get_language(update.message.chat_id))
        def_lang = db.get_def_lang(update.message.chat_id, lang.value)
        word_to_lang = db.get_word_lang(update.message.chat_id, lang.value)
        introduced_word(context.bot, update, lang, concept, def_lang,
                        word_to_lang)
    elif state == State.set_user:
        try:
            db.update_username(update.message.chat_id, update.message.text)
        except:
            context.bot.sendMessage(update.message.chat_id,
                                    text="Sorry, something went wrong")
            return
        context.bot.sendMessage(update.message.chat_id,
                                text="Success! Username updated.")
    elif state == State.set_pass:
        try:
            db.update_password(update.message.chat_id, update.message.text)
        except:
            context.bot.sendMessage(update.message.chat_id,
                                    text="Sorry, something went wrong")
            return
        context.bot.sendMessage(update.message.chat_id,
                                text="Success! Password updated.")
    elif state == State.set_card_type:
        try:
            db.update_card_type(update.message.chat_id, update.message.text)
        except:
            context.bot.sendMessage(update.message.chat_id,
                                    text="Sorry, something went wrong")
            return
        context.bot.sendMessage(update.message.chat_id,
                                text="Success! Card type updated.")
    elif state in dict_state_to_lang:  # Set deck
        try:
            db.update_deck_name(update.message.chat_id, update.message.text,
                                dict_state_to_lang[state])
        except:
            context.bot.sendMessage(update.message.chat_id,
                                    text="Sorry, something went wrong")
            return
        context.bot.sendMessage(update.message.chat_id,
                                text="Success! Deck name updated.")

    elif state in dict_state_to_lang_tags:  # Set tags
        try:
            db.update_tags(update.message.chat_id, update.message.text,
                           dict_state_to_lang_tags[state])
        except:
            context.bot.sendMessage(
                update.message.chat_id,
                text="Sorry, something went wrong with the tags")
            return
        context.bot.sendMessage(update.message.chat_id,
                                text="Success! Tags updated.")
    elif state in dict_state_to_lang_defi:
        if update.message.text in supported_language_codes:
            context.bot.sendMessage(
                update.message.chat_id,
                text='Ok! I will translate these definitions into {}'.format(
                    supported_language_codes[update.message.text]))
            db.update_def_lang(update.message.chat_id,
                               dict_state_to_lang_defi[state],
                               update.message.text)
            db.update_state(update.message.chat_id, State.normal)
        else:
            keyboard = [[
                InlineKeyboardButton('Cancel',
                                     callback_data="tod{}".format(-1))
            ]]
            context.bot.sendMessage(
                update.message.chat_id,
                text=
                "\"{}\" is not a valid code. Introduce a valid code or press Cancel."
                .format(update.message.text),
                reply_markup=InlineKeyboardMarkup(keyboard))
    elif state in dict_state_to_lang_word:
        if update.message.text in supported_language_codes:
            context.bot.sendMessage(
                update.message.chat_id,
                text=
                'Ok! For new cards, I\'ll give you the option to add the concept translated to {}'
                .format(supported_language_codes[update.message.text]))
            db.update_word_lang(update.message.chat_id,
                                dict_state_to_lang_word[state],
                                update.message.text)
            db.update_state(update.message.chat_id, State.normal)
        else:
            keyboard = [[
                InlineKeyboardButton('Cancel',
                                     callback_data="tow{}".format(-1))
            ]]
            context.bot.sendMessage(
                update.message.chat_id,
                text=
                "\"{}\" is not a valid code. Introduce a valid code from [this list](https://telegra.ph/Language-codes-07-26) or press Cancel."
                .format(update.message.text),
                reply_markup=InlineKeyboardMarkup(keyboard),
                parse_mode='Markdown')
    else:
        print('not a valid state')