V
V
visual_ceo2022-04-19 20:07:18
Python
visual_ceo, 2022-04-19 20:07:18

File not importing?

I am creating a telegram bot, at the moment there are two files, the main app.py and the second for handlers, so as not to fill in the main file. I created the handlers in the corresponding file and imported them into the main file, but the import does not work, but when I transfer the written handlers to the main file, everything works. I poked around on the Internet and found only what you need to register handlers through the folder __init__ created and wrote the following there
< from .handlers import dp
__all__ = ['dp'] > But it still does not work

Here is the code of the main file

from aiogram import Bot, Dispatcher, types
from aiogram.utils import executor
import handlers

bot = Bot(token='token matching')
dp = Dispatcher(bot)

@dp.message_handler(commands='start')
async def start(message: types.Message):
chat_id = message.chat.id
await bot.send_message(chat_id=chat_id, text='Welcome in bot ')

@dp.message_handler()
async def echo(message: types.Message ):
text = message.text
await message.answer(text) # I want to write these handlers in another file and import them into this one, but the import does not work

executor.start_polling(dp)

Here is the separate code for handlers,

from app import bot, dp
from aiogram import types

@dp.message_handler(commands='start')
async def start(message: types.Message):
chat_id = message.chat.id
await bot.send_message(chat_id=chat_id, text='Welcome in bot for start' )
await message.answer(text='Welcome it is message.answer for start')
await message.reply(text='Welcome it is message.reply for start')

@dp.message_handler()
async def echo(message: types. Message):
text = message.text
await message.answer(text)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question