From 6ef07f766d2127013b6a75e3f616fd1dfb8016d9 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Tue, 21 Feb 2023 21:13:29 -0800 Subject: refine conversation flow with some inline keyboards --- bot.py | 134 ++++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 51 deletions(-) diff --git a/bot.py b/bot.py index efde56f..d49aaa5 100644 --- a/bot.py +++ b/bot.py @@ -39,18 +39,23 @@ logging.basicConfig( logger = logging.getLogger(__name__) MAIN_MENU = ReplyKeyboardMarkup([ - [telegram.KeyboardButton(text="/check in", request_location=True)], - [telegram.KeyboardButton(text="/cancel")], - [telegram.KeyboardButton(text="/setting")] + [KeyboardButton(text="Check-in here", request_location=True)], + # [KeyboardButton(text="/cancel")], + # [KeyboardButton(text="/setting")] ]) -SKIP_MENU = ReplyKeyboardMarkup([[telegram.KeyboardButton(text="/skip")]]) -SETTING_MENU = ReplyKeyboardMarkup( - [ - [KeyboardButton(text="/tos")], - [telegram.KeyboardButton(text="/back")], - ] -) +SKIP_LOCATION_SEARCH = "skip_location_search" + +SKIP_MENU = InlineKeyboardMarkup([ + [telegram.InlineKeyboardButton("Skip", callback_data=SKIP_LOCATION_SEARCH)] +]) + +# SETTING_MENU = InlineKeyboardMarkup( +# [ +# [InlineKeyboardButton(text="/tos")], +# [InlineKeyboardButton(text="/back")], +# ] +# ) async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: @@ -70,12 +75,16 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: context.user_data["latitude"] = update.message.location.latitude context.user_data["longitude"] = update.message.location.longitude - await update.message.reply_text("You can input location search keywords or press skip", reply_markup=SKIP_MENU) + + await update.message.reply_text("Searching...", reply_markup=telegram.ReplyKeyboardRemove()) + await update.message.reply_text("You can input location search keywords or press skip", + reply_markup=SKIP_MENU) return LOCATION_SEARCH async def process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: + print("process_callback") query = update.callback_query await query.answer() print(query.data) @@ -98,7 +107,7 @@ async def process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) - await query.message.reply_text( text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}", parse_mode=telegram.constants.ParseMode.MARKDOWN, - reply_markup=MAIN_MENU + # reply_markup=MAIN_MENU ) await query.message.reply_text("You can continue attaching photos, or press skip to finish", reply_markup=SKIP_MENU) @@ -124,22 +133,40 @@ async def location_search_callback(update: Update, context: ContextTypes.DEFAULT return WAIT_LOC -async def skip_location_search(update: Update, context: ContextTypes.DEFAULT_TYPE): +async def skip_location_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: + query = update.callback_query + await query.answer() + print("skip: ", query.data) + + await query.message.delete() + latitude = context.user_data["latitude"] + longitude = context.user_data["longitude"] + + keyboard = [] + + for poi in query_poi("", latitude, longitude): + keyboard.append([ + InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]), + ]) + + reply_markup = InlineKeyboardMarkup(keyboard) + await query.message.reply_text("Where are you? ", reply_markup=reply_markup) + return WAIT_LOC -async def tos(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await update.message.reply_text("TOS", reply_markup=MAIN_MENU) +# async def tos(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: +# await update.message.reply_text("TOS", reply_markup=MAIN_MENU) -async def setting(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await update.message.reply_text("Setting", reply_markup=SETTING_MENU) - return SETTING +# async def setting(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: +# await update.message.reply_text("Setting", reply_markup=SETTING_MENU) +# return SETTING -async def setting_process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE): - await update.message.reply_text("Setting Process Callback", reply_markup=SETTING_MENU) - return ConversationHandler.END +# async def setting_process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE): +# await update.message.reply_text("Setting Process Callback", reply_markup=SETTING_MENU) +# return ConversationHandler.END async def process_location(update: Update, context: ContextTypes.DEFAULT_TYPE): @@ -165,7 +192,7 @@ async def process_location(update: Update, context: ContextTypes.DEFAULT_TYPE): visibility="private", media_ids=media_id) - await update.message.delete_message() + await update.message.delete() return ConversationHandler.END @@ -174,17 +201,17 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No await update.message.reply_text("Use /start to test this bot.") -async def setting_cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: - """Cancels and ends the conversation.""" - user = update.message.from_user - logger.info("User %s canceled the conversation.", user.first_name) - await update.message.reply_text( - text="Setting canceled.", - # "Bye! I hope we can talk again some day.", - reply_markup=MAIN_MENU - ) - - return ConversationHandler.END +# async def setting_cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: +# """Cancels and ends the conversation.""" +# user = update.message.from_user +# logger.info("User %s canceled the conversation.", user.first_name) +# await update.message.reply_text( +# text="Setting canceled.", +# # "Bye! I hope we can talk again some day.", +# reply_markup=MAIN_MENU +# ) +# +# return ConversationHandler.END async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: @@ -278,9 +305,14 @@ async def photo(update: Update, context: CallbackContext): ) -async def skip_photo(update: Update, context: ContextTypes.DEFAULT_TYPE): +async def skip_photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: + query = update.callback_query + await query.answer() + print("skip photo: ", query.data) + print(context.user_data) - await update.message.reply_text( + await query.delete_message() + await query.message.reply_text( text="Done.", reply_markup=MAIN_MENU ) return ConversationHandler.END @@ -301,30 +333,30 @@ def main() -> None: WAIT_LOC: [CallbackQueryHandler(process_callback)], LOCATION_SEARCH: [ MessageHandler(filters.TEXT, location_search_callback), - CommandHandler("skip", skip_location_search), + CallbackQueryHandler(skip_location_search), ], PHOTO: [MessageHandler(filters.PHOTO, photo), - CommandHandler("skip", skip_photo)], + CallbackQueryHandler(skip_photo)], }, fallbacks=[CommandHandler("cancel", cancel)], per_message=False, allow_reentry=True, ) - setting_conv_handler = ConversationHandler( - entry_points=[CommandHandler("setting", setting)], - states={ - SETTING: [ - CallbackQueryHandler(setting_process_callback), - ], - }, - fallbacks=[CommandHandler("back", setting_cancel)], - per_message=False, - allow_reentry=True, - ) - - application.add_handler(CommandHandler("tos", tos)) - application.add_handler(setting_conv_handler, 2) + # setting_conv_handler = ConversationHandler( + # entry_points=[CommandHandler("setting", setting)], + # states={ + # SETTING: [ + # CallbackQueryHandler(setting_process_callback), + # ], + # }, + # fallbacks=[CommandHandler("back", setting_cancel)], + # per_message=False, + # allow_reentry=True, + # ) + + # application.add_handler(CommandHandler("tos", tos)) + # application.add_handler(setting_conv_handler, 2) application.add_handler(checkin_handler, 1) # Run the bot until the user presses Ctrl-C -- cgit v1.2.3