Search code examples
pythonpython-telegram-bot

aiogram message handler not firing for a message containing media


I am using aiogram 2.11.2 as Python interface to the Telegram API, but I am encountering an issue in the simplest callback: it does activate when the message is text only, but fails whenever any media is attached. This includes photos, videos, audios, stickers and GIFs, with or without caption.

I hope I'm not missing out on something.

import aiogram


class TelegramBot(object):
    def __init__(self):
        self.bot = aiogram.Bot(token="TOKEN")
        self.dispatcher = aiogram.Dispatcher(bot=self.bot)
        self.dispatcher.register_message_handler(self.on_msg,)
    
    async def on_msg(self, msg: aiogram.types.Message):
        print("Message received in telegram")

Solution

  • Turns out I was really missing out on something. To listen for all kinds of messages, it is sufficient to define content_type as

    dispatcher.register_message_handler(self.on_msg, content_type=aiogram.types.ContentType.all())
    

    Thanks to the answerer to my Git issue.