How can I configure my Telegram bot to respond only when directly mentioned in a group?

I have a Telegram bot that is part of a group chat, and I want it to react exclusively when someone directly mentions it in a message. Is there a known technique or API feature that helps detect these direct mentions? I would appreciate any advice or sample code that demonstrates how to set up this functionality, ensuring that the bot only processes messages where it is explicitly referenced.

The solution that worked for me involved inspecting the message entities that Telegram returns. When your bot receives a message, you can check if it contains an entity of type ‘mention’. This requires verifying that the specific mention includes your bot’s username. In my implementation, I looped through any entities in the message object and processed the message only if the bot device was explicitly referenced. This approach worked reliably, and combining it with simple string matching provided a robust solution, even in groups with many active users.

i used a regex check for direct ‘@yourbotname’ in the message text instead of relying solely on entities. it was simpler and didnt require looping thru entities on every msg. hope this helps u get the direct mention functionality working!