0

我正在使用 pyTelegramBotApi (telebot)

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

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

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

4

1 回答 1

0

message对象包含有关用户的许多信息;获取 first_name 使用;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)
于 2019-12-16T11:00:33.430 回答