I’m building a Telegram bot that uses the postMessage function to send messages. Tagging users with @username works fine, but what about users who haven’t set a username?
In the regular Telegram client, you can mention someone by typing @user_id (display_name) and it becomes a clickable mention. The user_id gets filled automatically when you pick someone from the list after typing @.
My specific situation: I’m using forceReply and need to target specific users. When they have usernames, mentioning them in the message text works perfectly.
This is for a quiz bot where players take turns answering questions. Each message from the bot needs to target a different player.
I want to keep Privacy Mode enabled because disabling it floods my server with unnecessary messages.
Are there alternative approaches to make the bot respond only to the intended player?
I ran into this exact issue when developing a group management bot last year. The solution is to use text entities with the mention type instead of plain text mentions. When sending messages through the Bot API, you need to construct the message with entities parameter that specifies the mention type and the user_id.
For your quiz bot scenario, construct your message text normally (like “John, it’s your turn to answer”) but include an entities array where you specify the offset, length, type as “text_mention”, and the user object with their ID. This creates a proper clickable mention even without usernames.
Regarding targeting specific players while keeping Privacy Mode enabled, you could implement a command-based system where players use specific commands like /answer followed by their response. The bot can track whose turn it is internally and only process answers from the correct player, ignoring others. This approach worked well for my turn-based bot and eliminates the need to disable Privacy Mode entirely.
To effectively tag users without usernames in your Telegram bot, utilize the text_mention entity type when sending messages. In your API call, set the entities parameter to include the type text_mention and the complete user object. This allows for clickable mentions even for users lacking a username. Additionally, consider implementing a state management system to keep track of turns in your quiz bot. Use forceReply to ensure only the designated player can respond, matching the incoming message from field with the expected player ID. This maintains Privacy Mode while limiting responses to the correct player. Another effective tactic is to employ inline keyboards with custom callback data for each user, enhancing engagement and control over message interactions.
yeah you can use text_mention but there’s another trick - try sending a reply to the users previous message if they sent one recently. this way telegram automatically targets them even without username. also for quiz bots i usually store the expected player_id in memory and just check msg.from.id against it, works great with privacy mode on.