def edit_pack_name(bot, update): state = _states[user(update)] pack_info = queries.get_pack(state['pack_id']) if pack_info['owner_id'] != user(update): update.message.reply_text(say.access_denied) return CHOOSE_PACK_ACTION update.message.reply_text(say.choose_pack_name) return EDIT_PACK_NAME
def edit_pack_privacy(bot, update): state = _states[user(update)] pack_info = queries.get_pack(state['pack_id']) if pack_info['owner_id'] != user(update): update.message.reply_text(say.access_denied) return CHOOSE_PACK_ACTION update.message.reply_text(say.choose_pack_privacy, reply_markup=row_markup(PrivacyType.values())) return EDIT_PACK_PRIVACY
def delete_pack(bot, update): state = _states[user(update)] pack_info = queries.get_pack(state['pack_id']) if pack_info['owner_id'] != user(update): update.message.reply_text(say.access_denied) return choose_pack_action(bot, update) update.message.reply_text( say.pack_deletion_confirmation_prompt.format(pack_info['name'])) return DELETE_PACK
def edit_pack_name_h(bot, update): state = _states[user(update)] pack_info = queries.get_pack(state['pack_id']) if pack_info['owner_id'] != user(update): update.message.reply_text(say.access_denied) return choose_pack_action(bot, update) queries.update_pack_name(state['pack_id'], update.message.text) update.message.reply_text(say.pack_name_updated) return choose_pack_action(bot, update)
def pack_chosen(bot, update): pack_id = get_pack_id(update) if not pack_id: return choose_pack(bot, update) cards = queries.select_cards(user(update), pack_id) if not cards: send(update, say.pack_is_empty) return choose_pack(bot, update) states[user(update)] = cards return choose_mode(bot, update)
def accept_users_handle_state(bot, update): res = update.message.text.strip() # get user_id from state if res == "Accept": queries.make_user(states[user(update)].group_id, states[user(update).user_in_process]) else: queries.reject_user(states[user(update)].group_id, states[user(update).user_in_process]) return accept_users(bot, update)
def move(self, update): self.cards = self.wrong_answers self.wrong_answers = [] if self.first_go and self.type != ReviewTypes.PRACTICE: for i in self.right_answers: queries.update_card_data(user(update), i.id, 1) for i in self.wrong_answers: queries.update_card_data(user(update), i.id, 0) self.first_go = False self.shuffle()
def edit_pack_status_h(bot, update): state = _states[user(update)] pack_info = queries.get_pack(state['pack_id']) text = update.message.text if not CardStatusType.has(text): update.message.reply_text('Wrong privacy type') return edit_pack_status(bot, update) queries.update_pack_status(user(update), state['pack_id'], text) update.message.reply_text(say.pack_status_updated) return end(bot, update)
def end(bot, update): if not states[user(update)].wrong_answers: send(update, say.completed) review_quit(bot, update) return ConversationStates.QUIT send( update, say.inter_results(len(states[user(update)].right_answers), len(states[user(update)].wrong_answers))) states[user(update)].move(update) return ask(bot, update)
def language_chosen(bot, update): if update.message.text not in languages: send(update, say.incorrect_input) return choose_language(bot, update) states[user(update)].language = (0 if update.message.text == languages[0] else 1) states[user(update)].shuffle() return ask( bot, update) if states[user(update)].type != ReviewTypes.TRUST else ask( bot, update, start_markup)
def trust_check(bot, update): if update.message.text == start_markup[0]: pass elif update.message.text == trust_markup[0]: states[user(update)].right(update) else: states[user(update)].wrong(update, None) states[user(update)].store() send(update, states[user(update)].answer()) if not states[user(update)].cards: return end(bot, update) return ask(bot, update)
def notify_stats_handle(bot, update): res = update.message.text if res not in enums.NotificationType.values(): send(update, say.incorrect_input + say.choose_general_goal, markup=enums.NotificationType.values()) return NOTIFY_STATS states[user(update)].append(res) send(update, say.registration_completed) menu.head_menu(bot, update) queries.add_user(states.pop(user(update))) return ConversationHandler.END
def choose_pack_h(bot, update): colon_ind = update.message.text.find(':') try: pack_id = int(update.message.text[:colon_ind]) except ValueError: update.message.reply_text('Invalid') return CHOOSE_PACK if queries.if_added(user(update), pack_id): send(update, say.already_added) return CHOOSE_PACK queries.add_pack(user(update), pack_id) return end(bot, update)
def edit_pack_privacy_h(bot, update): state = _states[user(update)] pack_info = queries.get_pack(state['pack_id']) text = update.message.text if pack_info['owner_id'] != user(update): update.message.reply_text(say.access_denied) return choose_pack_action(bot, update) if not PrivacyType.has(text): update.message.reply_text('Wrong privacy type') return edit_pack_privacy(bot, update) queries.update_pack_privacy(state['pack_id'], text) update.message.reply_text(say.pack_privacy_updated) return end(bot, update)
def delete_pack_h(bot, update): state = _states[user(update)] pack_info = queries.get_pack(state['pack_id']) if pack_info['owner_id'] != user(update): update.message.reply_text(say.access_denied) return choose_pack_action(bot, update) if update.message.text != say.pack_deletion_confirmation.format( pack_info['name']): update.message.reply_text('Cancelled deletion') return choose_pack_action(bot, update) queries.delete_pack(state['pack_id']) update.message.reply_text(say.pack_deleted) return choose_pack(bot, update)
def choose_pack_action_h(bot, update): state = _states[user(update)] text = update.message.text if text == 'Edit name': return edit_pack_name(bot, update) if text == 'Edit privacy': return edit_pack_privacy(bot, update) if text == 'Edit pack status': return edit_pack_status(bot, update) if text == 'Delete pack': return delete_pack(bot, update) if text == 'Do exercise': # TODO: Save selected pack id for /menu handler update.message.reply_text(say.not_implemented) update.message.reply_text(say.use_begin) return end(bot, update) if text == 'Show cards': return choose_card(bot, update) update.message.reply_text(say.not_recognized) return CHOOSE_PACK_ACTION
def choose_pack(bot, update): packs = queries.active_packs(user(update)) if not packs: send(update, say.no_packs_available) return ConversationStates.QUIT send(update, say.enumerated(packs)) return ConversationStates.CHOOSE_PACK
def review_quit(bot, update): try: states.pop(user(update)) except KeyError: pass # send(update, "Quitting...") menu.head_menu(bot, update) return ConversationHandler.END
def notify_learn_handle(bot, update): res = update.message.text if res not in enums.NotificationType.values(): send(update, say.incorrect_input + say.choose_learn_notifications, markup=enums.NotificationType.values()) return NOTIFY_LEARN states[user(update)].append(res) return notify_stats(update)
def general_goal_handle(bot, update): res = update.message.text if res not in enums.GenGoalType.values(): send(update, say.incorrect_input + say.choose_general_goal, markup=enums.GenGoalType.values()) return GENERAL_GOAL states[user(update)].append(res) return weekly_goal(update)
def choose_pack_h(bot, update): state = _states[user(update)] colon_ind = update.message.text.find(':') try: pack_id = int(update.message.text[:colon_ind]) except ValueError: update.message.reply_text('Invalid') return CHOOSE_PACK state['pack_id'] = pack_id return choose_pack_action(bot, update)
def weekly_goal_handle(bot, update): try: res = int(update.message.text) if not 1000 > res > 0: raise ValueError except (TypeError, ValueError): send(update, say.incorrect_weekly_goal) return WEEKLY_GOAL states[user(update)].append(res) return notify_learn(update)
def choose_privacy_h(bot, update): state = _states[user(update)] text = update.message.text if not PrivacyType.has(text): update.message.reply_text('Wrong privacy type') return CHOOSE_PRIVACY state['privacy'] = text return choose_pack_file(bot, update)
def choose_pack_action(bot, update): state = _states[user(update)] pack_id = state['pack_id'] pack_info = queries.get_pack(pack_id, user(update)) if not queries.has_pack_read_access(pack_id, user(update)): update.message.reply_text(say.access_denied) return CHOOSE_PACK markup = [] markup.append(['Do exercise', 'Show cards', 'Edit pack status']) if pack_info['owner_id'] == user(update): markup.append(['Edit name', 'Edit privacy', 'Delete pack']) update.message.reply_text( say.pack_info.format(pack_info['name'], pack_info['privacy'], pack_info['status']), reply_markup=ReplyKeyboardMarkup(markup, one_time_keyboard=True)) return CHOOSE_PACK_ACTION
def choose_pack_file_h(bot, update): state = _states[user(update)] file_id = update.message.document.file_id # Doesn't support pipe :( # TODO: Delete temp file bot.getFile(file_id).download('/tmp/learning_cards_' + str(file_id)) try: pack = packfile.load(open('/tmp/learning_cards_' + str(file_id))) except packfile.InvalidPack as e: update.message.reply_text(say.invalid_pack.format(e.line)) return CHOOSE_PACK_FILE pack = pack if len(pack) < 100 else pack[:100] pack_id = queries.new_pack( name=state['name'], owner=user(update), privacy=state['privacy'], cards=pack ) update.message.reply_text(say.pack_created.format(pack_id)) return END
def start(bot, update): user_id = user(update) name = update.message.from_user.username states[user_id] = [user_id, name] if queries.if_registered(user_id): send(update, say.hello) menu.head_menu(bot, update) return ConversationHandler.END send(update, say.welcome) return general_goal(update)
def choose_card(bot, update): state = _states[user(update)] cards = queries.get_all_cards_in_pack(state['pack_id']) cards_print = [ '{}: {} - {} - {}'.format(x['card_id'], x['front'], x['back'], x['comment']) for x in cards ] markup = [['Add card(s)', 'Export as file']] + \ [[x] for x in cards_print] update.message.reply_text('\n'.join(cards_print), reply_markup=ReplyKeyboardMarkup( markup, one_time_keyboard=True)) return CHOOSE_CARD
def check(bot, update): if states[user(update)].type == ReviewTypes.TRUST: return trust_check(bot, update) if states[user(update)].type == ReviewTypes.PRACTICE: states[user(update)].right(update) else: true_ans = states[user(update)].compare(update.message.text.strip()) if not true_ans: states[user(update)].right(update) else: states[user(update)].wrong(update, true_ans) if not states[user(update)].cards: return end(bot, update) return ask(bot, update)
def ask(bot, update, special_markup=None): if states[user(update)].type == ReviewTypes.TRUST: opts = trust_markup elif states[user(update)].type == ReviewTypes.ENTER: opts = None elif states[user(update)].type == ReviewTypes.TEST: opts = states[user(update)].test_markup() else: opts = practice_markup send(update, states[user(update)].ask(), markup=(special_markup or opts)) if states[user(update)].type != ReviewTypes.TRUST: states[user(update)].store() return ConversationStates.ITERATE
def res(bot, update): try: states.pop(user(update)) except KeyError: pass return func(bot, update)