如何从用户名中获取用户?

问题描述

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

代码

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);
});

解决方法

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

现在您有 2 个解决方案:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...