Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the user's name in Telegram Bot?

I'm working in Telegram bot with a handler. where I need to get the user's name or users id once they use the command.

MY CODE

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()

However, I get only info about the bot. I can't retrieve info about the user who used handler.

OUTPUT

{'first_name': 'SPIOTSYSTEMS', 'id': 581614234, 'username': 'spiotsystems_bot', 'is_bot': True, 'last_name': None, 'language_code': None}

How do I get the user id or name of the person who uses info command? which method i shall use? please advise.

NOTE:

My bot is linked with the Telegram group. and I've removed the Telegram TOKEN from my MVC for security reasons.

like image 866
Sundararajan Avatar asked May 25 '18 03:05

Sundararajan


People also ask

How can I get username in Telegram bot?

Head to WP Admin → CM Telegram Bot → Settings → API Settings tab. Fill the Bot Username field.

How can I get Telegram user details?

Locate "Chat." It's about halfway down the information page. This section displays your Chat ID, First name, Last Name, and your Username. Note the number next to "ID." The number next to "ID" below "Chat" is your personal Chat ID.

What is Telegram bot name?

To create a chatbot on Telegram, you need to contact the BotFather, which is essentially a bot used to create other bots. Your bot should have two attributes: a name and a username. The name will show up for your bot, while the username will be used for mentions and sharing.


1 Answers

try this:

message.from_user.id
message.from_user.first_name
message.from_user.last_name
message.from_user.username

or read this https://core.telegram.org/bots/api#user

like image 75
Michael K Avatar answered Sep 30 '22 04:09

Michael K