【问题标题】:Telegram bot: How to mention user by its id (not its username)Telegram bot:如何通过 id(而不是用户名)提及用户
【发布时间】:2017-02-24 04:45:13
【问题描述】:

我正在创建一个电报机器人并使用sendMessage 方法发送消息。 用@username提用户很容易,但是没有用户名怎么提用户呢?

如果使用电报应用/网页,我们可以通过@integer_id (name)提及用户,电报应用/网页会将其转换为可点击的文本。 integer_id会在我们选择用户时自动生成,输入@之后。

另一个背景: 我正在尝试使用 forceReply 并且我想定位用户,如果他们有用户名,我可以通过在 sendMessage 方法的文本中提及他们来轻松定位他们。

我正在创建的机器人是一个类似于机器人的“测验”。每个玩家需要轮流,并且机器人正在向他们发送问题,来自机器人的每个消息都会针对不同的玩家。

注意:我没有禁用Privacy Mode,我不希望电报用我不需要的味精轰炸我的服务器。它使我廉价的讨厌的服务器超载。所以,禁用它不是一个选项。

我愿意接受其他解决方案,让机器人可以收听选定的播放器。

谢谢。

21/10 更新: 他们说,我已经与 BotSupport 谈过电报,目前 Bots 不能提及没有用户名的用户。

所以在我的情况下,我仍然继续使用forceReply,并且还给没有用户名的用户提供了一个简短的消息来设置它,这样他们就可以从forceReply 功能中受益。

【问题讨论】:

    标签: telegram telegram-bot


    【解决方案1】:

    机器人能够通过用户 ID 标记用户,但他们无法使用官方 HTTP Bot API 做到这一点。

    更新:不再需要,因为 Telegram 添加了对此的原生支持。

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

    [Daniil Gentili](提及:@danogentili)

    【讨论】:

    • OP 想知道如何通过用户 ID 提及用户,这是一个 32 位整数,而不是您建议的用户名
    • @Reith 查看 MadelineProto 文档。您可以用数字替换@username。例如。 [Daniil Gentili](mention:123456789)
    • MadelineProto 使用 MTProto,而不是 Telegram Bot API
    【解决方案2】:

    根据official documentation,可以通过带有标记的数字ID来提及用户:

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

    【讨论】:

    • 这些提及只有在用户过去联系过机器人、通过内联按钮向机器人发送回调查询或者是提及他的组中的成员时才能保证有效。
    • 这个方法的另一个问题是它会通知用户,即使你设置了disable_notification
    • 如果用户从隐私设置中禁用“转发消息”,则无法正常工作
    【解决方案3】:

    试试这个:

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

    【讨论】:

      【解决方案4】:

      据此link : 可以通过带有标记的数字 id 来提及用户:

      Markdown 样式

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

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

      你也可以使用HTML风格:

      HTML 样式

      要使用此模式,请在使用 sendMessage 时在 parse_mode 字段中传递 HTML。目前支持以下标签:

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          不,此限制与 Telegram 的隐私政策和防止滥用有关。

          发送消息时可以提及用户(BOT API),但这不是您需要的:

          [inline mention of a user](tg://user?id=&lt;user_id&gt;)

          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

          【讨论】:

            猜你喜欢
            • 2016-10-05
            • 2017-07-01
            • 2018-12-15
            • 2015-01-24
            • 2019-02-14
            • 2017-04-20
            • 2021-02-07
            • 2018-11-04
            • 2017-04-15
            相关资源
            最近更新 更多