如何在Telegram Bot中获得用户名?

Sun*_*jan 2 python python-3.x telegram python-telegram-bot telegram-bot

我正在使用处理程序在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()
Run Code Online (Sandbox Code Playgroud)

但是,我仅获得有关该机器人的信息。我无法获取有关使用处理程序的用户的信息。

输出值

{'first_name': 'SPIOTSYSTEMS', 'id': 581614234, 'username': 'spiotsystems_bot', 'is_bot': True, 'last_name': None, 'language_code': None}
Run Code Online (Sandbox Code Playgroud)

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

注意:

我的机器人与Telegram组链接。并且出于安全原因,我已从我的MVC中删除了Telegram TOKEN。

Sha*_*dow 6

getMe()您正在使用的功能用于获取有关机器人的信息。但是您需要发送消息的人的名字。

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

因此,类似的东西会给您带来更多的运气name = message.from_user.first_name


Mic*_*l K 6

try this:

message.from_user.id
message.from_user.first_name
message.from_user.last_name
message.from_user.username
Run Code Online (Sandbox Code Playgroud)

or read this https://core.telegram.org/bots/api#user