def add_job(chat_id): user = crud.get_user(chat_id) if not user.run: job = scheduler.add_job(user_job, 'interval', [chat_id], seconds=user.interval) crud.update_job_id(chat_id, job.id) crud.update_run(chat_id, True)
def remove_job(chat_id): user = crud.get_user(chat_id) if user.run: job = scheduler.get_job(user.job_id) if job: scheduler.remove_job(user.job_id) crud.update_job_id(chat_id, None) crud.update_run(chat_id, False)
def change_interval(message): args = message.text.split() interval = args[1] if 1 < len(args) else None if interval and interval.isdigit(): crud.update_interval(message.chat.id, interval) user = crud.get_user(message.chat.id) if user.run: scheduler.remove_job(message.chat.id) scheduler.add_job(message.chat.id) else: bot.send_message(message.chat.id, help.interval_text)
def help_text(chat_id): user = crud.get_user(chat_id) return """Helping text: /run - launch notification /stop - stop notification /current - show current value /interval <seconds> - change interval. Current: {interval} /overweight <integer> - change overweight rate to notify. Current: {overweight} /min_value <integer> - change min border. Current: {min_value} /max_value <integer> - change max border. Current: {max_value} """.format(interval=user.interval, overweight=user.overweight, diff=user.diff, min_value=user.min_value, max_value=user.max_value)
def user_job(chat_id): user = crud.get_user(chat_id) new_value = coin.get_current_value() diff = new_value - user.value if abs(diff) > user.overweight: crud.update_value(chat_id, new_value) prefix = "Up" if diff > 0 else "Down" body = prefix + ": " + str(new_value) + " | Diff: " + str(int(diff)) bot.send_message(chat_id, body) if new_value > user.max_value: body = "Alert! Rose higher: " + str( user.max_value) + " | " + " Current: " + str(new_value) crud.update_max_value(chat_id, new_value) bot.send_message(chat_id, body) if new_value < user.min_value: body = "Alert! Fell below: " + str( user.min_value) + " | " + " Current: " + str(new_value) crud.update_min_value(chat_id, new_value) bot.send_message(chat_id, body)