Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to repair my aiogram code? Dispatcher problem

I want to have output "Hi" after entering "/Hello". It is working with

@dp.message_handler(commands=["Hello"]) 

but isn’t with

def register_handlers_client(dp : Dispatcher):
    dp.register_message_handler(raspisanije, commands=["Hello"])

What’s the problem?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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


btn1 = KeyboardButton("/Hello")
mainMenu1 = ReplyKeyboardMarkup(resize_keyboard = True).add(btn1)

# @dp.message_handler(commands=["Hello"]) 
async def raspisanije(message: types.Message):
    await bot.send_message(message.from_user.id, "Hi")

def register_handlers_client(dp : Dispatcher):
    dp.register_message_handler(raspisanije, commands=["Hello"])

if __name__ == "__main__":
    executor.start_polling(dp, skip_updates=True)

>Solution :

The register_handlers_client function is not being called.
You need to call it in the if __name__ == "__main__": so it becomes:

if __name__ == "__main__":
    register_handlers_client(dp)
    executor.start_polling(dp, skip_updates=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading