[英]How to get the user's name in Telegram Bot?
我正在使用處理程序在 Telegram bot 中工作。 一旦他們使用命令,我需要獲取用戶名或用戶 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。
試試這個:
message.from_user.id
message.from_user.first_name
message.from_user.last_name
message.from_user.username
@bot.message_handler(commands=['Start'])
def greet(message):
user_first_name = str(message.chat.first_name)
bot.reply_to(message, f"Hey! {user_first_name} \n Welcome To The...")```
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.