如何在电报bot中用用户名来称呼用户?

时间:2019-12-16 10:03:10

标签: python-3.x telegram python-telegram-bot

我正在使用pyTelegramBotApi(远程机器人)

我需要的是在用户按下/ start后欢迎使用其名字的用户

@bot.message_handler(commands=['start'])
def start_command(message):
    bot.send_message(
        message.chat.id, 'Welcome!')

这只是发送消息Welcome!,但是我需要它看起来像Welcome, %first_name%!

1 个答案:

答案 0 :(得分:0)

message对象包含有关用户的许多信息; 要获取名字,请使用; message.from_user.first_name

import telebot

bot = telebot.TeleBot("TOKEN")

@bot.message_handler(commands=['start'])
def send_welcome(message):

    bot.reply_to(message, "Hi " + message.from_user.first_name + "!")

bot.polling()

如果您对所有选项感到好奇,最简单的方法是print()对象;

def send_welcome(message):
    print(message.from_user)