From d77288ea0735d981bc46e24b50e4c37c73855a5d Mon Sep 17 00:00:00 2001 From: Denis Date: Sat, 19 Jul 2025 16:58:42 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D1=81?= =?UTF-8?q?=D0=BE=20=D0=B2=D1=81=D0=B5=D0=BC=D0=B8=20=D0=BA=D0=BD=D0=BE?= =?UTF-8?q?=D0=BF=D0=BA=D0=B0=D0=BC=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D0=B2=D0=B8=D0=B0=D1=82=D1=83=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/keyboard/main.py | 153 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 examples/keyboard/main.py diff --git a/examples/keyboard/main.py b/examples/keyboard/main.py new file mode 100644 index 0000000..4ea23d3 --- /dev/null +++ b/examples/keyboard/main.py @@ -0,0 +1,153 @@ +import asyncio +import logging + +from maxapi import Bot, Dispatcher + +# Кнопки +from maxapi.types import ( + ChatButton, + LinkButton, + CallbackButton, + RequestGeoLocationButton, + MessageButton, + ButtonsPayload, # Для постройки клавиатуры без InlineKeyboardBuilder + RequestContactButton, + OpenAppButton, +) + +from maxapi.types import ( + MessageCreated, + MessageCallback, + MessageChatCreated, + CommandStart, + Command +) + +from maxapi.utils.inline_keyboard import InlineKeyboardBuilder + +logging.basicConfig(level=logging.INFO) + +bot = Bot('тут_ваш_токен') +dp = Dispatcher() + + +@dp.message_created(CommandStart()) +async def echo(event: MessageCreated): + await event.message.answer( + ( + 'Привет! Мои команды:\n\n' + + '/builder - Клавиатура из InlineKeyboardBuilder\n' + '/pyaload - Клавиатура из pydantic моделей\n' + ) + ) + + +@dp.message_created(Command('builder')) +async def echo(event: MessageCreated): + builder = InlineKeyboardBuilder() + + builder.row( + ChatButton( + text="Создать чат", + chat_title='Test', + chat_description='Test desc' + ), + LinkButton( + text="Канал разработчика", + url="https://t.me/loveapples_dev" + ), + ) + + builder.row( + RequestGeoLocationButton(text="Геолокация"), + MessageButton(text="Сообщение"), + ) + + builder.row( + RequestContactButton(text="Контакт"), + OpenAppButton( + text="Приложение", + web_app=event.bot.me.username, + contact_id=event.bot.me.user_id + ), + ) + + builder.row( + CallbackButton( + text='Callback', + payload='test', + ) + ) + + await event.message.answer( + text='Клавиатура из InlineKeyboardBuilder', + attachments=[ + builder.as_markup() + ]) + + +@dp.message_created(Command('payload')) +async def echo(event: MessageCreated): + buttons = [ + [ + # кнопку типа "chat" убрали из документации, + # возможны баги + ChatButton( + text="Создать чат", + chat_title='Test', + chat_description='Test desc' + ), + LinkButton( + text="Канал разработчика", + url="https://t.me/loveapples_dev" + ), + ], + [ + RequestGeoLocationButton(text="Геолокация"), + MessageButton(text="Сообщение"), + ], + [ + RequestContactButton(text="Контакт"), + OpenAppButton( + text="Приложение", + web_app=event.bot.me.username, + contact_id=event.bot.me.user_id + ), + ], + [ + CallbackButton( + text='Callback', + payload='test', + ) + ] + ] + + buttons_payload = ButtonsPayload(buttons=buttons).pack() + + await event.message.answer( + text='Клавиатура из pydantic моделей', + attachments=[ + buttons_payload + ]) + + +@dp.message_chat_created() +async def callback(obj: MessageChatCreated): + await obj.bot.send_message( + chat_id=obj.chat.chat_id, + text=f'Чат создан! Ссылка: {obj.chat.link}' + ) + + +@dp.message_callback() +async def callback(callback: MessageCallback): + await callback.message.answer('Вы нажали на Callback!') + + +async def main(): + await dp.start_polling(bot) + + +if __name__ == '__main__': + asyncio.run(main()) \ No newline at end of file