V
V
VladRF4R4R2021-12-15 00:03:17
Python
VladRF4R4R, 2021-12-15 00:03:17

How to create inline buttons in Telegram using JSON?

Good evening!!!
I am writing an asynchronous bot in the aiogram library, and I don’t know why I can’t create a keyboard.
I'll be grateful for the tips.

@dp.message_handler(commands=["Час"])
async def sendInlineMessageForBookingTime(message: types.Message):
    global buttons
    current_time = datetime.datetime.now()
    current_hour = str(current_time)[11:13]
    if int(current_hour) < 8:
        buttons = [
            types.InlineKeyboardButton(text='8:00'),
            types.InlineKeyboardButton(text='9:00'),
            types.InlineKeyboardButton(text='10:00'),
            types.InlineKeyboardButton(text='11:00'),
            types.InlineKeyboardButton(text='12:00'),
            types.InlineKeyboardButton(text='13:00'),
            types.InlineKeyboardButton(text='14:00'),
            types.InlineKeyboardButton(text='15:00'),
            types.InlineKeyboardButton(text='16:00'),
            types.InlineKeyboardButton(text='17:00'),
            types.InlineKeyboardButton(text='18:00'),
            types.InlineKeyboardButton(text='19:00'),
        ]
    elif 8 <= int(current_hour) < 10:\
        buttons = [
            types.InlineKeyboardButton(text='10:00'),
            types.InlineKeyboardButton(text='11:00'),
            types.InlineKeyboardButton(text='12:00'),
            types.InlineKeyboardButton(text='13:00'),
            types.InlineKeyboardButton(text='14:00'),
            types.InlineKeyboardButton(text='15:00'),
            types.InlineKeyboardButton(text='16:00'),
            types.InlineKeyboardButton(text='17:00'),
            types.InlineKeyboardButton(text='18:00'),
            types.InlineKeyboardButton(text='19:00'),
        ]
    elif 10 <= int(current_hour) < 12:\
        buttons = [
            types.InlineKeyboardButton(text='12:00'),
            types.InlineKeyboardButton(text='13:00'),
            types.InlineKeyboardButton(text='14:00'),
            types.InlineKeyboardButton(text='15:00'),
            types.InlineKeyboardButton(text='16:00'),
            types.InlineKeyboardButton(text='17:00'),
            types.InlineKeyboardButton(text='18:00'),
            types.InlineKeyboardButton(text='19:00'),
        ]
    elif 12 <= int(current_hour) < 14:\
        buttons = [
            types.InlineKeyboardButton(text='14:00'),
            types.InlineKeyboardButton(text='15:00'),
            types.InlineKeyboardButton(text='16:00'),
            types.InlineKeyboardButton(text='17:00'),
            types.InlineKeyboardButton(text='18:00'),
            types.InlineKeyboardButton(text='19:00'),
        ]
    elif 14 <= int(current_hour) < 16:\
        buttons= [
            types.InlineKeyboardButton(text='16:00'),
            types.InlineKeyboardButton(text='17:00'),
            types.InlineKeyboardButton(text='18:00'),
            types.InlineKeyboardButton(text='19:00'),
        ]
    elif 16 <= int(current_hour) < 18:\
        buttons = [
            types.InlineKeyboardButton(text='18:00'),
            types.InlineKeyboardButton(text='19:00'),
        ]
    elif 18 <= int(current_hour) < 20:\
        buttons = [
            types.InlineKeyboardButton(text='19:00'),
        ]
    keyboard = types.InlineKeyboardMarkup(row_width=2)
    keyboard.row(*buttons)
    await bot.send_message(message.chat.id, f"{message.from_user.last_name} виберіть зручний час для запису",reply_markup=keyboard)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2021-12-15
@FCKJesus

Example from my bot

def get_accounts_btn(accounts: list) -> InlineKeyboardMarkup:
  buttons_list = []
  for account in accounts:
    if account['bot_is_active'] == 1:
      active = ' '
    else:
      active = ' '
    buttons_list.append([
      InlineKeyboardButton(
        str('Войти как: {0} {1}'.format(account['account_id'], active)),
        callback_data='num_{0}'.format(account['account_id']))
    ])

    kb = InlineKeyboardMarkup(inline_keyboard=buttons_list)
  return kb

async def get_list_accounts(message: types.Message):
    user_id = message.from_user.id
    ...
        accounts = await commands.get_account_id_bot_is_active(user_id)
        if accounts:
            await message.answer('Выбери аккаунт для входа', reply_markup=get_accounts_btn(accounts))
        else:
            await message.answer('Аккаунтов не найдено')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question