M
M
maqstein2019-09-07 14:29:06
Python
maqstein, 2019-09-07 14:29:06

How to import handlers from different files?

I don't want the handlers in my bot to be in the same file, as this interferes with the readability of the code and the architecture
. I have such a hierarchy
5d73939ba27a5377683327.png
here

from mess import *
from aiogram.utils import executor
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.redis import RedisStorage2

bot = Bot(token)
storage = RedisStorage2()
dp = Dispatcher(bot, storage=storage)

@dp.message_handler(commands=['start'])
async def p(m: types.Message):
    await bot.send_message(m.chat.id, 'hello')

executor.start_polling(dp)

here is mess code
import sys
sys.path.append("..")
from main import dp,types
@dp.message_handler(commands=['stop'])
async def p(m: types.Message):
    await bot.send_message(m.chat.id, 'goodbye!')

there are no errors when starting the bot, but only the start command works in the telegram itself,
I found this repository on the github, https://github.com/PavlMais/wrapper_for_bot
it looks like my problem is solved there, but there is no "hundler" file from which to import init function
, tell me how to implement import from different files, and if you point out errors or better solutions to something, I will be grateful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zxChain, 2020-08-20
@zxChain

modulesCreate a file in the folder__init__.py

from .mess import dp

__all__ = ['dp']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question