Skip to content

Commit

Permalink
Add back and cancel buttons to currency and gateway keyboards
Browse files Browse the repository at this point in the history
Signed-off-by: alfred richardsn <rchrdsn@protonmail.ch>
  • Loading branch information
r4rdsn committed Mar 13, 2020
1 parent 0b8dad1 commit 5b447ef
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 23 deletions.
71 changes: 56 additions & 15 deletions src/handlers/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,29 @@ async def change_state(call: types.CallbackQuery, state: FSMContext):
return await call.answer(answer)


@dp.callback_query_handler(lambda call: call.data == "cancel", state=any_state)
async def cancel_order_creation(call: types.CallbackQuery, state: FSMContext):
async def cancel_order_creation(user_id: int, chat_id: int):
"""Cancel order creation."""
await state.finish()
await call.answer()
await dp.get_current().current_state().finish()

order = await database.creation.delete_one({"user_id": call.from_user.id})
order = await database.creation.delete_one({"user_id": user_id})
if not order.deleted_count:
await tg.send_message(
call.message.chat.id, i18n("no_creation"), reply_markup=start_keyboard(),
chat_id, i18n("no_creation"), reply_markup=start_keyboard(),
)
return True

await tg.send_message(
call.message.chat.id, i18n("order_cancelled"), reply_markup=start_keyboard()
chat_id, i18n("order_cancelled"), reply_markup=start_keyboard()
)


@dp.callback_query_handler(lambda call: call.data == "cancel", state=any_state)
async def cancel_button(call: types.CallbackQuery, state: FSMContext):
"""React to cancel button."""
await call.answer()
await cancel_order_creation(call.from_user.id, call.message.chat.id)


async def set_gateway(currency_type: str, message: types.Message):
"""Try to append gateway from message text to currency."""
gateway = message.text.upper()
Expand Down Expand Up @@ -192,9 +197,7 @@ async def match_currency(currency_type: str, message: types.Message):
await tg.send_message(
message.chat.id,
i18n("choose_gateway {currency}").format(currency=currency),
reply_markup=whitelist.gateway_keyboard(
currency, one_time_keyboard=currency_type == "sell"
),
reply_markup=whitelist.gateway_keyboard(currency, currency_type),
)
await OrderCreation.next()
return None
Expand Down Expand Up @@ -264,6 +267,10 @@ async def whitelisting_request(call: types.CallbackQuery):
@private_handler(state=OrderCreation.buy)
async def choose_buy(message: types.Message, state: FSMContext):
"""Set currency user wants to buy and ask for one they want to sell."""
if message.text.startswith(emojize(":x:")):
await cancel_order_creation(message.from_user.id, message.chat.id)
return

match = await match_currency("buy", message)
if not match:
return
Expand All @@ -275,14 +282,25 @@ async def choose_buy(message: types.Message, state: FSMContext):
await tg.send_message(
message.chat.id,
i18n("ask_sell_currency"),
reply_markup=whitelist.currency_keyboard(one_time_keyboard=True),
reply_markup=whitelist.currency_keyboard("sell"),
)


@private_handler(state=OrderCreation.buy_gateway)
async def choose_buy_gateway(message: types.Message, state: FSMContext):
"""Set gateway of buy currency and ask for sell currency."""
if not message.text.startswith(emojize(":fast_forward:")):
if message.text.startswith(emojize(":fast_reverse_button:")):
await OrderCreation.previous()
await tg.send_message(
message.chat.id,
i18n("ask_buy_currency"),
reply_markup=whitelist.currency_keyboard("buy"),
)
return
elif message.text.startswith(emojize(":x:")):
await cancel_order_creation(message.from_user.id, message.chat.id)
return
elif not message.text.startswith(emojize(":fast_forward:")):
order = await set_gateway("buy", message)
if not order:
return
Expand All @@ -291,7 +309,7 @@ async def choose_buy_gateway(message: types.Message, state: FSMContext):
await tg.send_message(
message.chat.id,
i18n("ask_sell_currency"),
reply_markup=whitelist.currency_keyboard(one_time_keyboard=True),
reply_markup=whitelist.currency_keyboard("sell"),
)


Expand All @@ -312,6 +330,18 @@ async def set_price_state(message: types.Message, order: Mapping[str, Any]):
@private_handler(state=OrderCreation.sell)
async def choose_sell(message: types.Message, state: FSMContext):
"""Set currency user wants to sell and ask for price."""
if message.text.startswith(emojize(":fast_reverse_button:")):
await OrderCreation.buy.set()
await tg.send_message(
message.chat.id,
i18n("ask_buy_currency"),
reply_markup=whitelist.currency_keyboard("buy"),
)
return
elif message.text.startswith(emojize(":x:")):
await cancel_order_creation(message.from_user.id, message.chat.id)
return

match = await match_currency("sell", message)
if not match:
return
Expand All @@ -325,7 +355,7 @@ async def choose_sell(message: types.Message, state: FSMContext):
await tg.send_message(
message.chat.id,
i18n("same_currency_error"),
reply_markup=whitelist.currency_keyboard(one_time_keyboard=True),
reply_markup=whitelist.currency_keyboard("sell"),
)
return

Expand All @@ -335,7 +365,18 @@ async def choose_sell(message: types.Message, state: FSMContext):
@private_handler(state=OrderCreation.sell_gateway)
async def choose_sell_gateway(message: types.Message, state: FSMContext):
"""Set gateway of sell currency and ask for price."""
if not message.text.startswith(emojize(":fast_forward:")):
if message.text.startswith(emojize(":fast_reverse_button:")):
await OrderCreation.previous()
await tg.send_message(
message.chat.id,
i18n("ask_sell_currency"),
reply_markup=whitelist.currency_keyboard("sell"),
)
return
elif message.text.startswith(emojize(":x:")):
await cancel_order_creation(message.from_user.id, message.chat.id)
return
elif not message.text.startswith(emojize(":fast_forward:")):
order = await set_gateway("sell", message)
if not order:
return
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/start_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async def handle_create(message: types.Message, state: FSMContext):
await tg.send_message(
message.chat.id,
i18n("ask_buy_currency"),
reply_markup=whitelist.currency_keyboard(),
reply_markup=whitelist.currency_keyboard("buy"),
)


Expand Down
28 changes: 21 additions & 7 deletions src/whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,33 @@
}


def currency_keyboard(one_time_keyboard: bool = False) -> ReplyKeyboardMarkup:
def currency_keyboard(currency_type: str) -> ReplyKeyboardMarkup:
"""Get keyboard with currencies from whitelists."""
keyboard = ReplyKeyboardMarkup(row_width=6, one_time_keyboard=one_time_keyboard)
keyboard = ReplyKeyboardMarkup(
row_width=6, one_time_keyboard=currency_type == "sell"
)
keyboard.row(*[KeyboardButton(c) for c in FIAT])
keyboard.add(*[KeyboardButton(c) for c in CRYPTOCURRENCY])
cancel_button = KeyboardButton(emojize(":x: ") + i18n("cancel"))
if currency_type == "sell":
keyboard.row(
KeyboardButton(emojize(":fast_reverse_button: ") + i18n("back")),
cancel_button,
)
else:
keyboard.row(cancel_button)
return keyboard


def gateway_keyboard(
currency: str, one_time_keyboard: bool = False
) -> ReplyKeyboardMarkup:
def gateway_keyboard(currency: str, currency_type: str) -> ReplyKeyboardMarkup:
"""Get keyboard with gateways of ``currency`` from whitelist."""
keyboard = ReplyKeyboardMarkup(row_width=6, one_time_keyboard=one_time_keyboard)
keyboard = ReplyKeyboardMarkup(
row_width=6, one_time_keyboard=currency_type == "sell"
)
keyboard.add(*[KeyboardButton(g) for g in CRYPTOCURRENCY[currency]])
keyboard.row(KeyboardButton(emojize(":fast_forward: ") + i18n("without_gateway")))
keyboard.row(
KeyboardButton(emojize(":fast_reverse_button: ") + i18n("back")),
KeyboardButton(emojize(":fast_forward: ") + i18n("without_gateway")),
KeyboardButton(emojize(":x: ") + i18n("cancel")),
)
return keyboard

0 comments on commit 5b447ef

Please sign in to comment.