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

4

14

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)

Nubianubian answered 6/12, 2016 at 2:16 Comment(0)
M
33

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']))
Mease answered 9/12, 2016 at 7:0 Comment(1)
I want to add one thing. Sometimes, if the bot flow allows user to navigate by pressing some InlineButtons, user might not send a message. So to understand user info, update.callback_query.from_user method can be used too. According to the nature of the interaction, a different method might be needed.Putup
P
12

You can use the json file and access information such as username, ID, etc. You can print this file yourself for information about the json file. See the example below.

# pip install python-telegram-bot==12.0.0
from telegram.ext import Updater
from telegram.ext import CommandHandler
updater = Updater('token')

def start(bot, update):
    # print('json file update : ' ,update)
    # print("json file bot : ', bot)
    chat_id = update.message.chat_id
    first_name = update.message.chat.first_name
    last_name = update.message.chat.last_name
    username = update.message.chat.username
    print("chat_id : {} and firstname : {} lastname : {}  username {}". format(chat_id, first_name, last_name , username))
    bot.sendMessage(chat_id, 'text')

start_command = CommandHandler('start',start)
updater.dispatcher.add_handler(start_command)
updater.start_polling()
updater.idle()
Piwowar answered 5/6, 2020 at 12:33 Comment(0)
H
1

Check your python version on your terminal or cmd by typing==> python --version If you are using Pycharm as your IDE then, do the following:

  1. Install Telegram API using this command on your terminal ==>pip3 install pyTelegramBotAPI
  2. Afterwards, you can import the telebot
  3. Then run the following commands below on your terminal
  4. Install pip install telegram via terminal
  5. Install pip install python-telegram-bot via terminal
  6. Now import from telegram.ext import *

Then use the following code below to get the user information from the chat:

import telebot

from telegram.ext import *

from creds import API_KEY

shopflipper = telebot.TeleBot(API_KEY, parse_mode=None)

@shopflipper.message_handler(commands=["help", "hello"])
def send_help_message(msg):
        #print(msg) --> this prints out all the information related to the chat
        print(msg.from_user.username)
        username = str(msg.from_user.username)
        #print(username)
       
        shopflipper.reply_to(msg, "Hello")


shopflipper.polling()
Huehuebner answered 8/1, 2023 at 21:30 Comment(0)
C
1

update.effective_user.id

This worked for me in any case, whether it's a callback from inlines or simple MessageHandler, the user id is same.

Control answered 2/9, 2023 at 17:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.