def msg_not_undestood(msg): """ Change setting sequence """ user_id = get_id(msg) user = user_manager.get_user(get_id(msg)) user.send_message("#message_not_understood", txt_args=(msg.text, ))
def setup_user(msg): """ Register user into database. """ user_id = get_id(msg) username = get_username(msg) if username == None: bot = user_manager.bot_controller_factory.get_bot_controller(user_id, 0) bot.send_message("#setup_user_username_error") bot.stop_instance() del bot return if user_manager.check_user_existence(user_id): user = user_manager.get_user(get_id(msg)) if user.get_active() == 0: user.set_active(1) user.send_message("#welcome_back") return m = user_manager.add_user(user_id, username) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger logger.warning("New username {} {} ".format(user_id, username)) logger = logging.getLogger('Bot_Sender') logger.warning("New username {} {} ".format(user_id, username)) options = ['English', 'Português'] user.send_string_keyboard("#setup_user_mother_language", options) user.set_state(fsm.next_state[fsm.IDLE]['setup user'])
def list_words1(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger operation, subject, keyboard_option, keyboard_len = user.parse_keyboard_ans(msg) if operation == 'Error': user.send_message("#choose_from_keyboard", markup=None) user.set_state(fsm.next_state[(fsm.LIST, fsm.GET_SUBJECT)][operation]) return if operation == 'End': user.send_message("#ok") user.set_state(fsm.next_state[(fsm.LIST, fsm.GET_SUBJECT)][operation]) return topics = user.get_active_topics(subject) topics.sort() markdown_options = ["_"] * len(topics) + ["*", "*"] user.send_navigation_string_keyboard("#list_topic_selection", options=topics, markdown_options=markdown_options, end_btn="End selection", back_btn="Go back") user.temp_study_item = StudyItemDeck(user_id, None, None) user.temp_study_item.subject = subject user.set_state(fsm.next_state[(fsm.LIST, fsm.GET_SUBJECT)][operation])
def ADD_ITEM1(msg): """ Add word: Get word's language """ user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger valid, subject, keyboard_option, keyboard_len = user.parse_keyboard_ans( msg) user.send_message("#item_subject", (subject, )) user.send_message("#add_item_topic_intro") known_topics = user.get_topics(subject) topics = [] for topic in known_topics: topics.append(topic[0]) topics.sort() if len(topics) > 0: user.send_string_keyboard("#list_topics", topics) else: user.send_string_keyboard("#add_item_no_topics", topics) user.temp_study_item = StudyItemDeck( user_id, user.get_highest_study_item_id() + 1, 0, subject) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_SUBJECT)]['done'])
def ADD_ITEM5(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger valid, should_continue, keyboard_option, keyboard_len = user.parse_keyboard_ans( msg) if valid == False: user.send_message("#choose_from_keyboard", markup=None) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_CONTINUE)]['error']) return if keyboard_option == 1: user.send_message("#ok") user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_CONTINUE)]['done']) return study_item = user.temp_study_item subject = study_item.get_subject() topic = study_item.get_topic() user.send_message("#item_topic", (topic, )) user.send_message("#add_item_ask_study_item", (subject, )) user.temp_study_item = StudyItemDeck( user_id, user.get_highest_study_item_id() + 1, 0, subject) user.temp_study_item.topic = topic user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_CONTINUE)]['continue'])
def answer_card(msg): """ Get user answer to card sequence """ user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger card_id = user.get_card_waiting() card = user.get_card(card_id) user.temp_card = card correctness = card.answer_comparison(msg.text) if correctness != "": user.send_message(correctness) user.send_card_answer(card) study_item_deck = user.get_study_item_deck(card.get_study_item_id()) user.send_all_cards(study_item_deck, except_type=card.get_question_type()) options = ['0', '1', '2', '3', '4', '5'] user.send_message("#grade_your_performance") user.send_string_keyboard("#poll_text", options, first_option_value=0) user.set_state(fsm.next_state[fsm.WAITING_ANS])
def ADD_ITEM4(msg): """ Add word: Get foreign word """ user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger study_item_deck = user.temp_study_item filename = "study_item" if debug_mode: debug_mode_text = "_debug" else: debug_mode_text = "" path = user.save_image( msg, "../data{}/{}/{}/".format(debug_mode_text, user_id, study_item_deck.study_item_id), filename) user.temp_study_item.set_study_item(path, 1) options = ['Send text'] user.send_selection_inline_keyboard( "#add_item_relate_menu", options, translate_options=True, empty_keyboard_text="#add_item_relate_menu_empty", no_empty_flag=True) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_IMAGE)]['done'])
def erase_words(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger valid, subject, keyboard_option, keyboard_len = user.parse_keyboard_ans( msg) if valid == False: user.send_message("#choose_from_options") user.set_state(fsm.next_state[(fsm.ERASE, fsm.GET_OPTION)]['error']) return known_subjects = user.get_only_subjects() if keyboard_option == 0: user.send_selection_inline_keyboard("#erase_select_subjects", known_subjects) user.set_state(fsm.next_state[(fsm.ERASE, fsm.GET_OPTION)]['subject']) elif keyboard_option == 1: user.send_string_keyboard("#subject_selection", known_subjects) user.set_state(fsm.next_state[(fsm.ERASE, fsm.GET_OPTION)]['topic']) elif keyboard_option == 2: user.send_string_keyboard("#subject_selection", known_subjects) user.set_state(fsm.next_state[(fsm.ERASE, fsm.GET_OPTION)]['study_item'])
def ADD_ITEM2(msg): """ Add word: Get topic """ user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger valid, topic, keyboard_option, keyboard_len = user.parse_keyboard_ans( msg) if len(topic) >= 45: user.send_message("#character_exceeded", (45, len(topic))) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_TOPIC)]['error']) return if len(topic) == 0: user.send_message("#character_null") user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_TOPIC)]['error']) return subject = user.temp_study_item.subject user.temp_study_item.topic = topic user.send_message("#item_topic", (topic, )) user.send_message("#add_item_ask_study_item", (subject, )) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_TOPIC)]['done'])
def callback_select_words(call): user_id = get_id(call.message) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger done, btn_set, btn = user.parse_keyboard_ans(call) if done == True: subject = user.temp_subject user.set_subject_active(subject, 0) txt = translation.translate("#active_topics_listing", user.get_native_language()) + "\n" txt_args = (subject, ) for i in btn_set: user.set_topic_active(subject, btn[i][0], 1) txt += "*.%s*" txt_args += (btn[i][0], ) if len(txt_args) == 1: txt += translation.translate("#no_active_topics", user.get_native_language()) user.send_message(txt, txt_args=txt_args, translate_flag=False) user.set_state(fsm.next_state[(fsm.SELECT_TRAINING, fsm.GET_TOPICS)]['done']) else: user.set_state(fsm.next_state[(fsm.SELECT_TRAINING, fsm.GET_TOPICS)]['continue'])
def review1(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger valid, subject, keyboard_option, keyboard_len = user.parse_keyboard_ans( msg) if valid == False: user.send_message("#choose_from_keyboard", markup=None) user.set_state(fsm.next_state[(fsm.REVIEW, fsm.GET_SUBJECT)]['error']) return user.temp_subject = subject known_topics = user.get_topics(subject) topics = [] for topic in known_topics: topics.append(topic[0]) topics.sort() user.send_selection_inline_keyboard( "#review_select_topics", topics, empty_keyboard_text="#review_select_topics_empty", no_empty_flag=True) user.set_state(fsm.next_state[(fsm.REVIEW, fsm.GET_SUBJECT)]['done'])
def review2(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger valid, number, keyboard_option, keyboard_len = user.parse_keyboard_ans( msg) if valid == False: user.send_message("#choose_from_keyboard", markup=None) user.set_state(fsm.next_state[(fsm.REVIEW, fsm.GET_NUMBER)]['error']) return try: user.counter = int(number) except ValueError: user.counter = len(user.cards_to_review) user.pos = 0 user.review_card_number = 1 shuffle(user.cards_to_review) user.send_card_query(user.cards_to_review[0], "Review", user.review_card_number) user.temp_card = user.cards_to_review[0] user.set_state(fsm.next_state[(fsm.REVIEW, fsm.GET_NUMBER)]['done'])
def audio_option(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger valid, option, keyboard_option, keyboard_len = user.parse_keyboard_ans( msg) if valid == False: user.send_message("#choose_from_keyboard", markup=None) user.set_state(fsm.next_state[(fsm.REVIEW, fsm.AUDIO_OPT)]['error']) return if keyboard_option == 1: cards_list = [] for card in user.cards_to_review: if card.get_question_type() != 'audio': cards_list.append(card) user.cards_to_review = cards_list user.send_message('#review_length', (len(user.cards_to_review), )) options = [ '5', '10', '15', '20', '25', '40', '50', user.translate('#all_cards') ] user.send_string_keyboard("#review_card_number", options) user.set_state(fsm.next_state[(fsm.REVIEW, fsm.AUDIO_OPT)]['done'])
def handle_help(bot, user_manager, debug_mode): #=====================HELP===================== @bot.message_handler(func=lambda msg: user_manager.get_user(get_id(msg)). get_state() == fsm.IDLE, commands=['help']) def help(msg): user = user_manager.get_user(get_id(msg)) user.send_message("#help_msg")
def overwrite_check(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger valid, overwrite, keyboard_option, keyboard_len = user.parse_keyboard_ans( msg) if valid == False: user.send_message("#choose_from_keyboard", markup=None) user.set_state(fsm.next_state[(fsm.COPY_FROM_USER, fsm.GET_OVERWRITE)]['error']) return if keyboard_option == 0: overwrite = True else: overwrite = False selected_topics = user.temp_topics_list text = translation.translate('#copy_results', user.get_native_language()) + "\n" user.send_message(text, translate_flag=False) count = 0 for topic in selected_topics: txt_args = () text = "" copied, overwritten_items = user_manager.copy_topic( user, user.temp_user, user.temp_subject, topic, overwrite) text += translation.translate('#topic', user.get_native_language()) + "\n" txt_args += (topic, ) if len(overwritten_items) > 0: text += translation.translate( '#overwritten_items', user.get_native_language()) + "\n" count += 1 for item in overwritten_items: text += '_.%s_\n' txt_args += (item, ) if len(copied) > 0: text += translation.translate( '#copied_items', user.get_native_language()) + "\n" count += 1 for item in copied: text += '_.%s_\n' txt_args += (item, ) print(text) user.send_message(text, txt_args, translate_flag=False) user.set_state(fsm.next_state[(fsm.COPY_FROM_USER, fsm.GET_OVERWRITE)]['done'])
def handle_message_not_understood(bot, user_manager, debug_mode): #=====================MESSAGE NOT UNDERSTOOD===================== @bot.message_handler( func=lambda msg: (True and user_manager.get_user(get_id(msg)).get_active() == 1)) def msg_not_undestood(msg): """ Change setting sequence """ user_id = get_id(msg) user = user_manager.get_user(get_id(msg)) user.send_message("#message_not_understood", txt_args=(msg.text, )) @bot.callback_query_handler(func=lambda call: (True and user_manager.get_user( get_id(call.message)).get_active() == 1)) def callback_not_understood(call): user_id = get_id(call.message)
def copy_from_user(msg): """ Copy words from user """ user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger user.send_message("#copy_get_telegram_username") user.set_state(fsm.next_state[fsm.IDLE]['copy_from_user'])
def stop(msg): """ Stop: stop receiving cards """ user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger user.set_active(0) user.send_message("#stop_msg") user.set_state(fsm.IDLE)
def ADD_ITEM3(msg): """ Add word: Get foreign word """ user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger study_item_text = string_treating.treat_special_chars(msg.text) if len(study_item_text) >= 190: user.send_message("#character_exceeded", (190, len(study_item_text))) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_STUDY_ITEM)]['error']) return if len(study_item_text) == 0: user.send_message("#character_null") user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_STUDY_ITEM)]['error']) return if study_item_text == "&img": user.send_message("#add_item_get_study_item_1"), user.set_state( fsm.next_state[(fsm.ADD_ITEM, fsm.GET_STUDY_ITEM)]['study item 1']) return user.temp_study_item.set_study_item(study_item_text, 0) study_item_deck = user.temp_study_item exist, aux_study_item_id = user.check_study_item_existence( study_item_deck.subject, study_item_deck.topic, study_item_deck.study_item) if exist == True: user.send_message("#add_item_study_item_already_exists") user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_STUDY_ITEM)]['error_idle']) return options = ['Send text', 'Send image', 'Send audio'] user.send_selection_inline_keyboard( "#add_item_relate_menu", options, translate_options=True, empty_keyboard_text="#add_item_relate_menu_empty", no_empty_flag=True) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.GET_STUDY_ITEM)]['done'])
def handle_user_dont_exist(bot, user_manager, debug_mode): #=====================USER DOESN'T EXIST - MSG===================== @bot.message_handler(func=lambda msg: user_manager.check_user_existence( get_id(msg)) == False, content_types=['text', 'photo', 'audio', 'voice']) def user_existence(msg): user_id = get_id(msg) bot = user_manager.bot_controller_factory.get_bot_controller( user_id, 0) bot.send_message("#user_dont_exist_error_handling") bot.stop_instance() #=====================USER DOESN'T EXIST - CALLBACK===================== @bot.callback_query_handler( func=lambda call: user_manager.check_user_existence( get_id(call.message)) == False) def callback_user_existence(call): user_id = get_id(call.message) bot = user_manager.bot_controller_factory.get_bot_controller( user_id, 0) bot.send_message("#user_dont_exist_error_handling") bot.stop_instance()
def handle_add_item_audio(bot, user_manager, debug_mode): @bot.message_handler(func = lambda msg: user_manager.get_user(get_id(msg)).get_state() == (fsm.ADD_ITEM, fsm.SEND_AUDIO), content_types=['audio', 'voice']) def add_audio(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger study_item = user.temp_study_item card_id = user.get_highest_card_id() + 1 + len(study_item.cards) filename = card_id data_path = "../data/{}/{}/".format(user_id, study_item.get_study_item_id()) if debug_mode: data_path = "../data_debug/{}/{}/".format(user_id, study_item.get_study_item_id()) path = "" if msg.audio != None: path = user.save_audio(msg, data_path, "{}".format(filename)) elif msg.voice != None: path = user.save_voice(msg, data_path, "{}".format(filename)) #print(path) card = Card.from_study_info(study_item, card_id, path, 'audio') user.temp_study_item.set_card(card) #print(str(user.temp_study_item)) user.send_message("#audio_received") if user.receive_queue.empty(): message_handlers.add_item.save_word(user) subject = user.temp_study_item.get_subject() topic = user.temp_study_item.get_topic() options = ['Yes', 'No'] user.send_string_keyboard("#add_item_ask_more_items", options, (subject, topic), translate_options=True) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.SEND_AUDIO)]['done']) else: content_type = user.receive_queue.get() message_handlers.add_item.prepare_to_receive(user, content_type) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.SEND_AUDIO)][content_type])
def handle_cancel(bot, user_manager, debug_mode): #=====================CANCEL===================== @bot.message_handler( func=lambda msg: (user_manager.get_user(get_id(msg)).not_locked( ) and user_manager.get_user(get_id(msg)).get_active() == 1), commands=['cancel']) def cancel(msg): """ Cancels any ongoing events for the user. """ user_id = get_id(msg) user = user_manager.get_user(user_id) prev_state = user.get_state() user.set_state(fsm.LOCKED) logger = user.logger if (prev_state == fsm.WAITING_ANS or prev_state == fsm.WAITING_POLL_ANS): user.set_card_waiting(0) user.send_message("#cancel") user.set_state(fsm.IDLE)
def cancel(msg): """ Cancels any ongoing events for the user. """ user_id = get_id(msg) user = user_manager.get_user(user_id) prev_state = user.get_state() user.set_state(fsm.LOCKED) logger = user.logger if (prev_state == fsm.WAITING_ANS or prev_state == fsm.WAITING_POLL_ANS): user.set_card_waiting(0) user.send_message("#cancel") user.set_state(fsm.IDLE)
def handle_add_item_text(bot, user_manager, debug_mode): @bot.message_handler( func=lambda msg: user_manager.get_user(get_id(msg)).get_state() == (fsm.ADD_ITEM, fsm.SEND_TEXT), content_types=['text']) def add_word8(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger text_add = msg.text.strip() if len(text_add) >= 290: user.send_message("#character_exceeded", (290, len(text_add))) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.SEND_TEXT)]['error']) return study_item = user.temp_study_item card_id = user.get_highest_card_id() + 1 + len(study_item.cards) card = Card.from_study_info(study_item, card_id, text_add, 'text') user.temp_study_item.set_card(card) #print(str(user.temp_study_item)) user.send_message("#text_received") if user.receive_queue.empty(): message_handlers.add_item.save_word(user) subject = user.temp_study_item.get_subject() topic = user.temp_study_item.get_topic() options = ['Yes', 'No'] user.send_string_keyboard("#add_item_ask_more_items", options, (subject, topic), translate_options=True) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.SEND_TEXT)]['done']) else: content_type = user.receive_queue.get() message_handlers.add_item.prepare_to_receive(user, content_type) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.SEND_TEXT)][content_type])
def add_word1(msg): """ Add word: Get word's language """ user = user_manager.get_user(get_id(msg)) user.set_state(fsm.LOCKED) valid, language, keyboard_opt, keyboard_len = user.parse_keyboard_ans(msg) if valid == False: user.send_message("#setup_user_choose_from_keyboard", markup=None) user.set_state(fsm.next_state[(fsm.SETUP_USER, fsm.GET_LANGUAGE)]['error']) return user.set_native_language(language) user.send_message("#welcome") user.set_state(fsm.next_state[(fsm.SETUP_USER, fsm.GET_LANGUAGE)]['done'])
def list_words(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger options = user.get_active_subjects() if len(options) == 0: user.send_message("#no_active_subjects") user.set_state(fsm.IDLE) return markdown_options = ["_"] * len(options) + ["*"] user.send_navigation_string_keyboard("#list_subject_selection", options=options, markdown_options=markdown_options, end_btn="End selection") user.set_state(fsm.next_state[fsm.IDLE]['list'])
def erase_words(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger known_subjects = user.get_only_subjects() if len(known_subjects) == 0: user.send_message("#no_subjects") user.set_state(fsm.IDLE) return options = ['Subjects', 'Topics', 'Study Items'] user.send_string_keyboard("#ask_what_to_erase", options, translate_options=True) user.set_state(fsm.next_state[fsm.IDLE]['erase'])
def handle_stop(bot, user_manager, debug_mode): #=====================STOP===================== @bot.message_handler(func = lambda msg: user_manager.get_user(get_id(msg)).get_state() == fsm.IDLE, commands = ['stop']) def stop(msg): """ Stop: stop receiving cards """ user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger user.set_active(0) user.send_message("#stop_msg") user.set_state(fsm.IDLE)
def callback_select_words(call): user_id = get_id(call.message) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger done, btn_set, btn = user.parse_keyboard_ans(call) if done == True: user.receive_queue = Queue() for i in btn_set: user.receive_queue.put(btn[i][1]) content_type = user.receive_queue.get() prepare_to_receive(user, content_type) user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.RELATE_MENU)][content_type]) else: user.set_state(fsm.next_state[(fsm.ADD_ITEM, fsm.RELATE_MENU)]['continue'])
def get_user(msg): user_id = get_id(msg) user = user_manager.get_user(user_id) user.set_state(fsm.LOCKED) logger = user.logger username = string_treating.treat_username_str(msg.text) valid, user2 = user_manager.get_user_by_username(username) user.temp_user = user2 if valid == False: user.send_message("#invalid_username") user.set_state(fsm.next_state[(fsm.COPY_FROM_USER, fsm.GET_USER)]['error']) return if user.get_id() == user2.get_id(): user.send_message("#invalid_yourself_username") user.set_state(fsm.next_state[(fsm.COPY_FROM_USER, fsm.GET_USER)]['error']) return public = user2.get_public() if public == False: user.send_message("#invalid_private_username") user.set_state(fsm.next_state[(fsm.COPY_FROM_USER, fsm.GET_USER)]['error']) return options = user2.get_only_subjects() if len(options) == 0: user.send_message("#copy_no_subjects", (user2.get_username(), )) user.set_state(fsm.IDLE) return user.send_string_keyboard("#subject_selection", options) user.set_state(fsm.next_state[(fsm.COPY_FROM_USER, fsm.GET_USER)]['done'])