How to send private message to user on Telegram using his @username and not chat id

148 views Asked by At

I am making a cron job in PHP that will send some text message to users on Telegram at certain intervals.

I am using following to send message to user which is working fine. But the problem is this method only sends message to user via chat_id.

https://api.telegram.org/bot<MY_TOKEN>/sendMessage?text=ok&chat_id=12345678

When I try to use username then I get error:

https://api.telegram.org/bot<MY_TOKEN>/sendMessage?text=ok&chat_id=@username

Error: Bad Request: chat not found

Even if I use username and not @username it results in same issue. I know that user must send message to bot first before bot can send message to user. I have already done it that's why I am able to send message using chat_id. But how do I send message using @username?

Or may be there is a way to get chat_id of a user via telegram API? If that is the case then I can call that method first and then call sendMessage method. But I don't know any such API.

0

There are 0 answers