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.sstatic.net/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);
});
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:
user_id
& username
in a database). Then match a username to an id through the database and use that to get user info.