aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorclarkzjw <[email protected]>2023-02-21 21:13:29 -0800
committerclarkzjw <[email protected]>2023-02-21 21:13:29 -0800
commit6ef07f766d2127013b6a75e3f616fd1dfb8016d9 (patch)
tree6f19c65342aaff49c18161b3d07e5183ec972d33
parent5b09e9d190b642a4e5571c0e4504c155f56a6f5a (diff)
downloadswarm2fediverse-6ef07f766d2127013b6a75e3f616fd1dfb8016d9.tar.gz
refine conversation flow with some inline keyboards
-rw-r--r--bot.py134
1 files 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(
39logger = logging.getLogger(__name__) 39logger = logging.getLogger(__name__)
40 40
41MAIN_MENU = ReplyKeyboardMarkup([ 41MAIN_MENU = ReplyKeyboardMarkup([
42 [telegram.KeyboardButton(text="/check in", request_location=True)], 42 [KeyboardButton(text="Check-in here", request_location=True)],
43 [telegram.KeyboardButton(text="/cancel")], 43 # [KeyboardButton(text="/cancel")],
44 [telegram.KeyboardButton(text="/setting")] 44 # [KeyboardButton(text="/setting")]
45]) 45])
46 46
47SKIP_MENU = ReplyKeyboardMarkup([[telegram.KeyboardButton(text="/skip")]]) 47SKIP_LOCATION_SEARCH = "skip_location_search"
48SETTING_MENU = ReplyKeyboardMarkup( 48
49 [ 49SKIP_MENU = InlineKeyboardMarkup([
50 [KeyboardButton(text="/tos")], 50 [telegram.InlineKeyboardButton("Skip", callback_data=SKIP_LOCATION_SEARCH)]
51 [telegram.KeyboardButton(text="/back")], 51])
52 ] 52
53) 53# SETTING_MENU = InlineKeyboardMarkup(
54# [
55# [InlineKeyboardButton(text="/tos")],
56# [InlineKeyboardButton(text="/back")],
57# ]
58# )
54 59
55 60
56async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: 61async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
@@ -70,12 +75,16 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
70async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: 75async def checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
71 context.user_data["latitude"] = update.message.location.latitude 76 context.user_data["latitude"] = update.message.location.latitude
72 context.user_data["longitude"] = update.message.location.longitude 77 context.user_data["longitude"] = update.message.location.longitude
73 await update.message.reply_text("You can input location search keywords or press skip", reply_markup=SKIP_MENU) 78
79 await update.message.reply_text("Searching...", reply_markup=telegram.ReplyKeyboardRemove())
80 await update.message.reply_text("You can input location search keywords or press skip",
81 reply_markup=SKIP_MENU)
74 82
75 return LOCATION_SEARCH 83 return LOCATION_SEARCH
76 84
77 85
78async def process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: 86async def process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
87 print("process_callback")
79 query = update.callback_query 88 query = update.callback_query
80 await query.answer() 89 await query.answer()
81 print(query.data) 90 print(query.data)
@@ -98,7 +107,7 @@ async def process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -
98 await query.message.reply_text( 107 await query.message.reply_text(
99 text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}", 108 text=f"Selected place: {poi['name']}, `{query.data}`\nPosted to Mastodon: {status['url']}",
100 parse_mode=telegram.constants.ParseMode.MARKDOWN, 109 parse_mode=telegram.constants.ParseMode.MARKDOWN,
101 reply_markup=MAIN_MENU 110 # reply_markup=MAIN_MENU
102 ) 111 )
103 112
104 await query.message.reply_text("You can continue attaching photos, or press skip to finish", reply_markup=SKIP_MENU) 113 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
124 return WAIT_LOC 133 return WAIT_LOC
125 134
126 135
127async def skip_location_search(update: Update, context: ContextTypes.DEFAULT_TYPE): 136async def skip_location_search(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
137 query = update.callback_query
138 await query.answer()
139 print("skip: ", query.data)
140
141 await query.message.delete()
142 latitude = context.user_data["latitude"]
143 longitude = context.user_data["longitude"]
144
145 keyboard = []
146
147 for poi in query_poi("", latitude, longitude):
148 keyboard.append([
149 InlineKeyboardButton(poi["name"], callback_data=poi["fsq_id"]),
150 ])
151
152 reply_markup = InlineKeyboardMarkup(keyboard)
153 await query.message.reply_text("Where are you? ", reply_markup=reply_markup)
154
128 return WAIT_LOC 155 return WAIT_LOC
129 156
130 157
131async def tos(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 158# async def tos(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
132 await update.message.reply_text("TOS", reply_markup=MAIN_MENU) 159# await update.message.reply_text("TOS", reply_markup=MAIN_MENU)
133 160
134 161
135async def setting(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 162# async def setting(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
136 await update.message.reply_text("Setting", reply_markup=SETTING_MENU) 163# await update.message.reply_text("Setting", reply_markup=SETTING_MENU)
137 return SETTING 164# return SETTING
138 165
139 166
140async def setting_process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE): 167# async def setting_process_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
141 await update.message.reply_text("Setting Process Callback", reply_markup=SETTING_MENU) 168# await update.message.reply_text("Setting Process Callback", reply_markup=SETTING_MENU)
142 return ConversationHandler.END 169# return ConversationHandler.END
143 170
144 171
145async def process_location(update: Update, context: ContextTypes.DEFAULT_TYPE): 172async def process_location(update: Update, context: ContextTypes.DEFAULT_TYPE):
@@ -165,7 +192,7 @@ async def process_location(update: Update, context: ContextTypes.DEFAULT_TYPE):
165 visibility="private", 192 visibility="private",
166 media_ids=media_id) 193 media_ids=media_id)
167 194
168 await update.message.delete_message() 195 await update.message.delete()
169 return ConversationHandler.END 196 return ConversationHandler.END
170 197
171 198
@@ -174,17 +201,17 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
174 await update.message.reply_text("Use /start to test this bot.") 201 await update.message.reply_text("Use /start to test this bot.")
175 202
176 203
177async def setting_cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: 204# async def setting_cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
178 """Cancels and ends the conversation.""" 205# """Cancels and ends the conversation."""
179 user = update.message.from_user 206# user = update.message.from_user
180 logger.info("User %s canceled the conversation.", user.first_name) 207# logger.info("User %s canceled the conversation.", user.first_name)
181 await update.message.reply_text( 208# await update.message.reply_text(
182 text="Setting canceled.", 209# text="Setting canceled.",
183 # "Bye! I hope we can talk again some day.", 210# # "Bye! I hope we can talk again some day.",
184 reply_markup=MAIN_MENU 211# reply_markup=MAIN_MENU
185 ) 212# )
186 213#
187 return ConversationHandler.END 214# return ConversationHandler.END
188 215
189 216
190async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: 217async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
@@ -278,9 +305,14 @@ async def photo(update: Update, context: CallbackContext):
278 ) 305 )
279 306
280 307
281async def skip_photo(update: Update, context: ContextTypes.DEFAULT_TYPE): 308async def skip_photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
309 query = update.callback_query
310 await query.answer()
311 print("skip photo: ", query.data)
312
282 print(context.user_data) 313 print(context.user_data)
283 await update.message.reply_text( 314 await query.delete_message()
315 await query.message.reply_text(
284 text="Done.", reply_markup=MAIN_MENU 316 text="Done.", reply_markup=MAIN_MENU
285 ) 317 )
286 return ConversationHandler.END 318 return ConversationHandler.END
@@ -301,30 +333,30 @@ def main() -> None:
301 WAIT_LOC: [CallbackQueryHandler(process_callback)], 333 WAIT_LOC: [CallbackQueryHandler(process_callback)],
302 LOCATION_SEARCH: [ 334 LOCATION_SEARCH: [
303 MessageHandler(filters.TEXT, location_search_callback), 335 MessageHandler(filters.TEXT, location_search_callback),
304 CommandHandler("skip", skip_location_search), 336 CallbackQueryHandler(skip_location_search),
305 ], 337 ],
306 PHOTO: [MessageHandler(filters.PHOTO, photo), 338 PHOTO: [MessageHandler(filters.PHOTO, photo),
307 CommandHandler("skip", skip_photo)], 339 CallbackQueryHandler(skip_photo)],
308 }, 340 },
309 fallbacks=[CommandHandler("cancel", cancel)], 341 fallbacks=[CommandHandler("cancel", cancel)],
310 per_message=False, 342 per_message=False,
311 allow_reentry=True, 343 allow_reentry=True,
312 ) 344 )
313 345
314 setting_conv_handler = ConversationHandler( 346 # setting_conv_handler = ConversationHandler(
315 entry_points=[CommandHandler("setting", setting)], 347 # entry_points=[CommandHandler("setting", setting)],
316 states={ 348 # states={
317 SETTING: [ 349 # SETTING: [
318 CallbackQueryHandler(setting_process_callback), 350 # CallbackQueryHandler(setting_process_callback),
319 ], 351 # ],
320 }, 352 # },
321 fallbacks=[CommandHandler("back", setting_cancel)], 353 # fallbacks=[CommandHandler("back", setting_cancel)],
322 per_message=False, 354 # per_message=False,
323 allow_reentry=True, 355 # allow_reentry=True,
324 ) 356 # )
325 357
326 application.add_handler(CommandHandler("tos", tos)) 358 # application.add_handler(CommandHandler("tos", tos))
327 application.add_handler(setting_conv_handler, 2) 359 # application.add_handler(setting_conv_handler, 2)
328 application.add_handler(checkin_handler, 1) 360 application.add_handler(checkin_handler, 1)
329 361
330 # Run the bot until the user presses Ctrl-C 362 # Run the bot until the user presses Ctrl-C
Powered by cgit v1.2.3 (git 2.41.0)