Search code examples
pythontelegramtelegram-botpython-telegram-bot

How To Obtain Username, First Name Or Last Name Of A Telegram User With Python-Telegram-Bot?


I'm Creating A Telegram Bot Using Python-Telegram-Bot

I Know That update.message.chat_id Returns The User's Chat ID, But I Need To Know How To Get User's Username Or First Name And/Or Last Name.

I've Found This On Telegram's Documentation But I Don't Know How To Use It ( I Tried bot.getChat(update.message.chat_id) But No Result)


Solution

  • All infomation about user (as username, id, first/last name and profile photos) available from telegram.User object. You can easily get it using telegram.Message

    user = update.message.from_user
    print('You talk with user {} and his user ID: {} '.format(user['username'], user['id']))