0

I was writing a Telegram bot noticed that it doesn't respond to photos. This echo bot should copy every message (other than /start) and sendit back using message.send_copy(message.from_id). It works on simple text messages, but if I send a photo to the bot, it doesn't do anything. Here is the code:

import config
from aiogram import Bot, Dispatcher, executor, types

bot = Bot(config.TOKEN, parse_mode="HTML")
dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def start_cmd(message: types.Message):
    await message.answer('Hello!')

@dp.message_handler()
async def echo(message: types.Message):
    await message.send_copy(message.from_id)

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

I tried to use content_types='*' like this:

@dp.message_handler(content_types='*')
async def echo(message: types.Message):
    await message.send_copy(message.from_id)

but this did not work.

Kolay.Ne
  • 1,345
  • 1
  • 8
  • 23
TimKostenok
  • 52
  • 11
  • 1
    Take a look at the [documentation](https://docs.aiogram.dev/en/latest/dispatcher/index.html?highlight=content_types#aiogram.Dispatcher.register_message_handler): `register_message_handler` and `message_handler`. The attribute `content_types` should be a list of `aiogram.types.message.ContentType`s – Kolay.Ne Jul 27 '23 at 17:55
  • Thanks you! I also found a way to set content_types to [Any](https://docs.aiogram.dev/en/dev-3.x/api/enums/content_type.html#aiogram.enums.content_type.ContentType.ANY): `ContentType.ANY`, that equals to string 'any' – TimKostenok Jul 27 '23 at 18:10

1 Answers1

1

Thanks Kolay.Ne I found what I was searching. I should modify my code like this:

import config
from aiogram import Bot, Dispatcher, executor, types
from aiogram.enums.content_type import ContentType

bot = Bot(config.TOKEN, parse_mode="HTML")
dp = Dispatcher(bot)

@dp.message_handler(commands=['start'])
async def start_cmd(message: types.Message):
    await message.answer('Hello!')

@dp.message_handler(content_types=ContentType.ANY) # or 'any'
async def echo(message: types.Message):
    await message.send_copy(message.from_id)

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

replacing content_types='*' with ContentType.ANY.

TimKostenok
  • 52
  • 11