There are 3 conditions:
1-@user
is not subscribed to your bot before(not initiated chat with your bot before or it does block your bot after some chat with your bot: In this condition you can NEVER send any type of message to that @user
.
2-@user
doesn't exist or is incorrect . As you know, a telegram user subscribe with phone and first name (at least) and he/she could not have any @user
id: In this condition there is no user name to send.
3-@user
exists and already is your bot subscriber. In this condition you should save all subscribers chat_id
and their related user_name
as they sends some messages to your bot in a database because these data are part of each message you receive from users. So for send to that @user
you should find related chat_id
from your DB.(Non of Telegram Bot API
methods use @username
but they use chat_id
)
This is a sample JSON response from Telegram when user send /1
command, as you see telegram always attaches sender chat_id
,firs_name
,last_name
and username
(if exists) to each message that bot receives:

UPDATED:
As users may change they @username
during the time, it is better to
update your chat_id
<-->@username
database with each message you
receive.