gpt4 book ai didi

python - 如何在 Telegram Bot 中获取用户名?

转载 作者:行者123 更新时间:2023-11-28 20:59:02 25 4
gpt4 key购买 nike

我正在使用处理程序在 Telegram 机器人中工作。一旦他们使用命令,我需要在其中获取用户名或用户 ID。

我的代码

import telebot  #Telegram Bot API

bot = telebot.TeleBot(<BotToken>)

@bot.message_handler(commands=['info'])
def send_welcome(message):
name = bot.get_me()
print(name)
bot.reply_to(message, "Welcome")

bot.polling()

但是,我只得到有关机器人的信息。我无法检索有关使用处理程序的用户的信息。

输出

{'first_name': 'SPIOTSYSTEMS', 'id': 581614234, 'username': 'spiotsystems_bot', 'is_bot': True, 'last_name': None, 'language_code': None}

如何获取使用 info 命令的人的用户 ID 或姓名?我应该使用哪种方法?请指教。

注意:

我的机器人与 Telegram 组相关联。出于安全原因,我已经从我的 MVC 中删除了 Telegram TOKEN。

最佳答案

您正在使用的 getMe() 函数用于获取有关机器人的信息。但是您想要发送消息的人的姓名。

message API,有一个 from 属性(python 中的 from_user),它包含一个 User对象,其中包含发送消息的人的详细信息。

所以你会更幸运,比如 name = message.from_user.first_name

关于python - 如何在 Telegram Bot 中获取用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50521031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com