diff --git a/src/commands/config.py b/src/commands/config.py index 9567184..7df667b 100644 --- a/src/commands/config.py +++ b/src/commands/config.py @@ -3,18 +3,20 @@ from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHa from utils import Section -MENU, API_KEY, FREQUENCY = range(3) +MENU, API_KEY, FREQUENCY, ENABLED = range(4) def show_menu(update: Update, context: CallbackContext): keyboard = [ [InlineKeyboardButton('API Key', callback_data=API_KEY)], + [InlineKeyboardButton('Auto Updates', callback_data=ENABLED)], [InlineKeyboardButton('Frequency', callback_data=FREQUENCY)], [InlineKeyboardButton('Done', callback_data=ConversationHandler.END)], ] update.effective_user.send_message( '_Current settings:_\n' f'API Key: *{context.user_data[Section.API_Key.value]}*\n' + f'Auto Updates: *{context.user_data[Section.Enabled.value]}*\n' f'Frequency: *{context.user_data[Section.Frequency.value]}*\n' '\nWhat settings do you want to configure?', parse_mode=ParseMode.MARKDOWN, @@ -73,6 +75,8 @@ def menu(update: Update, context: CallbackContext): return show_menu_api_key(update, context) elif selected == FREQUENCY: return show_menu_frequency(update, context) + elif selected == ENABLED: + toggle_enabled(update, context) else: return ConversationHandler.END @@ -100,6 +104,14 @@ def set_frequency(update: Update, context: CallbackContext): return show_menu(update, context) +def toggle_enabled(update: Update, context: CallbackContext): + new = not context.user_data.setdefault(Section.Enabled.value, True) + context.user_data[Section.Enabled.value] = new + update.effective_user.send_message('Auto updates enabled' if new else 'Auto updates disabled') + + return show_menu(update, context) + + def cancel(update: Update, context: CallbackContext): update.message.reply_text('Canceled', reply_markup=ReplyKeyboardRemove()) return ConversationHandler.END diff --git a/src/commands/other.py b/src/commands/other.py index 9a9df65..2cee468 100644 --- a/src/commands/other.py +++ b/src/commands/other.py @@ -84,11 +84,14 @@ def send_updates(context: CallbackContext): now = current_timestamp() for user, user_data in persistence.user_data.items(): + enabled = user_data.setdefault(Section.Enabled.value, True) last_run = user_data.setdefault(Section.LastRun.value, 0) frequency = parse(user_data.setdefault(Section.Frequency.value, '1d')) - if last_run + frequency < now: + if enabled and last_run + frequency < now: delta = now - user_data.setdefault(Section.Interval.value, delta_timestamp(days=365)) send_update_to_user(user=user, delta=delta) + except Exception as e: + print(e) finally: SENDING = False