0

大家早上好,我尝试编写一个用于电报的机器人。我需要从用户名中获取用户 [对象]。可以办到?我试图用谷歌搜索它,但我没有找到任何东西,而且我读到没有办法做到这一点。

代码:

https://i.imgur.com/ptDNTGp.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);
});
4

1 回答 1

0

无法使用用户名获取 Telegram 组中的用户信息。检查getChatMember 方法,您可以看到它使用user_id. 不同语言的 API 只是这些方法的接口。

现在您有 2 个解决方案:

  1. 通过收听群组消息来存储和更新用户信息(当用户发送消息时,在数据库中存储或更新他们的信息user_idusername。然后通过数据库将用户名与 id 匹配,并使用它来获取用户信息。
  2. 使用 MTPROTO API 遍历组的所有成员并检查他们的用户名是否是您想要的。
于 2021-05-25T16:59:22.573 回答