Conversation timeout changed to 120 seconds. Changed list command to send buttons and show details on click

This commit is contained in:
Joan Cano
2023-03-19 23:45:57 +01:00
parent 8dad58f882
commit 7a94f7fa9f
3 changed files with 61 additions and 23 deletions

View File

@@ -83,7 +83,7 @@ async def menu_click_handler(update: Update, context: CallbackContext):
return REMOVE_PRODUCT
if query.data == 'list':
await send_list(update, context)
return ConversationHandler.END
return LIST
else:
await context.bot.send_message(chat_id=update.effective_chat.id,
text=helpers.telegram_escape_characters('Activa tu periodo de prueba de 7 días con `/test` o contacta con @jocarduck para más información.'), parse_mode=ParseMode.MARKDOWN_V2)
@@ -317,31 +317,31 @@ async def remove_product(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
await update.message.reply_text(message)
return ConversationHandler.END
async def product_details(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.callback_query.edit_message_reply_markup(None)
query = update.callback_query
product_name = query.data
telegram_user_id = helpers.get_telegram_user_id(update)
product = walladb.get_product({'product_name':product_name,'telegram_user_id':telegram_user_id})
if product:
categories = helpers.generate_categories_string(product['category'], product['subcategory'])
text = f"*Nombre del producto:* {helpers.telegram_escape_characters(product['product_name'])}\n\
*Precio desde *{product['min_price']}€ *hasta *{product['max_price']}\n\
*En las coordenadas *{helpers.telegram_escape_characters(product['latitude'])}, {helpers.telegram_escape_characters(product['longitude'])} *y a *{product['distance']}km *de estas*\n\
*En las categorías: *{helpers.telegram_escape_characters(categories)}\n\
*Palabras excluídas del título: *`{helpers.telegram_escape_characters(product['title_exclude'])}`\n\
*Palabras excluídas del título y la descripción: *`{helpers.telegram_escape_characters(product['title_description_exclude'])}`"
await context.bot.send_message(chat_id=update.effective_chat.id, text=text, parse_mode=ParseMode.MARKDOWN_V2)
return ConversationHandler.END
async def send_list(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
telegram_user_id = helpers.get_telegram_user_id(update)
if walladb.is_user_valid(telegram_user_id):
if walladb.is_user_testing(telegram_user_id) or walladb.is_user_premium(telegram_user_id):
products = walladb.get_products_from_user(telegram_user_id)
keyboard = helpers.create_products_keyboard(telegram_user_id)
markup = InlineKeyboardMarkup(keyboard)
args = ''
found = False
if len(args) > 1:
product = walladb.get_product({'product_name':args[1],'telegram_user_id':telegram_user_id})
if product:
table = prettytable.PrettyTable(['Campo', 'Valor'])
table.align['Campo'] = 'l'
table.align['Valor'] = 'r'
for key in product:
table.add_row([key, product[key]])
found = True
if not found:
table = prettytable.PrettyTable(['Producto', 'Mín', 'Máx'])
table.align['Producto'] = 'l'
table.align['Mín'] = 'r'
table.align['Máx'] = 'r'
for product in products:
table.add_row([helpers.telegram_escape_characters(product['product_name']), f"{helpers.telegram_escape_characters(product['min_price'])}", f"{helpers.telegram_escape_characters(product['max_price'])}"])
await context.bot.send_message(chat_id=update.effective_chat.id, text=f'```{(table)}```', parse_mode=ParseMode.MARKDOWN_V2)
await context.bot.send_message(chat_id=update.effective_chat.id, text="Escoge para ver detalles", reply_markup=markup)
async def admin_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if helpers.is_user_admin(update.message.chat_id):
@@ -432,7 +432,7 @@ def main()->None:
#application.add_handler(CallbackQueryHandler(pattern="list", callback=send_list()))
conv_handler = ConversationHandler(
conversation_timeout=60,
conversation_timeout=120,
entry_points=[CommandHandler("menu", main_menu)],
states={
ACTION: [CallbackQueryHandler(menu_click_handler)],
@@ -444,6 +444,7 @@ def main()->None:
CONTINUE_OR_FINISH: [CallbackQueryHandler(continue_or_finish),
MessageHandler(filters.TEXT, continue_or_finish)],
REMOVE_PRODUCT: [MessageHandler(filters.TEXT, remove_product)],
LIST: [CallbackQueryHandler(product_details)],
ConversationHandler.TIMEOUT: [CallbackQueryHandler(conv_timeout),
MessageHandler(filters.TEXT, conv_timeout)],
ConversationHandler.END: [MessageHandler(filters.TEXT, conv_finish)],