当前位置: 首页 > 知识库问答 >
问题:

使用用户ID(telegramBot)在电报中提及人

有骏祥
2023-03-14

我试图让我的电报机器人提及用户的电报谁没有用户名,并希望提及他们使用用户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)

根据链接:可以通过带有标记的数字id提及用户:Markdown样式

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

内联提及用户

我无法将其应用到我的代码中并使其工作。有人能帮我吗?该功能已在此处成功应用

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

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

共有2个答案

郭彬郁
2023-03-14

您应该指定parse\u mode参数

context.bot.send_message(
    chat_id=chat_id,
    text=text,
    parse_mode='markdown',
)

顺便说一句,当您使用内联链接时,没有必要写“@”。

此外,我建议您使用电报文本模块来编写指向用户的链接:

from telegram_text import InlineUser, User


def welcome(update, context, new_member):
    ...
    if new_member.username is None:
        user = InlineUser(new_member.first_name, new_member.id)
    else:
        user = User(new_member.username)

    text = (
        f"Hello {user}! Welcome to the {message.chat.title} "
        "telegram group!\n"
        "Please introduce yourself."
    )
谯嘉木
2023-03-14

您必须指定解析模式=电报。解析模式。标记或send\u message方法中的parse\u 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\u V2。

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

提高代码质量和简化代码的提示(我认为您正在使用python Telegrame bot):

  • 像这样导入库。它使您可以更容易地从电报导入编码:
    *
    。外部导入*

 类似资料:
  • 问题内容: 我正在尝试使用discord.py编写一个简单的机器人,所以我从有趣的命令开始,例如只是为了获得api的优势。 问题答案: 因此,我经过几天的反复试验终于弄清楚了如何做到这一点,希望其他人可以从中受益,并且比我实际承受的痛苦更少。解决方案最终很容易。

  • 我想提及的用户,甚至是那些没有用户名的用户。 如果用户设置了用户名,我会返回 作为一个答案,但如果用户没有,我不能这样做。 我尝试使用唯一的用户ID,例如。 但这并不奏效。 我怎样才能做到这一点?

  • 这可能是一个简单的问题,但我无法解决。 我使用Slack Python Api在频道中提到用户,我在这里指的是文档,https://api.slack.com/methods/chat.postMessage,我的代码很简单, 这将向频道发送消息,但我找不到任何提及用户的选项。我试图在信息中加入,例如 该消息将被发布,但以纯文本形式发布,而不是真正提及某人。顺便说一句,我正在使用测试令牌。(或者也

  • 我一直在使用电报机器人为一个组和用户发送通知。 我已经知道我可以通过使用getUpdates在我的机器人上接收用户的消息来获取聊天ID。 我也知道我可以使用相同的方法获取Group ID... 但我真正需要的是:我的团队中有三个用户。 我的机器人 有可能得到第三个用户的ID吗?PS:我是群主,还将我的机器人添加为管理员... 第三个用户是普通用户。有人能帮我吗? 谢啦!

  • 嗨,我正在尝试建立一个网站登录系统,用户可以使用电报登录。我希望实现此工作流: < li >用户选择通过电报登录/注册。 < li >用户输入他们的用户名(@ExampleJoe)。 < li >通过我的电报机器人(@ExampleBot)向他们发送6位验证码。 < li >用户将收到的6位数字代码输入到我的网站,并被允许进入。 我在步骤 2-3 中遇到了问题,我的机器人可以仅使用其用户名向他们发

  • 我在我的系统中使用谷歌广告API PHP库。但是我没有在这里提供的API列表中找到以下2个APIhttps://developers.google.com/google-ads/api/docs/account-management/create-account: API,以查明用户是否已经存在使用电子邮件的谷歌广告帐户?如果找到,则返回其10位数的客户ID。 我在几个网站上见过同样的过程。每当用