def word_lang(update, context, lang): try: concept = update.message.text.split(' ', 1)[1].strip() except IndexError: return db = AnkiGenDB() 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)
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')