Last active
July 18, 2023 09:21
-
-
Save oktober13/a1461086ce91965c4b68732b008ef0f7 to your computer and use it in GitHub Desktop.
A template for creating interactive keyboards for your bot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from aiogram import Bot, Dispatcher, types | |
API_TOKEN = 'your_token' | |
logging.basicConfig(level=logging.INFO) | |
bot = Bot(token=API_TOKEN) | |
dp = Dispatcher(bot) | |
async def send_message_with_keyboard(chat_id: int, text: str, keyboard_markup: types.ReplyKeyboardMarkup): | |
await bot.send_message(chat_id, text, reply_markup=keyboard_markup) | |
def create_inline_keyboard_button(text: str, callback_data: str): | |
return types.InlineKeyboardButton(text=text, callback_data=callback_data) | |
def create_reply_keyboard_button(text: str): | |
return types.KeyboardButton(text=text) | |
def create_reply_keyboard(buttons: list): | |
keyboard_markup = types.ReplyKeyboardMarkup(resize_keyboard=True) | |
for button_text in buttons: | |
keyboard_markup.add(create_reply_keyboard_button(button_text)) | |
return keyboard_markup | |
def create_inline_keyboard(buttons: list, row_width: int = 2): | |
keyboard_markup = types.InlineKeyboardMarkup(row_width=row_width) | |
for button_data, button_text in buttons: | |
keyboard_markup.add(create_inline_keyboard_button(button_text, button_data)) | |
return keyboard_markup | |
if __name__ == '__main__': | |
# Usage example: | |
# reply_buttons = ['Button 1', 'Button 2'] | |
# reply_markup = create_reply_keyboard(reply_buttons) | |
# await send_message_with_keyboard(chat_id, "Select a button:", reply_markup) | |
# | |
# inline_buttons = [('data1', 'Button 1'), ('data2', 'Button 2')] | |
# inline_markup = create_inline_keyboard(inline_buttons) | |
# await send_message_with_keyboard(chat_id, "Select a button:", inline_markup) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment