0

I want to read user names from receiving updates however but I face an special issue.

For some users, $message['from']['username'] contains a Telegram User Name however for some users this parameter is not defined.

How can I reliably receive user names ( get username field), using Telegram Bot API?

VSB
  • 9,825
  • 16
  • 72
  • 145

2 Answers2

8

It's not mandatory to have a username, so there are users with no username.

Maak
  • 4,720
  • 3
  • 28
  • 39
1

Simply use this code:

if(!$username){
$parameters = array("chat_id" => $chatId, "text" => "Set a username to use this bot!\nSettings > Username");
}

When a user have no username it can't use the bot.

Pato05
  • 326
  • 1
  • 14