如何让 Telegram Bot 从提及中获取用户信息

How to make Telegram Bot get User info from mention

我刚刚开始研究 Telegram Bot API 以了解如何通过 Java 对 Bot 进行编程。 我不明白机器人如何识别通过 @<username> 提到的用户并获取有关他的信息。

例如,假设我希望我的机器人能够计算并存储某个用户在群组中被提及的次数。他需要的是被标记用户的id.

我可以使用 update.getMessage().getEntities() 解析传入消息以检测实体,然后使用 MessageEntity.getType() 获取实体类型。现在,如果我理解的话,有两种类型的提及:mention 表示通过 @<username> 进行的提及,text_mention 表示对没有用户名的用户的提及。 问题来了,因为我希望机器人能够检索有关具有用户名的用户的更多信息,因为它是可编辑但唯一的,而不是关于没有用户名的用户,因为他 'tagged' 使用他的名字保存在发件人的联系人列表中。 相反,只有 text_mention 类型 MessageEntity 对象包含 User 对象,我可以从中获取用户的 id,正如我在 Telegram Bot API documentation 中读到的那样。从 mention 类型 MessageEntity 对象中,我只能获取用户名本身,因为它是文本,但这还不够,因为它可以更改。

所以我的第一个问题是:为什么?我真的不明白为什么 text_mention 应该携带有关标记用户的信息而 mention 不应该。

那么主要问题是:我该如何解决这个问题?

目前没有正确的方法。

对于此类功能,您可以使用 TdLib. Here are Java examples

如果用户在组中,在提到他们的地方,您可以使用此类方法获取用户列表并按用户名查找:

//@description  Return recently joined users in reverse chronological order 
channelParticipantsRecent = ChannelParticipantsFilter;

//@description  Return privileged participants  
channelParticipantsAdmins = ChannelParticipantsFilter;

//@description  Return kicked from the channel  
channelParticipantsKicked = ChannelParticipantsFilter;

//@description  Return bots in the channel  
channelParticipantsBots = ChannelParticipantsFilter;