Reputation: 611
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.
Upvotes: 61
Views: 131524
Reputation: 1
const mentionString = `[${mention(ctx.from.first_name, ctx.from.id).text}](tg://user?id=${ctx.from.id})`
ctx.reply(
`Buyer: ${groupData[groupId].buyer.mention}!\nSeller: ${groupData[groupId].seller.mention}!\nClick on crypto to choose cryptocurrency`,
{
parse_mode: 'Markdown',
...Markup.inlineKeyboard([
Markup.button.callback('Choose currency', 'choose_crypto_method')
]),
}
);
This way you can mention a use easily by using parse_mode: 'Markdown'
easily even with Inline buttons, you just have to wrap entities of message in a single {}
For Example:
{ parse_mode: 'Markdown', ... }
| Do some amazing stuff after ... (i.e, Inline buttons etc)
Upvotes: 0
Reputation: 4198
This works for me for any user_id (clickable on Android, but not on Web)
[username](tg://openmessage?user_id=9000000009)
Upvotes: 0
Reputation: 87
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")
Upvotes: 1
Reputation: 46
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
Upvotes: 1
Reputation: 982
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")
Upvotes: 2
Reputation: 714
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")
Upvotes: 6
Reputation: 2106
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 usingsendMessage
. 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>
Upvotes: 30
Reputation: 115
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)
Upvotes: -4
Reputation: 1168
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)
Upvotes: 59