From cbfde03e620188d80213a8b15a4dffdb8948b257 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Thu, 23 Feb 2023 21:18:49 -0800 Subject: bot: implement /logout command --- bot.py | 2 ++ command.py | 5 +++-- config.py | 5 +++++ dbstore/peewee_store.py | 5 +++++ prompt/string.py | 1 + 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index 6479460..aea62af 100644 --- a/bot.py +++ b/bot.py @@ -124,6 +124,8 @@ async def main() -> None: Application.builder().updater(None).token(BOT_TOKEN).context_types(context_types).build() ) + # TODO: + # check user login status before invoking commands checkin_handler = ConversationHandler( entry_points=[ CommandHandler("start", start_command), diff --git a/command.py b/command.py index 6feeec7..b12483a 100644 --- a/command.py +++ b/command.py @@ -2,7 +2,7 @@ from telegram import Update from telegram.constants import ParseMode from telegram.error import BadRequest from telegram.ext import ContextTypes, ConversationHandler -from dbstore.peewee_store import get_user_access_key, get_user_home_instance +from dbstore.peewee_store import get_user_access_key, get_user_home_instance, delete_user_by_id from config import * @@ -46,7 +46,8 @@ async def list_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No async def logout_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await update.message.reply_text(PROMPT_LOGOUT, parse_mode=ParseMode.HTML, reply_markup=MAIN_MENU) + if delete_user_by_id(str(update.effective_user.id)): + await update.message.reply_text(PROMPT_LOGOUT_SUCCESS, parse_mode=ParseMode.HTML, reply_markup=LOGIN_MENU) async def toggle_visibility_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: diff --git a/config.py b/config.py index 83ee37a..9b52205 100644 --- a/config.py +++ b/config.py @@ -19,6 +19,11 @@ MAIN_MENU = ReplyKeyboardMarkup([ [KeyboardButton(text="/Help")] ]) +LOGIN_MENU = ReplyKeyboardMarkup([ + [KeyboardButton(text="/login")], + [KeyboardButton(text="/Help")] +]) + SKIP_LOCATION_SEARCH = CALLBACK_SKIP INLINE_SKIP_MENU = InlineKeyboardMarkup([ [InlineKeyboardButton("Skip", callback_data=SKIP_LOCATION_SEARCH)] diff --git a/dbstore/peewee_store.py b/dbstore/peewee_store.py index 2f637bf..f10627b 100644 --- a/dbstore/peewee_store.py +++ b/dbstore/peewee_store.py @@ -51,6 +51,11 @@ def get_user_access_key(telegram_user_id: str) -> str: return "" +def delete_user_by_id(telegram_user_id: str) -> int: + with db.connection_context(): + return User.delete().where(User.telegram_user_id == telegram_user_id).execute() + + def get_user_home_instance(telegram_user_id: str) -> dict: with db.connection_context(): try: diff --git a/prompt/string.py b/prompt/string.py index 207713f..b7aeff1 100644 --- a/prompt/string.py +++ b/prompt/string.py @@ -42,6 +42,7 @@ PROMPT_HELP = "Available commands:" \ PROMPT_LIST = "You are linked with the following Fediverse accounts:" PROMPT_LIST_NO_RESULT = "You are not linked with any Fediverse account yet. \n\n Input /login to login." PROMPT_LOGOUT = "Choose Fediverse account to logout" +PROMPT_LOGOUT_SUCCESS = "Successfully logged out from Fediverse account" PROMPT_TOGGLE_VIS = "Choose visibility of your checkins on Fediverse" CALLBACK_SKIP = "Skip" -- cgit v1.2.3