Telegram机器人:如何通过用户ID(而不是用户名)提及用户

57
我正在创建一个Telegram机器人,并使用 sendMessage 方法发送消息。在文本中使用 @username 很容易提及用户,但是当他们没有用户名时该怎么办呢?
如果在Telegram应用程序/网页上使用,我们可以通过@integer_id (name)来提及用户,Telegram应用程序/网页将其转换为可点击的文本。在选择用户并输入 @ 后,integer_id 会自动生成。
另一个背景:我正在尝试使用forceReply,我想要针对用户,如果他们有用户名,我可以轻松地在 sendMessage 方法的文本中提到他们。
我创建的机器人是类似“Quiz”的机器人。每个玩家需要轮流进行,机器人会向他们发送问题,每个消息都会针对不同的玩家。
注意:我没有禁用Privacy Mode,我不希望Telegram向服务器发送我不需要的消息。这会过载我的廉价服务器。因此,禁用它不是一个选项。
我愿意接受其他解决方案,让机器人可以听取所选的玩家。
更新于21/10: 我已经与Telegram的BotSupport联系,他们说,目前机器人无法提及没有用户名的用户。
因此,在我的情况下,我仍然使用forceReply,并向没有用户名的用户发一条简短的消息来设置它们,以便他们可以从forceReply功能中受益。
7个回答

56
根据官方文档,可以使用标记语言通过数字id提及用户:

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


12
仅当用户过去已经联系过机器人、通过内联按钮发送了回调查询或是被在提及的群组中的成员时,这些提及才能保证起作用。 - Yuriy Litvin
2
这种方法的另一个问题是,即使您设置了“disable_notification”,它也会通知用户。 - David Rodrigues
3
如果用户在隐私设置中禁用了“转发的消息”,则该功能将无法正常工作。 - Mwthreex

28
根据这个链接: 可以使用标记来提及用户的数字ID:

Markdown样式

要使用此模式,请在使用sendMessage时将Markdown作为parse_mode字段传递。 在您的消息中使用以下语法:

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

您也可以使用 HTML 样式:

HTML 样式

要使用此模式,请在使用 sendMessage 时将 HTML 传递给 parse_mode 字段。当前支持以下标记:

<a href="tg://user?id=123456789">inline mention of a user</a>

6

试试这个:

@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")

2

对于python-telegram-bot,您可以执行以下操作:

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")

1
您需要链接到文本:“tg://user?id=”与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")

这里有一个例子:

@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")

1
不,这个限制与Telegram的隐私政策和防止滥用有关。
在发送消息时(BOT API),可以提及用户,但这不是您需要的: [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


-3

机器人可以通过其ID标记用户,但它们不能使用官方的HTTP Bot API来实现此功能。

更新:由于Telegram已经原生支持此功能,因此不再需要。

如果您使用MadelineProto(PHP)登录机器人帐户,则可以使用此“链接”将某人按其ID提及,并将parse_mode设置为markdown

[Daniil Gentili](mention:@danogentili)


2
OP 想知道如何通过用户 ID 提及用户,它是一个 32 位整数,而不是您建议的用户名。 - reith
1
@Reith 请查看MadelineProto文档。你可以将@用户名替换为数字。例如:[Daniil Gentili](mention:123456789) - Jonas Fowl
1
MadelineProto使用MTProto,而不是Telegram Bot API。 - Dima Karaush

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接