option to delete all the data form the settings

This commit is contained in:
cupcakearmy 2019-12-23 02:20:26 +01:00
parent 29aa9cc366
commit 33877495fd
2 changed files with 37 additions and 5 deletions

View File

@ -1,9 +1,10 @@
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardRemove, ParseMode from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardRemove, ParseMode
from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler, CallbackContext from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler, CallbackContext
from commands.other import stop_handler
from constants import Section from constants import Section
MENU, API_KEY, ENABLED, DELETE = range(4) MENU, API_KEY, ENABLED, DELETE = map(chr, range(4))
def init(update: Update, context: CallbackContext): def init(update: Update, context: CallbackContext):
@ -25,7 +26,7 @@ def show_menu(update: Update, context: CallbackContext):
] ]
update.effective_user.send_message( update.effective_user.send_message(
'_Current settings:_\n' '_Current settings:_\n'
f'API Key: *{context.user_data.get(Section.API_Key.value, "No Api key set")}*\n' f'API Key: *{context.user_data.get(Section.API_Key.value, "No API key set")}*\n'
f'Global auto updates: *{context.user_data[Section.Enabled.value]}*\n' f'Global auto updates: *{context.user_data[Section.Enabled.value]}*\n'
'\nWhat settings do you want to configure?', '\nWhat settings do you want to configure?',
parse_mode=ParseMode.MARKDOWN, parse_mode=ParseMode.MARKDOWN,
@ -36,7 +37,7 @@ def show_menu(update: Update, context: CallbackContext):
def menu(update: Update, context: CallbackContext): def menu(update: Update, context: CallbackContext):
selected = int(update.callback_query.data) selected = update.callback_query.data
context.bot.delete_message( context.bot.delete_message(
chat_id=update.callback_query.message.chat_id, chat_id=update.callback_query.message.chat_id,
@ -47,10 +48,40 @@ def menu(update: Update, context: CallbackContext):
return show_api_key(update, context) return show_api_key(update, context)
elif selected == ENABLED: elif selected == ENABLED:
toggle_enabled(update, context) toggle_enabled(update, context)
elif selected == DELETE:
return show_delete(update, context)
else: else:
return ConversationHandler.END return ConversationHandler.END
def show_delete(update: Update, context: CallbackContext):
keyboard = [
[InlineKeyboardButton('Yes', callback_data='1')],
[InlineKeyboardButton('Back', callback_data='0')],
]
update.effective_user.send_message(
'🗑 Are you sure? Cannot be undone!',
reply_markup=InlineKeyboardMarkup(keyboard)
)
return DELETE
def delete(update: Update, context: CallbackContext):
selected = update.callback_query.data
context.bot.delete_message(
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
)
if selected == '1':
stop_handler(update, context)
return ConversationHandler.END
else:
return show_menu(update, context)
def show_api_key(update: Update, context: CallbackContext): def show_api_key(update: Update, context: CallbackContext):
update.effective_user.send_message( update.effective_user.send_message(
'Send me your API Key 🙂' 'Send me your API Key 🙂'
@ -85,8 +116,9 @@ config_handler = ConversationHandler(
MENU: [CallbackQueryHandler(menu)], MENU: [CallbackQueryHandler(menu)],
API_KEY: [ API_KEY: [
CommandHandler('cancel', cancel), CommandHandler('cancel', cancel),
MessageHandler(Filters.all, api_key), MessageHandler(Filters.text, api_key),
], ],
DELETE: [CallbackQueryHandler(delete)]
}, },
fallbacks=[CommandHandler('cancel', cancel)] fallbacks=[CommandHandler('cancel', cancel)]
) )

View File

@ -28,7 +28,7 @@ def help_handler(update: Update, context: CallbackContext):
def stop_handler(update: Update, context: CallbackContext): def stop_handler(update: Update, context: CallbackContext):
context.user_data.clear() context.user_data.clear()
update.message.reply_text('You and your data were deleted 🗑') update.effective_user.send_message('You and your data were deleted 🗑')
@run_async @run_async