首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从用户名中获取用户?

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

Stack Overflow用户
提问于 2021-05-25 09:15:44
回答 1查看 721关注 0票数 0

大家早上好,我试着为电报用机器人编码。我需要从用户名中获取一个userobject。能办到吗?我试着在谷歌上搜索,但是我什么也没找到,而且我还读到没有办法去做。

代码:

https://i.imgur.com/ptDNTGp.png

代码语言:javascript
运行
复制
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);
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-25 16:59:22

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

现在您有了两个解决方案:

  1. 通过侦听组消息来存储和更新用户信息(当用户发送消息时,在数据库中存储或更新他们的信息,如user_idusername )。然后通过数据库将用户名与id匹配起来,并使用它获取用户信息。
  2. 使用MTPROTO迭代组的所有成员,并检查它们的用户名是否符合您的要求。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67685044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档