skip to Main Content
@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

2

Answers


  1. You should await it await message.delete()

    Login or Signup to reply.
  2. You should delete the post after the cycle ends:

    @dp.message_handler(content_types=['text'])
    async def delete_messages(message: types.Message):
        linked = False
        for entity in message.entities:
            if entity.type in ["url", "text_link"]:
                linked = True
        if linked:
            await message.delete()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search