-
Notifications
You must be signed in to change notification settings - Fork 1
Keyboards
Maksym Zavalniuk edited this page Sep 8, 2023
·
1 revision
- In the
keyboards/reply/your_keyboard.py
create function:
def keyboard():
builder = ReplyKeyboardBuilder()
for i in range(1, 17):
builder.add(types.KeyboardButton(text=str(i)))
builder.adjust(4)
return builder.as_markup(resize_keyboard=True)
- Add to your handler
@dp.message(Command("special_buttons"))
async def cmd_special_buttons(message: types.Message):
builder = ReplyKeyboardBuilder()
builder.row(
types.KeyboardButton(text="Ask geoposition", request_location=True),
types.KeyboardButton(text="Ask contact", request_contact=True)
)
builder.row(types.KeyboardButton(
text="Create poll",
request_poll=types.KeyboardButtonPollType(type="quiz"))
)
await message.answer(
"Choose an action:",
reply_markup=builder.as_markup(resize_keyboard=True),
)
from aiogram.utils.keyboard import InlineKeyboardBuilder
@dp.message(Command("inline_url"))
async def cmd_inline_url(message: types.Message, bot: Bot):
builder = InlineKeyboardBuilder()
builder.row(types.InlineKeyboardButton(
text="GitHub", url="https://github.com")
)
builder.row(types.InlineKeyboardButton(
text="Telegram",
url="tg://resolve?domain=telegram")
)
user_id = 1234567890
chat_info = await bot.get_chat(user_id)
if not chat_info.has_private_forwards:
builder.row(types.InlineKeyboardButton(
text="User",
url=f"tg://user?id={user_id}")
)
await message.answer(
'Choose a link',
reply_markup=builder.as_markup(),
)
from typing import Optional
from aiogram.filters.callback_data import CallbackData
class NumbersCallbackFactory(CallbackData, prefix="fabnum"):
action: str
value: Optional[int] = None
def get_keyboard_fab():
builder = InlineKeyboardBuilder()
builder.button(
text="-2", callback_data=NumbersCallbackFactory(action="change", value=-2)
)
builder.button(
text="-1", callback_data=NumbersCallbackFactory(action="change", value=-1)
)
builder.button(
text="+1", callback_data=NumbersCallbackFactory(action="change", value=1)
)
builder.button(
text="+2", callback_data=NumbersCallbackFactory(action="change", value=2)
)
builder.button(
text="Подтвердить", callback_data=NumbersCallbackFactory(action="finish")
)
builder.adjust(4)
return builder.as_markup()
@dp.callback_query(NumbersCallbackFactory.filter(F.action == "change"))
async def callbacks_num_change_fab(
callback: types.CallbackQuery,
callback_data: NumbersCallbackFactory
):
user_value = user_data.get(callback.from_user.id, 0)
user_data[callback.from_user.id] = user_value + callback_data.value
await update_num_text_fab(callback.message, user_value + callback_data.value)
await callback.answer()
@dp.callback_query(NumbersCallbackFactory.filter(F.action == "finish"))
async def callbacks_num_finish_fab(callback: types.CallbackQuery):
user_value = user_data.get(callback.from_user.id, 0)
await callback.message.edit_text(f"Итого: {user_value}")
await callback.answer()