'Telegram-bot replies to messages from specific user in group chat (aiogram)

I want my bot to reply to a certain person in chat every time he writes (in fact every 2 hrs, like regular cuddling) e.g.

User1: Good morning everyone

User2: Good morning

Bot: (reply to User2): Have a nice day!

Now i can send messages to group from bot

@dp.message_handler(commands="HAD")
async def cmd_dice(message: types.Message):
    await message.bot.send_message($here chat_id$, text="Have a nice day!")

I don't understand how I can reply to user messages if he doesn't address the bot. Is this possible? Thanks in advance for your help!



Solution 1:[1]

  1. To get all the messages in the bot group, you need to turn off Privacy mode. Open the @botfather. Send the /mybots command. Select the bot. bot_settings --> allow_groups --> turn_groups_of

  2. Use the magic_filter

...
from magic_filter import F
...


...
@dp.message_handler(F.from_user.id.in_({42, 1000, 123123})) # 42, 1000, 123123 users id
async def message_sender(message: types.Message):
    await message.answer(text="Have a nice day!")
...

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Shahobiddin Anorboyev