tdlight-java/src/main/java/it/tdlight/client/AuthorizationStateWaitAuthenticationDataHandler.java

60 lines
2.3 KiB
Java
Raw Normal View History

2021-09-27 19:27:13 +02:00
package it.tdlight.client;
2021-10-22 12:54:28 +02:00
import it.tdlight.common.ExceptionHandler;
import it.tdlight.common.TelegramClient;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.PhoneNumberAuthenticationSettings;
import it.tdlight.jni.TdApi.SetAuthenticationPhoneNumber;
import it.tdlight.jni.TdApi.UpdateAuthorizationState;
2021-09-27 19:27:13 +02:00
final class AuthorizationStateWaitAuthenticationDataHandler implements GenericUpdateHandler<UpdateAuthorizationState> {
private final TelegramClient client;
private final Authenticable authenticable;
private final ExceptionHandler exceptionHandler;
public AuthorizationStateWaitAuthenticationDataHandler(TelegramClient client,
Authenticable authenticable,
ExceptionHandler exceptionHandler) {
this.client = client;
this.authenticable = authenticable;
this.exceptionHandler = exceptionHandler;
}
@Override
public void onUpdate(UpdateAuthorizationState update) {
if (update.authorizationState.getConstructor() == TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR) {
2021-10-24 01:03:16 +02:00
authenticable.getAuthenticationData(this::onAuthData);
}
}
2021-09-27 19:27:13 +02:00
2021-10-24 01:03:16 +02:00
public void onAuthData(AuthenticationData authenticationData) {
if (authenticationData.isBot()) {
String botToken = authenticationData.getBotToken();
TdApi.CheckAuthenticationBotToken response = new TdApi.CheckAuthenticationBotToken(botToken);
client.send(response, ok -> {
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {
throw new TelegramError((TdApi.Error) ok);
}
}, exceptionHandler);
} else if (authenticationData.isQrCode()) {
TdApi.RequestQrCodeAuthentication response = new TdApi.RequestQrCodeAuthentication();
client.send(response, ok -> {
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {
throw new TelegramError((TdApi.Error) ok);
}
}, exceptionHandler);
} else {
PhoneNumberAuthenticationSettings phoneSettings = new PhoneNumberAuthenticationSettings(false, false, false, false, null);
2021-10-24 01:03:16 +02:00
String phoneNumber = String.valueOf(authenticationData.getUserPhoneNumber());
SetAuthenticationPhoneNumber response = new SetAuthenticationPhoneNumber(phoneNumber, phoneSettings);
client.send(response, ok -> {
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {
throw new TelegramError((TdApi.Error) ok);
}
}, exceptionHandler);
2021-09-27 19:27:13 +02:00
}
}
}