expense-bot/handlers/users/test.py

79 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from aiogram import types
from loader import dp
@dp.message_handler(commands="test")
async def cmd_random(message: types.Message):
keyboard = types.InlineKeyboardMarkup()
keyboard.add(types.InlineKeyboardButton(text="Нажми меня", callback_data="random_value"))
keyboard.add(types.InlineKeyboardButton(text="Нажми меня 2", callback_data="random_value2"))
await message.answer("test reply 1-2-3", reply_markup=keyboard)
@dp.callback_query_handler(text="random_value")
async def send_random_value(call: types.CallbackQuery):
await call.message.answer('test reply 1')
# shows alert with 'Ok' button
await call.answer(text="Спасибо, что воспользовались ботом!", show_alert=True)
# или просто await call.answer()
@dp.callback_query_handler(text="random_value2")
async def send_random_value(call: types.CallbackQuery):
await call.message.answer('test reply 2')
await call.answer()
########################################################################################
from aiogram.utils.callback_data import CallbackData
from aiogram.utils.exceptions import MessageNotModified
from contextlib import suppress
# fabnum - префикс, action - название аргумента, которым будем передавать значение
callback_numbers = CallbackData("fabnum", "action")
# Здесь хранятся пользовательские данные.
# Т.к. это словарь в памяти, то при перезапуске он очистится
user_data = {}
def get_keyboard_fab():
buttons = [
types.InlineKeyboardButton(text="-1", callback_data=callback_numbers.new(action="decr")),
types.InlineKeyboardButton(text="+1", callback_data=callback_numbers.new(action="incr")),
types.InlineKeyboardButton(text="Подтвердить", callback_data=callback_numbers.new(action="finish"))
]
keyboard = types.InlineKeyboardMarkup(row_width=2)
keyboard.add(*buttons)
return keyboard
async def update_num_text_fab(message: types.Message, new_value: int):
with suppress(MessageNotModified):
await message.edit_text(f"Укажите число: {new_value}", reply_markup=get_keyboard_fab())
@dp.message_handler(commands="numbers_fab")
async def cmd_numbers(message: types.Message):
user_data[message.from_user.id] = 0
await message.answer("Укажите число: 0", reply_markup=get_keyboard_fab())
@dp.callback_query_handler(callback_numbers.filter(action=["incr", "decr"]))
async def callbacks_num_change_fab(call: types.CallbackQuery, callback_data: dict):
user_value = user_data.get(call.from_user.id, 0)
action = callback_data["action"]
if action == "incr":
user_data[call.from_user.id] = user_value + 1
await update_num_text_fab(call.message, user_value + 1)
elif action == "decr":
user_data[call.from_user.id] = user_value - 1
await update_num_text_fab(call.message, user_value - 1)
await call.answer()
@dp.callback_query_handler(callback_numbers.filter(action=["finish"]))
async def callbacks_num_finish_fab(call: types.CallbackQuery):
user_value = user_data.get(call.from_user.id, 0)
await call.message.edit_text(f"Итого: {user_value}")
await call.answer()