How can i take user from username?

1.3k Views Asked by At

Good morning everyone, I try to coding a bot for telegram. I need to take a user[object] from a username. It can be done? I tried to google about it, but I didn't find anything and besides I read that there is no way to do it.

Code:

https://i.stack.imgur.com/K6wkq.png

bot.onText(/\/avvia/, (msg, match) => {   // the user must write /avvia @username
const chatId = msg.chat.id;
if(chatId != ChatGroup) { return; }   // Check if the command is executed in the right group [ChatGroup is a const with value chat.id]

if(match.input == '/avvia')           // Check if it's just typing the command
{
    bot.sendMessage(chatId, 'Only /avvia');
    return;
}

var aCaso = match.input.replace('/avvia ', '').split(' ');
const resp = aCaso[0];

bot.getChatMember(chatId, resp /* <= THIS ONE*/).then( response => { // Check if the forwarded user is in the group
    //console.log(response)                                          // But RESP is wrong because is 
    if(response.status == 'left' || response.status == 'kicked')     // getChatMember(string|int chatId, int userId), and not a username
    {
        bot.sendMessage(chatId, 'Not in the group');
        return
    }
})

//console.log(resp);
});
1

There are 1 best solutions below

0
On BEST ANSWER

It's not possible to get users information in a Telegram group using their usernames. Checking getChatMember method you can see that it works using user_id. APIs in different languages are just interfaces to these methods.

Now you have 2 solutions:

  1. Store & update user info by listening to group messages (When users send message, store or update their information like user_id & username in a database). Then match a username to an id through the database and use that to get user info.
  2. Use MTPROTO API to iterate through all members of the group and check if their username is what you want.