Skip to content

Commit

Permalink
fix: use callback query patterns in handlers
Browse files Browse the repository at this point in the history
this makes sure that the correct handlers gets choosed, even if
different conversations are active at the same time
  • Loading branch information
jackstar12 committed Feb 10, 2025
1 parent 9827163 commit 4ff6dae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
10 changes: 7 additions & 3 deletions commands/mysubscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ async def select(update: Update, context: ContextTypes.DEFAULT_TYPE):
async def selected_subscription(
session: AsyncSession, update: Update, context: ContextTypes.DEFAULT_TYPE
):
subscription = await get_subscription(session, int(context.chat_data["selection"]))
try:
selection = int(context.chat_data.get("selection"))
subscription = await get_subscription(session, selection)
except ValueError | TypeError:
subscription = None
if not subscription:
await update.effective_chat.send_message(
"Could not get subscription. Try /mysubscriptions again."
Expand Down Expand Up @@ -122,8 +126,8 @@ async def update_threshold(update: Update, context: ContextTypes.DEFAULT_TYPE):
mysubscriptions_handler = ConversationHandler(
entry_points=[entry_point],
states={
SELECT: [CallbackQueryHandler(select)],
ACTION: [CallbackQueryHandler(action)],
SELECT: [CallbackQueryHandler(select, pattern=r"^\d+$")],
ACTION: [CallbackQueryHandler(action, pattern=r"^(edit|remove)$")],
UPDATE_THRESHOLD: [MessageHandler(filters.TEXT, update_threshold)],
},
fallbacks=[entry_point],
Expand Down
28 changes: 21 additions & 7 deletions commands/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@

FROM_ASSET, TO_ASSET, THRESHOLD, CUSTOM_THRESHOLD = range(4)

ASSET_PREFIX = "asset_"
ASSET_PATTERN = rf"^{ASSET_PREFIX}.+$"


def remove_asset_prefix(asset: str) -> str:
return asset.replace(ASSET_PREFIX, "")


def inline_keyboard(assets: Iterable[str]):
rows = [[InlineKeyboardButton(asset, callback_data=asset) for asset in assets]]
rows = [
[
InlineKeyboardButton(asset, callback_data=ASSET_PREFIX + asset)
for asset in assets
]
]
return InlineKeyboardMarkup(rows)


Expand Down Expand Up @@ -58,12 +70,13 @@ async def subscribe(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
async def from_asset(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
query = update.callback_query
await query.answer()
available = context.chat_data["available_pairs"][query.data]
asset = remove_asset_prefix(query.data)
available = context.chat_data["available_pairs"][asset]
await query.edit_message_text(
"Select the receive asset for your notifications.",
reply_markup=inline_keyboard(available.keys()),
)
context.chat_data["from_asset"] = query.data
context.chat_data["from_asset"] = asset
return TO_ASSET


Expand All @@ -82,7 +95,7 @@ async def to_asset(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
]
),
)
context.chat_data["to_asset"] = query.data
context.chat_data["to_asset"] = remove_asset_prefix(query.data)
return THRESHOLD


Expand Down Expand Up @@ -133,12 +146,13 @@ async def custom_threshold(update: Update, context: ContextTypes.DEFAULT_TYPE) -

entry_point = CommandHandler("subscribe", subscribe)


subscribe_handler = ConversationHandler(
entry_points=[entry_point],
states={
FROM_ASSET: [CallbackQueryHandler(from_asset)],
TO_ASSET: [CallbackQueryHandler(to_asset)],
THRESHOLD: [CallbackQueryHandler(threshold)],
FROM_ASSET: [CallbackQueryHandler(from_asset, pattern=ASSET_PATTERN)],
TO_ASSET: [CallbackQueryHandler(to_asset, pattern=ASSET_PATTERN)],
THRESHOLD: [CallbackQueryHandler(threshold, pattern=r"^(custom|-?\d*\.?\d+)$")],
CUSTOM_THRESHOLD: [MessageHandler(filters.TEXT, custom_threshold)],
},
fallbacks=[entry_point],
Expand Down

0 comments on commit 4ff6dae

Please sign in to comment.