mirror of
https://github.com/cupcakearmy/mercatus.git
synced 2024-12-22 16:16:32 +00:00
ability to toggle auto updates
This commit is contained in:
parent
59d5210372
commit
6f0930a0cc
@ -3,18 +3,20 @@ from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHa
|
|||||||
|
|
||||||
from utils import Section
|
from utils import Section
|
||||||
|
|
||||||
MENU, API_KEY, FREQUENCY = range(3)
|
MENU, API_KEY, FREQUENCY, ENABLED = range(4)
|
||||||
|
|
||||||
|
|
||||||
def show_menu(update: Update, context: CallbackContext):
|
def show_menu(update: Update, context: CallbackContext):
|
||||||
keyboard = [
|
keyboard = [
|
||||||
[InlineKeyboardButton('API Key', callback_data=API_KEY)],
|
[InlineKeyboardButton('API Key', callback_data=API_KEY)],
|
||||||
|
[InlineKeyboardButton('Auto Updates', callback_data=ENABLED)],
|
||||||
[InlineKeyboardButton('Frequency', callback_data=FREQUENCY)],
|
[InlineKeyboardButton('Frequency', callback_data=FREQUENCY)],
|
||||||
[InlineKeyboardButton('Done', callback_data=ConversationHandler.END)],
|
[InlineKeyboardButton('Done', callback_data=ConversationHandler.END)],
|
||||||
]
|
]
|
||||||
update.effective_user.send_message(
|
update.effective_user.send_message(
|
||||||
'_Current settings:_\n'
|
'_Current settings:_\n'
|
||||||
f'API Key: *{context.user_data[Section.API_Key.value]}*\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'
|
f'Frequency: *{context.user_data[Section.Frequency.value]}*\n'
|
||||||
'\nWhat settings do you want to configure?',
|
'\nWhat settings do you want to configure?',
|
||||||
parse_mode=ParseMode.MARKDOWN,
|
parse_mode=ParseMode.MARKDOWN,
|
||||||
@ -73,6 +75,8 @@ def menu(update: Update, context: CallbackContext):
|
|||||||
return show_menu_api_key(update, context)
|
return show_menu_api_key(update, context)
|
||||||
elif selected == FREQUENCY:
|
elif selected == FREQUENCY:
|
||||||
return show_menu_frequency(update, context)
|
return show_menu_frequency(update, context)
|
||||||
|
elif selected == ENABLED:
|
||||||
|
toggle_enabled(update, context)
|
||||||
else:
|
else:
|
||||||
return ConversationHandler.END
|
return ConversationHandler.END
|
||||||
|
|
||||||
@ -100,6 +104,14 @@ def set_frequency(update: Update, context: CallbackContext):
|
|||||||
return show_menu(update, context)
|
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):
|
def cancel(update: Update, context: CallbackContext):
|
||||||
update.message.reply_text('Canceled', reply_markup=ReplyKeyboardRemove())
|
update.message.reply_text('Canceled', reply_markup=ReplyKeyboardRemove())
|
||||||
return ConversationHandler.END
|
return ConversationHandler.END
|
||||||
|
@ -84,11 +84,14 @@ def send_updates(context: CallbackContext):
|
|||||||
now = current_timestamp()
|
now = current_timestamp()
|
||||||
|
|
||||||
for user, user_data in persistence.user_data.items():
|
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)
|
last_run = user_data.setdefault(Section.LastRun.value, 0)
|
||||||
frequency = parse(user_data.setdefault(Section.Frequency.value, '1d'))
|
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))
|
delta = now - user_data.setdefault(Section.Interval.value, delta_timestamp(days=365))
|
||||||
send_update_to_user(user=user, delta=delta)
|
send_update_to_user(user=user, delta=delta)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
finally:
|
finally:
|
||||||
SENDING = False
|
SENDING = False
|
||||||
|
Loading…
Reference in New Issue
Block a user