From 6602a954788e26be63c00369f6bf72822752c84b Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Tue, 21 Feb 2023 12:42:09 -0800 Subject: add setting handler --- bot.py | 108 ++++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 67 insertions(+), 41 deletions(-) diff --git a/bot.py b/bot.py index a0a00b3..5c90144 100644 --- a/bot.py +++ b/bot.py @@ -18,20 +18,20 @@ if __version_info__ < (20, 0, 0, "alpha", 1): f"{TG_VER} version of this example, " f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html" ) -from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, ReplyKeyboardRemove +from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import Application, CallbackQueryHandler, \ - CommandHandler, ContextTypes, MessageHandler, filters, ConversationHandler, CallbackContext, JobQueue + CommandHandler, ContextTypes, MessageHandler, filters, ConversationHandler, CallbackContext from config import BOT_TOKEN from foursquare.poi import query_poi from dbstore.dbm_store import get_loc from toot import mastodon_client from typing import TypedDict, List, cast -from telegram import Update, InputMediaVideo, InputMediaPhoto +from telegram import Update, ReplyKeyboardMarkup, KeyboardButton scheduler = None PRIVACY, TOOT = map(chr, range(8, 10)) -WAIT_LOC, LOCATION, PHOTO, PROCESS_PHOTO, FINAL = range(5) +WAIT_LOC, LOCATION, PHOTO, PROCESS_PHOTO, FINAL, SETTING = range(6) # Enable logging logging.basicConfig( @@ -39,6 +39,20 @@ 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")] +]) + +SKIP_MENU = ReplyKeyboardMarkup([[telegram.KeyboardButton(text="/skip")]]) +SETTING_MENU = ReplyKeyboardMarkup( + [ + [KeyboardButton(text="/tos")], + [telegram.KeyboardButton(text="/back")], + ] +) + async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: hello = "Hello, this is `checkin.bot`. \n\n" \ @@ -49,10 +63,7 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: "Start using this bot by sharing your location using Telegram context menu to it." await update.message.reply_text(hello, parse_mode=telegram.constants.ParseMode.MARKDOWN) - await update.message.reply_text("Please choose", - reply_markup=telegram.ReplyKeyboardMarkup([ - [telegram.KeyboardButton(text="Check in", request_location=True)], - [telegram.KeyboardButton(text="Setting")]])) + await update.message.reply_text("Use bot keyboard to choose an action", reply_markup=MAIN_MENU) return LOCATION @@ -66,7 +77,7 @@ async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: ]) reply_markup = InlineKeyboardMarkup(keyboard) - await update.message.reply_text("Select a place", reply_markup=reply_markup) + await update.message.reply_text("Where are you?", reply_markup=reply_markup) return WAIT_LOC @@ -94,32 +105,25 @@ 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=telegram.ReplyKeyboardMarkup([ - [telegram.KeyboardButton(text="Check in", request_location=True)], - [telegram.KeyboardButton(text="Setting")]]) + reply_markup=MAIN_MENU ) - await query.message.reply_text("You can continue attaching photos, or press skip to continue", - reply_markup=telegram.ReplyKeyboardMarkup([ - [telegram.KeyboardButton(text="/skip")]])) + await query.message.reply_text("You can continue attaching photos, or press skip to finish", reply_markup=SKIP_MENU) return PHOTO -async def action(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - if update.message.text == "Check in": - await update.message.reply_text("Please share your location", - reply_markup=telegram.ReplyKeyboardRemove()) - elif update.message.text == "Setting": - await update.message.reply_text("Setting") +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: - keyboard = [[ - InlineKeyboardButton("Privacy", callback_data=PRIVACY), - ]] + await update.message.reply_text("Setting", reply_markup=SETTING_MENU) + return SETTING - reply_markup = InlineKeyboardMarkup(keyboard) - await update.message.reply_text("Setting", reply_markup=reply_markup) + +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): @@ -154,12 +158,27 @@ 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 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( - "Bye! I hope we can talk again some day.", reply_markup=ReplyKeyboardRemove() + text="Canceled.", + # "Bye! I hope we can talk again some day.", + reply_markup=MAIN_MENU ) return ConversationHandler.END @@ -197,9 +216,8 @@ async def media_group_sender(context: CallbackContext): media_ids=media_id) await context.bot.send_message(chat_id=chat_id, text="Done", - reply_markup=telegram.ReplyKeyboardMarkup([ - [telegram.KeyboardButton(text="Check in", request_location=True)], - [telegram.KeyboardButton(text="Setting")]])) + reply_markup=MAIN_MENU + ) async def photo(update: Update, context: CallbackContext): @@ -225,9 +243,6 @@ async def photo(update: Update, context: CallbackContext): if jobs: jobs[0].data.append(msg_dict) else: - # TODO - # media_group_sender won't end the callback context - # should add a job event listener context.job_queue.run_once(callback=media_group_sender, when=5, data=[msg_dict], name=str(message.media_group_id)) else: @@ -243,17 +258,14 @@ async def photo(update: Update, context: CallbackContext): media_ids=media["id"]) await update.message.reply_text(text="Done", - reply_markup=telegram.ReplyKeyboardMarkup([ - [telegram.KeyboardButton(text="Check in", request_location=True)], - [telegram.KeyboardButton(text="Setting")]])) + reply_markup=MAIN_MENU + ) async def skip_photo(update: Update, context: ContextTypes.DEFAULT_TYPE): print(context.user_data) await update.message.reply_text( - text="Done.", reply_markup=telegram.ReplyKeyboardMarkup([ - [telegram.KeyboardButton(text="Check in", request_location=True)], - [telegram.KeyboardButton(text="Setting")]]) + text="Done.", reply_markup=MAIN_MENU ) return ConversationHandler.END @@ -261,7 +273,7 @@ async def skip_photo(update: Update, context: ContextTypes.DEFAULT_TYPE): def main() -> None: application = Application.builder().token(BOT_TOKEN).build() - conv_handler = ConversationHandler( + checkin_handler = ConversationHandler( entry_points=[ CommandHandler("start", start), MessageHandler(filters.LOCATION, checkin), @@ -279,7 +291,21 @@ def main() -> None: allow_reentry=True, ) - application.add_handler(conv_handler) + 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 application.run_polling() -- cgit v1.2.3