Search code examples
pythontelegram-botpy-telegram-bot-apitelebot

InlineKeyboardMarkup pyTelegramBotAPI


I am writing a telegram bot using a calendar.

There is such a construction:

def create_callback_data_time(action, time):
    """ Create the callback data associated to each button"""
    return ";".join([action, str(time)])

def create_time():
    markup_time = {"inline_keyboard": []}

    row = []
    for time in ["10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00"]:
        row.append({"text": time, "callback_data": create_callback_data_time("TIME", time)})

    markup_time["inline_keyboard"].append(row)
    return json.dumps(markup_time)

When calling the create_time() function, the bot adds data from the list to the InlineKeyboard with binding to callback_data. However, the buttons in the InlineKeyboard are added horizontally

enter image description here

And I need it vertically, that is, every hour from a new line!

Please give me some tips! I'm just learning, and this is the first full-fledged project

I took the calendar for the bot from here: https://github.com/grcanosa/telegram-calendar-keyboard

Thank you all in advance for your help!

I tried to use the Keybo library but without success

from keyboa import Keyboa



getting_list_of_services = core.check_service()
    list_services = []

    for key, values in enumerate(getting_list_of_services):
        list_services.append(values)

    keyboard = Keyboa( items=list_services, front_marker="SER", back_marker="VICE").items

    return keyboard

Solution

  • A row inside an inline_keyboard is defined by a nested array.

    So instead off appending to row, append to inline_keyboard self so that each time get it's own array inside inline_keyboard:

    def create_time():
        markup_time = {"inline_keyboard": []}
    
        for time in ["10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00"]:
            markup_time["inline_keyboard"].append([ {"text": time, "callback_data": create_callback_data_time("TIME", time)} ])
    
        return json.dumps(markup_time)