Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 698
Awang Setyawan Avatar asked Oct 14 '16 17:10

Awang Setyawan


People also ask

How can I mention a user in Telegram?

Starting today, you can mention any members in a group – even if they don't have a username. Just type the @ symbol and select whoever you would like to address.

How can I find people in Telegram without username?

From now on, you can choose a public username in the Settings section of Telegram. If you do, anyone will be able to find you by your username and contact you – without having to know your phone number. To find people by username, just start typing any name in the search field of the Contacts section.

Is username and Telegram ID same?

On Telegram, you've got your Telegram ID, and then you've got your Telegram user ID. The former is the name that you chose. To change your Telegram ID, go to the hamburger menu (three horizontal lines) and then go to Settings. The same goes on your mobile app, go to the hamburger menu, then go to Settings.

Can Telegram bot call a user?

As I know, a bot can't call a telegram user.


4 Answers

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)

like image 146
Anatoly Rugalev Avatar answered Oct 15 '22 20:10

Anatoly Rugalev


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>
like image 22
GameO7er Avatar answered Oct 15 '22 18:10

GameO7er


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")
like image 32
Batichico Avatar answered Oct 15 '22 19:10

Batichico


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")
like image 23
AKMalkadi Avatar answered Oct 15 '22 18:10

AKMalkadi