def set_reminder_days(user_reminder: UserReminder, days_str: str) -> None: """Sets reminder days in table user_temp_reminder_data :param days_str: 1 from offered options for days, need to be converted using DAYS_VOCAB :param user_reminder: UserReminder object for current user :return: None """ user_id = user_reminder.user_id if user_reminder.temp_reminder.name == DEFAULT_NAME: bot.send_message( user_id, text='Please, start from command /create ReminderName') return days = int_from_str(days_str) if not days: bot.send_message( user_id, text=f'Input ({days}) is not valid. Please, choose days to' f' remind:\n /choose_days\n"Weekdays" - press 1\n' f'"Weekends" - press 2\n "Every day" - press 3\n') return chosen_days = DAYS_VOCAB[int(days)] cur_temp_reminder = user_reminder.temp_reminder cur_temp_reminder.days = chosen_days user_reminder.update( actions=[UserReminder.temp_reminder.set(cur_temp_reminder)]) bot.send_message( user_id, text='Please, type "/done" to finish creating this reminder')
def set_reminder_amount(user_reminder: UserReminder, number_str: str) -> None: """Sets reminder number in table temp_reminder :param number: amount of reminders per day :param user_reminder: UserReminder object for current user :return: None """ user_id = user_reminder.user_id if user_reminder.temp_reminder.name == DEFAULT_NAME: bot.send_message( user_id, text='Please, start from command /create ReminderName') return amount = int_from_str(number_str) if not amount: bot.send_message( user_id, text=f'Input "{number_str}" is not a number. Please, use ' f'command:\n "/set_amount Amount"') return cur_temp_reminder = user_reminder.temp_reminder cur_temp_reminder.amount = amount cur_temp_reminder.current_state = amount user_reminder.update( actions=[UserReminder.temp_reminder.set(cur_temp_reminder)]) bot.send_message(user_id, text='Please, choose frequency of remineders per day. ' 'Reminders will be sent between 9 am and 9 pm your time ' 'with a frequency you will choose. For example, 1 means ' 'every hour, 2 means every 2 hours and so on. ' 'Please, use command:\n "/set_frequency Frequency"')
def set_reminder_frequency(user_reminder: UserReminder, frequency_str: str) -> None: """Sets reminder frequency in temp_reminder :param frequency_str: frequency of reminders per day :param user_reminder: UserReminder object for current user :return: None """ user_id = user_reminder.user_id if user_reminder.temp_reminder.name == DEFAULT_NAME: bot.send_message( user_id, text='Please, start from command /create ReminderName') return frequency = int_from_str(frequency_str) if not frequency: bot.send_message( user_id, text=f'Input "{frequency}" is not a Number. Please, use ' f'command"\n "/set_frequency Frequency"') return cur_temp_reminder = user_reminder.temp_reminder cur_temp_reminder.frequency = int(frequency) user_reminder.update( actions=[UserReminder.temp_reminder.set(cur_temp_reminder)]) bot.send_message(user_id, text='Please, choose days to remind: \n/choose_days\n' '"Weekdays" - press ' '1\n"Weekends" - press 2\n "Every day" - press 3\n')
def set_timezone(user_reminder: UserReminder, message_time: str, time_hour: str) -> None: """Calculates timezone from user entered time and inserts it into table :param user_reminder: UserReminder object for current user :param time_hour: time entered by user :return: None """ if not time_hour or not time_hour.isdigit() or not 0 <= int(time_hour) <= 24: bot.send_message( user_reminder.user_id, text='Please, enter the hours between 0 and 24. ' 'Use command:\n /set_timezone Hours') return dt_object = datetime.fromtimestamp(int(message_time)) delta = int(time_hour) - dt_object.hour if abs(delta) > 12: delta -= 24 * sign(delta) #save delta to user user_reminder.update(actions=[ UserReminder.time_delta.set(delta) ]) bot.send_message( user_reminder.user_id, text='Great! You can create your new reminder now!\n\n' 'The reminder should have:\n- Name - give a meaningful name to ' 'help your motivation\n' '- Amount - how many times you want to exercise/repeat this task ' 'per day. For example, 50 push-ups\n' '- Frequency - how often you want to get reminded per day\n' '- Days - which days you want to exerice/repeat your task\n\n' 'Every day you can check how many exercises/repeats are left to do. ' 'We encourage you to send an update with a number of repeats you ' 'have done every day. So you can track how mush is left.\n\n' 'Start by using command:\n "/create ReminderName"')
def create_new_reminder(user_reminder: UserReminder, name: str) -> None: """Sets name to new reminder, all rest values are set to default :param name: name of reminder :param user_reminder: UserReminder object for current user :return: None """ if not name: bot.send_message( user_reminder.user_id, text='You have not entered the name. Please, use command ' '"/create ReminderName"') return for reminder in user_reminder.active_reminders: if reminder.name == name and not reminder.is_deleted: bot.send_message( user_reminder.user_id, text=f'There is already reminder called {name}. Please, choose ' 'another name. Use command /create ReminderName') return user_reminder.update(actions=[ UserReminder.temp_reminder.set( ReminderMap(reminder_id=DEFAULT_REMINDER_ID, name=name, amount=DEFAULT_AMOUNT, frequency=DEFAULT_FREQUENCY, days=DEFAULT_DAYS, current_state=DEFAULT_AMOUNT)) ]) bot.send_message( user_reminder.user_id, text=f'You started to create reminder called {name}. Choose ' 'number of times per day you want to repeat a task. ' 'Please, use command:\n "/set_amount Amount"')
def update_reminder(user_reminder: UserReminder, today_update: str) -> None: user_update = today_update.split() if len(user_update) != 2 or not user_update[1].isdigit(): bot.send_message( user_reminder.user_id, text='Please, enter a number of exercises/updates you did.' 'Use command:\n /today_amount Number') for reminder in user_reminder.active_reminders: if reminder.name == user_update[0]: reminder.current_state -= int(user_update[1]) user_reminder.update(actions=[ UserReminder.active_reminders.set(user_reminder.active_reminders) ])
def get_or_create_user_reminder(user_id: int, time: int) -> UserReminder: """Returns UserReminder object with data from existing element or creates a new one with specified user_id :param user_id: id of user of interest :param time: registration time for a new user, should be ignored for existing user :return: UserReminder object """ try: print(f'U_ID: {user_id}') user_reminder = UserReminder.get(user_id) except UserReminder.DoesNotExist: user_reminder = UserReminder(user_id=user_id, registration_time=time) user_reminder.save() return user_reminder
def cancel_reminder(user_reminder: UserReminder, name: str) -> None: for reminder in user_reminder.active_reminders: if reminder.name == name: reminder.is_deleted = True user_reminder.update(actions=[ UserReminder.active_reminders.set( user_reminder.active_reminders) ]) bot.send_message(user_reminder.user_id, text=f'Reminder "{name}" was canceled') return bot.send_message( user_reminder.user_id, text=f'There is no reminder called "{name}". Please, use command ' '/show_reminders to show existing reminders. And then use' ' command /cancel_reminder ReminderName')
def move_reminder_temp2active(user_reminder: UserReminder) -> None: """transfer temporarily created reminder to main table 'user_data' :param user_reminder: UserReminder object for current user :return: None """ chat_id = user_reminder.user_id user_reminder.temp_reminder.reminder_id = random.randint( 10**REMINDER_ID_EXPONENT_MIN, 10**REMINDER_ID_EXPONENT_MAX) user_reminder.update(actions=[ UserReminder.active_reminders.set( (UserReminder.active_reminders |[]).append( [user_reminder.temp_reminder]) )]) bot.send_message( chat_id, text=f'Reminder {user_reminder.temp_reminder.name}' f' was added successfully')
def set_reminder_frequency(user_reminder: UserReminder, frequency: str) -> None: """Sets reminder frequency in temp_reminder :param number: frequency of reminders per day :param user_reminder: UserReminder object for current user :return: None """ chat_id = user_reminder.user_id if frequency.strip().isdigit(): cur_temp_reminder = user_reminder.temp_reminder cur_temp_reminder.frequency = int(frequency) user_reminder.update(actions=[ UserReminder.temp_reminder.set(cur_temp_reminder) ]) bot.send_message( chat_id, text='Please, choose days to remind: \n/choose_days\n' '"Weekdays" - press ' '1\n"Weekends" - press 2\n "Every day" - press 3\n' '"Once a week" - press 4') else: bot.send_message(chat_id, text=f'Input ({frequency}) is not a Number' f'Use command"\n "/set_frequency Frequency"')
def set_reminder_days(user_reminder: UserReminder, days: str) -> None: """Sets reminder days in table user_temp_reminder_data :param days: 1 from offered options for days, need to be converted using DAYS_VOCAB :param user_reminder: UserReminder object for current user :return: None """ chat_id = user_reminder.user_id if days.strip().isdigit() and int(days) in DAYS_VOCAB: chosen_days = DAYS_VOCAB[int(days)] cur_temp_reminder = user_reminder.temp_reminder cur_temp_reminder.days = chosen_days user_reminder.update(actions=[ UserReminder.temp_reminder.set(cur_temp_reminder) ]) bot.send_message( chat_id, text='Please, type "/set" to finish creating this reminder') else: bot.send_message(chat_id, text=f'Input ({days}) is not a valid choice' 'Please, choose days to remind:\n /choose_days\n' '"Weekdays" - press ' '1\n"Weekends" - press 2\n "Every day" - press 3\n' '"Once a week" - press 4')
def update_reminder(user_reminder: UserReminder, today_update: str) -> None: """Updates reminder if it exists""" reminder_to_update_name, value_to_update = parse_update_args(today_update) if not reminder_to_update_name: bot.send_message(user_reminder.user_id, text='Enter a number of exercises/updates you did. ' 'Please, use command:\n /today_amount Name Amount') return for reminder in user_reminder.active_reminders: if reminder.name == reminder_to_update_name and not reminder.is_deleted: reminder.current_state -= value_to_update user_reminder.update(actions=[ UserReminder.active_reminders.set( user_reminder.active_reminders) ]) bot.send_message( user_reminder.user_id, text=f'Great! You have only {reminder.current_state} left to ' f'do today') return bot.send_message( user_reminder.user_id, text=f'There is no reminder {reminder_to_update_name}. Please, use ' f'command /show_reminders to show existing reminders.')
def move_reminder_temp2active(user_reminder: UserReminder) -> None: """transfer temporarily created reminder to main table 'user_data' :param user_reminder: UserReminder object for current user :return: None """ user_id = user_reminder.user_id if user_reminder.temp_reminder.name == DEFAULT_NAME: bot.send_message( user_id, text='Please, start from command /create ReminderName') return user_reminder.temp_reminder.reminder_id = random.randint( 10**REMINDER_ID_EXPONENT_MIN, 10**REMINDER_ID_EXPONENT_MAX) user_reminder.update(actions=[ UserReminder.active_reminders.set(( UserReminder.active_reminders | []).append([user_reminder.temp_reminder])) ]) bot.send_message(user_id, text=f'Reminder {user_reminder.temp_reminder.name}' f' was added successfully') user_reminder.temp_reminder.name = DEFAULT_NAME user_reminder.update( actions=[UserReminder.temp_reminder.set(user_reminder.temp_reminder)])
def set_reminder_number(user_reminder: UserReminder, number: str) -> None: """Sets reminder number in table temp_reminder :param number: amount of reminders per day :param user_reminder: UserReminder object for current user :return: None """ chat_id = user_reminder.user_id if number.strip().isdigit(): cur_temp_reminder = user_reminder.temp_reminder cur_temp_reminder.amount = int(number) cur_temp_reminder.current_state = int(number) user_reminder.update(actions=[ UserReminder.temp_reminder.set(cur_temp_reminder) ]) bot.send_message( chat_id, text=f'Please, choose frequency of remineders per day. ' f'Reminders will be send from 9 till 21 your time with ' f'a frequency you will choose. For example, 12 means ' f'every hour. ' f'Use command:\n "/set_frequency Frequency"') else: bot.send_message(chat_id, text=f'Input ({number}) is not a number. ' f'Use command:\n "/set_amount Number"')
def send_reminders() -> None: users = UserReminder.scan() for user in users: hour_in_user = (datetime.now() + timedelta(hours=user.time_delta)).hour user_reminders = [] print(f'hour_in_user={hour_in_user}') for reminder in user.active_reminders: print(f'reminder.start_time_each_day={reminder.start_time_each_day}' f'reminder.end_time_each_day={reminder.end_time_each_day}') if (reminder.start_time_each_day <= hour_in_user and hour_in_user < reminder.end_time_each_day): user_reminders.append(f'You have a task called {reminder.name}\n' f'to repeat {reminder.amount} times.') message = "\n".join(user_reminders) bot.send_message(user.user_id, text=message)
def send_reminders() -> None: users = UserReminder.scan() for user in users: hour_user = (datetime.now() + timedelta(hours=user.time_delta)).hour day_user = (datetime.now() + timedelta(hours=user.time_delta)).weekday() user_reminders = [] print(f'hour_user={hour_user}') for reminder in user.active_reminders: print(f'is_deleted={reminder.is_deleted}') #print(f'reminder.start_time_each_day={reminder.start_time_each_day} ' # f'reminder.end_time_each_day={reminder.end_time_each_day} ') rest_amount = reminder.amount - reminder.current_state if if_alert( day2remind=reminder.days, hour_user=hour_user, day_user=day_user, start=reminder.start_time_each_day, end=reminder.end_time_each_day, freq=reminder.frequency, rest=rest_amount, is_deleted=reminder.is_deleted): user_reminders.append( f'You have a task called {reminder.name}\n' f'to repeat {rest_amount} times.') message = "\n".join(user_reminders) print(f'message={message}') if message: bot.send_message(user.user_id, text=message)