Home > Net >  How to repair my aiogram code? Dispatcher problem
How to repair my aiogram code? Dispatcher problem

Time:04-27

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?

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)

CodePudding user response:

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)
  • Related