【问题标题】:Mention people on telegram using userid (telegramBot)使用用户 ID 在电报上提及人(电报机器人)
【发布时间】:2021-10-22 04:57:52
【问题描述】:

我正在尝试让我的电报机器人在电报上提及没有用户名并希望使用用户 ID 提及他们的用户。 下面是我的python代码..它不会标记那些没有用户名的人。

def welcome(update, context, new_member):
    # Greets a person who joins the chat
    message = update.message
    chat_id = message.chat.id
    if new_member.username is None:
        username = "["+new_member.first_name+"](tg://user?id="+str(new_member.id)+")"
    else:
        username = new_member.username
    logger.info(
        "%s joined to chat %d (%s)",
        escape(username),
        chat_id,
        escape(message.chat.title),
    )

    text = (
        f"Hello @{username}! Welcome to the {message.chat.title} "
        "telegram group!\n"
        "Please introduce yourself."
    )

    context.bot.send_message(chat_id=chat_id, text=text)

根据link:可以通过带有标记的数字ID来提及用户: 降价风格

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

内联提及用户

我无法在我的代码中应用它并让它工作。有人可以帮我吗? 此处已成功应用该功能

https://github.com/domdorn/telegram/commit/ea308cadb739a9a018c7fbabc16824e2ff82d415

我想在我的代码中应用相同的内容,以便它会提及没有用户名和 ID 的用户。

【问题讨论】:

    标签: python telegram python-telegram-bot


    【解决方案1】:

    您必须在send_message 方法中指定parse_mode=telegram.ParseMode.MARKDOWN 或parse_mode='markdown'。 您编辑的代码应如下所示:

    def welcome(update, context, new_member):
        # Greets a person who joins the chat
        message = update.message
        chat_id = message.chat.id
        if new_member.username is None:
            username = "["+new_member.first_name+"](tg://user?id="+str(new_member.id)+")"
        else:
            username = new_member.username
        logger.info(
            "%s joined to chat %d (%s)",
            escape(username),
            chat_id,
            escape(message.chat.title),
        )
    
        text = (
            f"Hello @{username}! Welcome to the {message.chat.title} "
            "telegram group!\n"
            "Please introduce yourself."
        )
    
        context.bot.send_message(
            chat_id=chat_id,
            text=text,
            parse_mode=telegram.ParseMode.MARKDOWN,
        )
    

    您也可以使用HTML 或MARKDOWN_V2。

    文档和完整信息的链接:
    https://python-telegram-bot.readthedocs.io/en/stable/telegram.parsemode.html#telegram.ParseMode https://core.telegram.org/bots/api#formatting-options

    使您的代码更好并帮助您更轻松地编写代码的提示(我认为您正在使用python-telegram-bot):

    • 像这样导入你的库。它使您更容易编码:
      from telegram import *
      from telegram.ext import *
    • (考虑到您已经完成了上一个技巧)如果您正在使用 VSCode 或类似代码进行编码,您可以将 def welcome(update, context, new_member) 替换为 def welcome(update: Update, context: CallbackContext, new_member: User)。因此,您的编辑器/IDE 将了解变量的类型并推荐/自动填充您的代码。
    • 而不是context.bot.send_message 使用update.message.reply_text,所以你可以省略chat_id=chat_id。示例:
      update.message.reply_text(text, parse_mode='markdown')

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 2015-07-21
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2017-06-09
      • 2018-07-22
      • 2020-10-28
      • 1970-01-01
      相关资源
      最近更新 更多