def word_th(bot, update): db = AnkiGenDB() try: state = State(db.get_state(update.message.chat_id)) except ValueError as e: print(traceback.format_exc()) 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() if not concept.isalpha(): bot.sendMessage(update.message.chat_id, text="Write only one word") else: lang = Languages(db.get_language(update.message.chat_id)) defs = AnkiAutomatic(concept).retrieve_defs(lang.name) if defs is None: bot.sendMessage(update.message.chat_id, text='No definitions found') return if_add_phonetics = db.get_if_add_phonetics(update.message.chat_id) for definition in defs: keyboard = [[ InlineKeyboardButton("Add to anki", callback_data=concept), InlineKeyboardButton("Cancel", callback_data='-1') ]] reply_markup = InlineKeyboardMarkup(keyboard) update.message.reply_text(definition, reply_markup=reply_markup) elif state == State.set_user: try: db.update_username(update.message.chat_id, update.message.text) except: bot.sendMessage(update.message.chat_id, text="Sorry, something went wrong") return 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: bot.sendMessage(update.message.chat_id, text="Sorry, something went wrong") return bot.sendMessage(update.message.chat_id, text="Success! Password updated.") elif state == State.set_deck: try: db.update_deck(update.message.chat_id, update.message.text) except: bot.sendMessage(update.message.chat_id, text="Sorry, something went wrong") return bot.sendMessage(update.message.chat_id, text="Success! Deck updated.") else: print('not a valid state')
def word_th(bot, update): 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)) introduced_word(bot, update, lang, concept) elif state == State.set_user: try: db.update_username(update.message.chat_id, update.message.text) except: bot.sendMessage(update.message.chat_id, text="Sorry, something went wrong") return 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: bot.sendMessage(update.message.chat_id, text="Sorry, something went wrong") return bot.sendMessage(update.message.chat_id, text="Success! Password updated.") elif state in dict_state_to_lang: try: db.update_deck_name(update.message.chat_id, update.message.text, dict_state_to_lang[state]) except: bot.sendMessage(update.message.chat_id, text="Sorry, something went wrong") return bot.sendMessage(update.message.chat_id, text="Success! Deck name updated.") else: print('not a valid state')
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')