Skip to content

modify the way to enter the login verification code #220

Closed

Description

@exrtey

I would like to ask how to modify the way to enter the login verification code, I want to use a bot to let the program receive the verification code and log in to a user account, instead of entering it in the console?

Activity

  1. SachukS commented on Jul 14, 2024

    @SachukS

    I think you need to write your own ClientInteraction class, and then assign it to your client like in this code below:

    public static void loginClient(Partner partner) {
            LOG.error("Telegram login");
            APIToken apiToken = new APIToken(111111, "token");
            TDLibSettings settings = TDLibSettings.create(apiToken);
    
            Path sessionPath = Paths.get("tdlight-session-"+partner.getPhone());
            settings.setDatabaseDirectoryPath(sessionPath.resolve("data"));
            settings.setDownloadedFilesDirectoryPath(sessionPath.resolve("downloads"));
    
            SimpleTelegramClientBuilder clientBuilder = simpleTelegramClientFactory.builder(settings);
    
            SimpleAuthenticationSupplier<?> authenticationData = AuthenticationSupplier.qrCode();
    
            clientBuilder.addUpdateHandler(TdApi.UpdateAuthorizationState.class, Telegram::onUpdateAuthorizationState);
    
            clientBuilder.addUpdateHandler(TdApi.UpdateChatReadOutbox.class, Telegram::onUpdateChatReadOutbox);
    
            clientBuilder.addUpdateHandler(TdApi.UpdateMessageSendSucceeded.class, Telegram::onUpdateMessageSendSucceeded);
    
            clientBuilder.addUpdateHandler(TdApi.UpdateMessageSendFailed.class, Telegram::onUpdateMessageSendFailed);
    
            clientBuilder.addUpdateHandler(TdApi.UpdateNewMessage.class, Telegram::onUpdateNewMessage);
    
            client = clientBuilder.build(authenticationData);
            clientInteraction = new SpringClientInteraction(blockingExecutor, client);
            client.setClientInteraction(clientInteraction);
        }
    
  2. SachukS commented on Jul 14, 2024

    @SachukS

    this is my custom class

    public class SpringClientInteraction implements ClientInteraction {
        private final ExecutorService blockingExecutor;
        private final Authenticable authenticable;
    
        public static String qr = "";
    
        public SpringClientInteraction(ExecutorService blockingExecutor, Authenticable authenticable) {
            this.blockingExecutor = blockingExecutor;
            this.authenticable = authenticable;
        }
    
        @Override
        public CompletableFuture<String> onParameterRequest(InputParameter parameter, ParameterInfo parameterInfo) {
            AuthenticationSupplier<?> authSupplier = this.authenticable.getAuthenticationSupplier();
            AuthenticationData authData = this.getAuthDataNowOrNull(authSupplier);
            return CompletableFuture.supplyAsync(() -> {
                boolean useRealWho = authData != null;
                String who;
                if (!useRealWho) {
                    who = "login";
                } else if (authData.isQrCode()) {
                    who = "QR login";
                } else if (authData.isBot()) {
                    who = authData.getBotToken().split(":", 2)[0];
                } else {
                    who = "+" + authData.getUserPhoneNumber();
                }
    
                boolean trim = false;
                String question;
                switch (parameter) {
                    case ASK_FIRST_NAME:
                        question = "Enter first name";
                        trim = true;
                        break;
                    case ASK_LAST_NAME:
                        question = "Enter last name";
                        trim = true;
                        break;
                    case ASK_CODE:
                        question = "Enter authentication code";
                        ParameterInfoCode codeInfo = (ParameterInfoCode)parameterInfo;
                        question = question + "\n\tPhone number: " + codeInfo.getPhoneNumber();
                        question = question + "\n\tTimeout: " + codeInfo.getTimeout() + " seconds";
                        question = question + "\n\tCode type: " + codeInfo.getType().getClass().getSimpleName().replace("AuthenticationCodeType", "");
                        if (codeInfo.getNextType() != null) {
                            question = question + "\n\tNext code type: " + codeInfo.getNextType().getClass().getSimpleName().replace("AuthenticationCodeType", "");
                        }
    
                        trim = true;
                        break;
                    case ASK_PASSWORD:
                        question = "Enter your password";
                        String passwordMessage = "Password authorization:";
                        String hint = ((ParameterInfoPasswordHint)parameterInfo).getHint();
                        if (hint != null && !hint.isEmpty()) {
                            passwordMessage = passwordMessage + "\n\tHint: " + hint;
                        }
    
                        boolean hasRecoveryEmailAddress = ((ParameterInfoPasswordHint)parameterInfo).hasRecoveryEmailAddress();
                        passwordMessage = passwordMessage + "\n\tHas recovery email: " + hasRecoveryEmailAddress;
                        String recoveryEmailAddressPattern = ((ParameterInfoPasswordHint)parameterInfo).getRecoveryEmailAddressPattern();
                        if (recoveryEmailAddressPattern != null && !recoveryEmailAddressPattern.isEmpty()) {
                            passwordMessage = passwordMessage + "\n\tRecovery email address pattern: " + recoveryEmailAddressPattern;
                        }
    
                        System.out.println(passwordMessage);
                        break;
                    case NOTIFY_LINK:
                        String link = ((ParameterInfoNotifyLink)parameterInfo).getLink();
    
                        qr = link;
    
                        return "";
                    case TERMS_OF_SERVICE:
                        TdApi.TermsOfService tos = ((ParameterInfoTermsOfService)parameterInfo).getTermsOfService();
                        question = "Terms of service:\n\t" + tos.text.text;
                        if (tos.minUserAge > 0) {
                            question = question + "\n\tMinimum user age: " + tos.minUserAge;
                        }
    
                        if (!tos.showPopup) {
                            System.out.println(question);
                            return "";
                        }
    
                        question = question + "\nPlease press enter.";
                        trim = true;
                        break;
                    default:
                        question = parameter.toString();
                }
    
                String result = ScannerUtils.askParameter(who, question);
                return trim ? result.trim() : (String) Objects.requireNonNull(result);
            }, this.blockingExecutor);
        }
    
        private AuthenticationData getAuthDataNowOrNull(AuthenticationSupplier<?> authSupplier) {
            try {
                return (AuthenticationData)authSupplier.get().getNow(null);
            } catch (Throwable var3) {
                return null;
            }
        }
    }
    
  3. exrtey commented on Jul 14, 2024

    @exrtey
    Author

    Screenshot_2024_0714_222617
    Thank you. I think it should be modified here to receive the verification code through the robot, right? But I thought for a moment, I can only prompt the user to enter the verification code here, but I can't get the return value of the information input by the user. Is there any other way?

  4. SachukS commented on Jul 14, 2024

    @SachukS

    Yes, you need to implement here logic that send "question" with robot and then to make the thread to wait for the response (maybe looping it with GET requests, idk)

  5. SachukS commented on Jul 20, 2024

    @SachukS

    Screenshot_2024_0714_222617 Thank you. I think it should be modified here to receive the verification code through the robot, right? But I thought for a moment, I can only prompt the user to enter the verification code here, but I can't get the return value of the information input by the user. Is there any other way?

    is there any results?

  6. exrtey commented on Jul 20, 2024

    @exrtey
    Author

    I already know what to do. Thank you for your reply

  7. tLikWer commented on Aug 24, 2024

    @tLikWer

    I already know what to do. Thank you for your reply

    can u help me, i dont understand the solution, may be u can give me a project with this solution

  8. locked and limited conversation to collaborators on Aug 28, 2024
  9. converted this issue into a discussion #227 on Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions