我在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()
不过,我只知道机器人的信息。我无法检索有关使用handler的用户的信息。
输出
{'first_name': 'SPIOTSYSTEMS', 'id': 581614234, 'username': 'spiotsystems_bot', 'is_bot': True, 'last_name': None, 'language_code': None}
如何获取使用info
命令的用户id或姓名?
我应该用哪种方法?请告知。
注意:
我的机器人和电报组有联系。出于安全原因,我已经从MVC中删除了Telegram令牌。
您使用的
getMe()
函数用于获取有关bot的信息。但你需要发信息的人的名字。在messageAPI中,有一个
from
属性(python中的from_user
),它包含一个User对象,该对象包含发送消息的人的详细信息。所以你会有更多的运气,像
name = message.from_user.first_name
。相关问题 更多 >
编程相关推荐