Skip to content
Advertisement

Telegram bot How to get link to user profile from a message they send

I’m working with telegram bot and i have this method to get user messages. I need to find a way to get a link to the user who send this message I saw there are this method, but i dont know how to connect them to a “link” to a profile

   @Override
    public void onUpdateReceived(Update update) {
      update.getMessage().getFrom();
       // i have: getFirstName(), getLastName() - not helping
       // i have also getId() - but its a number how do i convert it to a link to telegram profile ?

}

Advertisement

Answer

Having username is optional on Telegram, setting it up means other people will be able to contact the user without knowing their phone number. Only users that chose their username in Telegram settings have a profile link, and the link is of the form https://t.me/<username>.

For ordinary messages, you can get the username via update.getMessage().getFrom().getUsername() and use it to create profile links, however, note that for many users this value will be empty (meaning they don’t have a profile link).

Also, if you keep database of chat IDs, you can get usernames of the users via getChat Telegram API call. This can be useful because while usernames can be changed by the user any time (meaning their profile link will also change), chat ID of the user is constant over time and the getChat method will always provide you with the most recent information.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement