Search code examples
telegramtelegram-bottelegram-api

How to send a Telegram message to a username using bot api or telegram api?


Hi I am trying to build a website login system where users are able to log in using their telegram. I would like to achieve this workflow:

  1. User selects to login/sign up via telegram.
  2. User enters their username (@ExampleJoe).
  3. A 6-digit verification code is sent to them via my Telegram bot (@ExampleBot).
  4. The user enters the received 6-digit code into my website and is allowed to enter.

I have issues with steps 2-3 where my bot can send them a direct message (not in a group chat) with only their username. I am aware that sendMessage method in the Telegram bot API requires a chat_id and that a user_id is also suitable. However, I do not know how to get the user_id with just the user's username.


Solution

  • That's not possible with the Bot API. You'd have to use a userbot for that. Note however, that TG provides a native login widget that you can use instead.