user2120413
user2120413

Reputation: 9

Not able to get user name in telegram bot

The name comes out as none. Can someone please help

def funtion_example(update, context):
    #name of the bot user
    chat_user_client = update.message.from_user.username
    update.message.reply_text(str(chat_user_client))

Upvotes: 0

Views: 3025

Answers (3)

Maks
Maks

Reputation: 1639

The answer is that some users don't have their username set in their Telegram app, thus it is omitted

Upvotes: 0

Danilo Cacace
Danilo Cacace

Reputation: 522

if you are retriving message from channel you can try sender_chat, in this you will have username of channels and real users, instead in the from_user you will find username of real users and bot. So you need to choose which one use depending of the use-case.

def funtion_example(update, context):
    #name of the bot user
    chat_user_client = update.message.sender_chat.username
    update.message.reply_text(str(chat_user_client))

Upvotes: 0

Chillie
Chillie

Reputation: 1485

If you look at the bot api docs you will see that only first_name, id and is_bot are not marked as Optional.

Thus a number of users will not have a username, which you have to take into account and work around.

Upvotes: 2

Related Questions