S
S
swpavlov2020-01-28 20:30:59
Python
swpavlov, 2020-01-28 20:30:59

How to split bot files?

def get_class(message):
    if str(message.text) == '11':
        bot.send_message(message.chat.id,'Загрузите фотографию 11')
        bot.register_next_step_handler(message, get_photo11)
    elif str(message.text) == '10':
        bot.send_message(message.chat.id,'Загрузите фотографию 10')
        bot.register_next_step_handler(message, get_photo10)
    elif str(message.text) == '9':
        bot.send_message(message.chat.id,'Загрузите фотографию 9')
        bot.register_next_step_handler(message, get_photo9)
    elif str(message.text) == '8':
        bot.send_message(message.chat.id,'Загрузите фотографию 8')
        bot.register_next_step_handler(message, get_photo8)
    elif str(message.text) == '7':
        bot.send_message(message.chat.id,'Загрузите фотографию 7')
        bot.register_next_step_handler(message, get_photo7)
    elif str(message.text) == '6':
        bot.send_message(message.chat.id,'Загрузите фотографию 6')
        bot.register_next_step_handler(message, get_photo6)
    elif str(message.text) == '5':
        bot.send_message(message.chat.id,'Загрузите фотографию 5')
        bot.register_next_step_handler(message, get_photo5)


I am new to programming. How to split one bot file with many def functions into multiple files? Divided into categories, so to speak. And in consequence, how to call these functions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-01-28
@swpavlov

Read about modules. And specifically your example, without any checks

def get_class(message):
    if int(message.text) in range(5, 12):
        bot.send_message(message.chat.id, f'Загрузите фотографию {message.text}')
        bot.register_next_step_handler(message, globals()[f'get_photo{message.text}'])

but I suspect that all these get_photoN can be replaced by one function,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question