Telegram bots - how to find out user's contacts without username

312 views Asked by At

I'm developing a telegram bot via Telegraf.js One problem that I faced is - how to find out user contacts if the user doesn't have a username? Project customer wants bot to use some identifier to create a record in the database. Then admin will check all the orders opened and contact user. I know that chat_id is a way to identify, but when I'm trying to use it in a link, I get redirected to telegram website main page. For example link https://t.me/my_chat_id doesn't work. With username it does, by the way, but sometimes users dont have one.

Please tell me how can I solve my problem? Thanks

I tried to check ctx.from, but there is nothing I can use if user has no username, seems like.

1

There are 1 answers

0
Ron Vaknin On

Username is optional in Telegram, so you can't rely on that (considering the user will not expose a username). The other option for a link is phone number which is also optional

Seems like best case is keeping the chat ID as DB index and trying getting the username with fallback to getting the phone number while considering the worst case when there is no username and no phone number if that's possible, then the customer should decide on another method such as asking for email

If you want to take that extra mile though, there is an option to forward messages but I haven't tried anything like that so you might just end up going down a rabbit hole