大家早上好,我试着为电报用机器人编码。我需要从用户名中获取一个userobject。能办到吗?我试着在谷歌上搜索,但是我什么也没找到,而且我还读到没有办法去做。
代码:
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);
});
发布于 2021-05-25 16:59:22
不可能使用用户的用户名在电报组中获取用户信息。检查getChatMember
方法,您可以看到它使用user_id
工作。不同语言中的API只是这些方法的接口。
现在您有了两个解决方案:
user_id
和username
)。然后通过数据库将用户名与id匹配起来,并使用它获取用户信息。https://stackoverflow.com/questions/67685044
复制相似问题