'Aiogram Telegram delete messages with links

@dp.message_handler(lambda message: message.entities != [] and message.chat.id)
async def delete_messages(message: types.Message):
    for entity in message.entities:
        if entity.type in ["url", "text_link"]:
            await bot.delete_message(message.from_user.id, message.message_id)
            break

I tried this, but the message does not want to be deleted, tell me, please, what is the error? I would also be grateful if you tell me how to make an exception. So that links from a specific domain are not deleted (there will be several of them)

@dp.message_handler(content_types=['text'])
async def delete_messages(message: types.Message):
    for entity in message.entities:
        if entity.type in ["url", "text_link"]:
            await message.delete()

This didn't help either



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source