async def do_unscramble(session, keys, data, sentence, revealed, message): logger.debug("{}: Keys = {}, revealed = {}", session.get_user_id(), len(keys), revealed) if len(keys) == 0 and len(revealed) > 0: if revealed.strip() == session.unscramble_sentence[0].strip(): add_event(session.get_user_id(), session.active_lang(), session.get_current_hid(), 'SENTENCE', 10, 1) await bot.send_message(session.get_user_id(), "Excellent!") await next_unscramble(session) else: k = bot_utils.to_one_row_keyboard(['Restart', 'Next'], [0, 1], ['restart_unscramble', 'next_unscramble']) res = bot_utils.compare(revealed.strip(), session.unscramble_sentence[0].strip()) #TODO use Levenshtian distance add_event(session.get_user_id(), session.active_lang(), session.get_current_hid(), 'SENTENCE', 10, 0) await bot.send_message(session.get_user_id(), "A bit wrong.\nIt is:\n" "{}\nyuor answer:\n{}" .format(res[1], res[0]), parse_mode=types.ParseMode.HTML, reply_markup=k) await message.edit_text("*" + sentence[1] + "*" + "\n" + revealed) return actions = ['unscramble'] * len(data) session.unscramble_keys = keys session.unscramble_data = data session.unscramble_revealed = revealed session.unscramble_sentence = sentence k = keys.copy() k.append("RESTART") a = actions.copy() a.append("restart_unscramble") d = data.copy() d.append('-1') k = bot_utils.to_vertical_keyboard(k, d, a) await message.edit_text("*" + sentence[1] + "*" + "\n" + revealed, reply_markup=k)
async def do_reading_errors1(session): bot_response = ["What word matches this definition?", "What word is correct for this definition?"] if session.has_more_errors(): word, variants = session.get_next_error() variants = list.copy(variants) variants.append(word[0]) random.shuffle(variants) c = variants.index(word[0]) a = ["mc_wrong"] * len(variants) a[c] = "mc_correct" definition = word[1] keyboard = to_vertical_keyboard(variants, data=[-3] * len(variants), action=a) # mc = multiple choice if word[2] < 2: await bot.send_message(session.get_user_id(), bot_response[word[2]]) try: await bot.send_message(session.get_user_id(), definition, reply_markup=keyboard) except CantParseEntities as e: logger.warning(e) string = session.get_current_definition() string = str(string).replace('*', '') string = str(string).replace('_', '') string = str(string).replace('`', '') await bot.send_message(session.get_user_id(), string, reply_markup=keyboard) else: if session.has_more_words_to_learn(): await do_learning1(session.get_user_id()) else: await bot.send_message(session.get_user_id(), "Congrats! You've learned all the words for now)\nYou may want to /test the words you've learned") session.user_status = None
async def start_learning_message(message): session, isValid = await authorize(message.from_user.id, with_lang=True) if not isValid: return if message.text == '/test': # FIXME do I need it? (Used in adding words to specify calls. Should be replaced with normal dp.callback_query_handler session.status = '/test' if message.text == '/learn': session.status = '/learn' await message.reply("OK, let's learn some " + session.active_lang()) hids = sr.get_items_to_learn( (session.get_user_id(), session.active_lang()), upper_recall_limit=0.5) lists = mysql_connect.get_list_names(message.from_user.id) keys = ['Learn all words (use /stop to finish learning)'] data = [-1] actions = ["start_learning"] await bot.send_message(session.get_user_id(), "You have {} words to learn.\n".format(len(hids))) if len(lists) > 0: keys.extend(lists) data.extend(list(range(len(lists)))) actions.extend(["start_learning"] * len(lists)) await bot.send_message(session.get_user_id(), "You have {} lists to learn.\n" .format(len(lists))) kb = to_vertical_keyboard(keys, data=data, action=actions) await bot.send_message(session.get_user_id(), "What do you want to learn now?", reply_markup=kb)
async def wordlist_command(message: types.Message): user_id = message.from_user.id logger.info(str(user_id) + ' /wordlist') session, isValid = await authorize(user_id, with_lang=True) if not isValid: return tokens = ["Top frequency words", "Smart list"] data = [0, 0] actions = ['topn', 'smart'] k = to_vertical_keyboard(tokens=tokens, data=data, action=actions) await bot.send_message(session.get_user_id(), "What type of lists would you like?", reply_markup=k)
async def delete_list(session): keys = mysql_connect.get_list_names(session.get_user_id()) data = list(range(len(keys))) actions = ["delete_list"] * len(keys) keys.append("CANCEL") data.append(-1) actions.append("keep_list") k = to_vertical_keyboard(keys, data, actions) await bot.send_message(session.get_user_id(), "Which list do you want to delete?", reply_markup=k)
async def source_keyb(used_sources): data = list() buttons = list() for i in range(len(DEFINITION_SOURCES)): if DEFINITION_SOURCES[i] in used_sources: continue data.append(i) buttons.append(DEFINITION_SOURCES[i]) actions = ['def_source'] * len(data) buttons.append("FINISH") data.append(-1) actions.append('def_source_finish') k = to_vertical_keyboard(buttons, data, actions) return k
async def text_message(message): logger.debug(str(message.from_user.id) + " Received message unknown message") logger.debug(str(message.from_user.id) + message.text) t = message.text buttons = ["Add word", "Search Wiktionary"] actions = ["add_user_definition", "wiktionary_search"] if str(t).lower() in LANGS: buttons.append("Set language") actions.append('setlanguage') buttons.append("CANCEL") actions.append("finish_adding_meanings") data = ['0'] * len(buttons) session, isValid = await authorize(message.from_user.id) if not isValid: return session.words_to_add = (t,) k = to_vertical_keyboard(buttons, action=actions, data=data) await message.reply("What would you like to do with this word?", reply_markup=k)
async def delete_list_action(query, callback_data): session, isValid = await authorize(query.from_user.id) if not isValid: return lists = mysql_connect.get_list_names(session.get_user_id()) list_name = lists[int(callback_data['data'])] keys = [ 'DELETE the list, KEEP the words', 'DELETE the list, DELETE al its words', 'CANCEL' ] data = [callback_data['data']] * 3 actions = ['del_list_keep_words', 'del_list_del_words', "keep_list"] k = to_vertical_keyboard(keys, data, actions) await bot.send_message( query.from_user.id, "You are about to delete list *{}*\nDo you want to delete all the words in the list? " "Or should I keep the words and delete the list only?".format( list_name), reply_markup=k)
def prepare_keyboard(definitions, list_hid_word): definitions = truncate(definitions, 30) # FIXME ideally, it should be called once, not every time the keyboard is built actions = ['meaning'] * len(definitions) button_titiles = definitions data = list(range(0, len(actions), 1)) actions.append('add_user_definition') button_titiles.append("ADD YOUR DEFINITION") data.append(0) if list_hid_word is None: button_titiles.append("CLOSE") actions.append('finish_adding_meanings') data.append(-1) else: button_titiles.append("NEXT") actions.append('next_word_from_list') data.append(-1) button_titiles.append("FINISH") actions.append('finish_adding_meanings') data.append(-1) k = to_vertical_keyboard(definitions, action=actions, data=data) return k