如何在Java中通过Telegram机器人仅使用user_id获取任何用户的信息?

huangapple go评论61阅读模式
英文:

How to get information about any user having only user_id in telegram bot in Java?

问题

I use Java and telegrambots library. Using the keyboardButtonRequestUser method, I can get the user_id of an outside user, but how to collect all the information about an outside user with only the user_id?

    public void onUpdateReceived(Update update) {

        if (update.hasMessage() && update.getMessage().hasText()) {

            String massageText = update.getMessage().getText();
            long chatId = update.getMessage().getChatId();
            User user = update.getMessage().getFrom();

            if (massageText.equals("/start")) {
                System.out.println("OutsideUserId = 6165875618 --> ...????");

                startCommandReceived(chatId, update.getMessage().getChat().getFirstName());
            } 
            else {
                sendMessage(chatId, "Sorry, command was not recognized");
            }

        } 
        else if (update.hasMessage()){
            long chatId = update.getMessage().getChatId();
            sendMessage(chatId, "UserShared --> "+ update.getMessage().getUserShared());
            sendMessage(chatId, "ChatShared --> "+ update.getMessage().getChatShared());
            sendMessage(chatId, "CurrentChatId --> "+chatId);
        }
    }

(Note: The code portion has been provided without translation as requested.)

英文:

I use Java and telegrambots library. Using the keyboardButtonRequestUser method, I can get the user_id of an outside user, but how to collect all the information about outside user with only the user_id?

    public void onUpdateReceived(Update update) {

        if (update.hasMessage() && update.getMessage().hasText()) {

            String massageText = update.getMessage().getText();
            long chatId = update.getMessage().getChatId();
            User user = update.getMessage().getFrom();

            if (massageText.equals("/start")) {
// UserShared --> UserShared(requestId=732633638, userId=6165875618)
                System.out.println("OutsideUserId = 6165875618 --> " + "...????");

                startCommandReceived(chatId, update.getMessage().getChat().getFirstName());
            } 
            else {
                sendMessage(chatId, "Sorry, command was not recognized");
            }

        } 
        else if (update.hasMessage()){
            long chatId = update.getMessage().getChatId();
            sendMessage(chatId, "UserShared --> "+ update.getMessage().getUserShared());
            sendMessage(chatId, "ChatShared --> "+ update.getMessage().getChatShared());
            sendMessage(chatId, "CurrentChatId --> "+chatId);
        }
    }

答案1

得分: 1

我不是100%确定,但我认为这是不可能的,因为我们只能在用户进入机器人时请求用户数据。有人对此持不同意见吗?

英文:

I'm not 100% sure, but in my opinion this is impossible because we can only request user data when he enters the robot. Anybody have a different opinion on this?

huangapple
  • 本文由 发表于 2023年4月4日 05:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定