Telegram bot: How to mention user by its id (not its username)
Asked Answered
F

7

61

I am creating a telegram bot and using sendMessage method to send the messages. it is easy to mention user using @username, But how to mention user when they don't have username?

If using the telegram app/web, we can mentioned the user by @integer_id (name), and telegram app/web will convert it into clickable text. integer_id will be generated automatically when we select the user, after typing @.

another background: I am trying to use forceReply and I want to target user, if they have username, I can easily target them, by mentioning them on the text on sendMessage method.

the bot I am creating is a "quiz" like bot. where each player need to take turn, and the bot is sending them the question, each msg from bot will target different player.

NOTE: I am not disabling the Privacy Mode, I don't want telegram bombing my server with msg I don't need. it was overloading my cheap nasty server. so, disabling it not an option.

I am open for other solution, where the bot can listen to selected player.

thanks.

UPDATE 21/10: I've spoke to BotSupport for telegram, they said, for now Bots can't mention user without username.

so in my case, I still keep using forceReply, and also, gave a short msg to user which doesn't have username to set it up, so they can get the benefit from forceReply function.

Fortuneteller answered 14/10, 2016 at 17:3 Comment(0)
S
57

According to official documentation it is possible to mention user by its numerical id with markup:

[inline mention of a user](tg://user?id=123456789)

Sialoid answered 19/9, 2017 at 22:38 Comment(3)
These mentions are only guaranteed to work if the user has contacted the bot in the past, has sent a callback query to the bot via inline button or is a member in the group where he was mentioned.Zuckerman
Another problem with this method is that it will notify the user, even if you set disable_notification.Eglanteen
Not working if the user disables "Forwarded Messages" from privacy settingHaemoglobin
M
28

According to this link : it is possible to mention user by its numerical id with markup:

Markdown style

To use this mode, pass Markdown in the parse_mode field when using sendMessage. Use the following syntax in your message:

[inline mention of a user](tg://user?id=123456789)

and you can also use HTML style :

HTML style

To use this mode, pass HTML in the parse_mode field when using sendMessage. The following tags are currently supported:

<a href="tg://user?id=123456789">inline mention of a user</a>
Moradabad answered 19/9, 2019 at 11:23 Comment(0)
O
6

Try this:

@bot.message_handler(func=lambda message: True)     
def echo_message(message):
    cid = message.chat.id 
    message_text = message.text 
    user_id = message.from_user.id 
    user_name = message.from_user.first_name 
    mention = "["+user_name+"](tg://user?id="+str(user_id)+")"
    bot_msg = f"Hi, {mention}"
    if message_text.lower() == "hi":
        bot.send_message(cid, bot_msg, parse_mode="Markdown")
Omnivore answered 27/6, 2019 at 9:42 Comment(0)
S
2

For python-telegram-bot you can do the following:

user_id = update.message.from_user['id']
user_name = update.message.from_user['username']

mention = "["+user_name+"](tg://user?id="+str(user_id)+")"
response = f"Hi, {mention}"

context.bot.sendMessage(chat_id=update.message.chat_id,text=response,parse_mode="Markdown")
Surfboard answered 13/9, 2021 at 2:56 Comment(0)
A
1

No, this restriction is related to Telegram's privacy policy and prevention of abuse.

It is possible to mention a user when sending messages (BOT API), but that is not what you need:

[inline mention of a user](tg://user?id=<user_id>)

Links tg://user?id= can be used to mention a user by their id without using a username. Please note:

These links will work only if they are used inside an inline link. For example, they will not work, when used in an inline keyboard button or in a message text.
These mentions are only guaranteed to work if the user has contacted the bot in the past, has sent a callback query to the bot via inline button or is a member in the group where he was mentioned.

https://core.telegram.org/bots/api#markdown-style

Anny answered 18/10, 2021 at 16:10 Comment(0)
I
1

you need to link to the text: "tg://user?id=" and id

user_id = 123456XX # id of the user to mention
chat_id = 123456XXX # chat id where to mention
user_name = name of user
await bot.send_message(chat_id, f"<a href='tg://user?id={user_id}'>{user_name}</a>", "HTML")

here is an example:

@dp.message_handler()
async def mention(msg: types.Message):
    await msg.answer(f"<a href='tg://user?id={msg.from_user.id}'>{msg.from_user.full_name}</a>", "HTML")
Induct answered 26/12, 2022 at 2:32 Comment(0)
C
-4

Bots are able to tag users by their ID, they just can't do this using the official HTTP Bot API.

Update: Not necessairy anymore, since Telegram added native Support for this.

If you log into your bots account with MadelineProto (PHP) you can use this 'link' to mention someone by it's ID with parse_mode set to markdown


[Daniil Gentili](mention:@danogentili)

Crosspollinate answered 20/4, 2017 at 20:7 Comment(3)
OP wants to know how to mention users by their user ID and It's a 32bit integer, not by username that you suggestedExmoor
@Reith Have a look at the MadelineProto Documentation. You could hust replace the @username with a number. E.g. [Daniil Gentili](mention:123456789)Crosspollinate
MadelineProto uses MTProto, not a Telegram Bot APIAuriculate

© 2022 - 2024 — McMap. All rights reserved.