Add test numbers
This commit is contained in:
parent
2a87d69088
commit
6a7ba6d412
5
.gitignore
vendored
5
.gitignore
vendored
@ -95,7 +95,4 @@ parent/.classpath
|
|||||||
tdlight-java/target-snapshot/
|
tdlight-java/target-snapshot/
|
||||||
|
|
||||||
tdlight-java-8/target-snapshot/
|
tdlight-java-8/target-snapshot/
|
||||||
/example-tdlight-session/data/db.sqlite
|
/example-tdlight-session/
|
||||||
/example-tdlight-session/data/db.sqlite-shm
|
|
||||||
/example-tdlight-session/data/db.sqlite-wal
|
|
||||||
/example-tdlight-session/data/td.binlog
|
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>it.tdlight</groupId>
|
<groupId>it.tdlight</groupId>
|
||||||
<artifactId>tdlight-java-bom</artifactId>
|
<artifactId>tdlight-java-bom</artifactId>
|
||||||
<version>3.0.0.0-SNAPSHOT</version>
|
<version>3.0.2+td.1.18.4</version>
|
||||||
<type>pom</type>
|
<type>pom</type>
|
||||||
<scope>import</scope>
|
<scope>import</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
@ -54,7 +54,7 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>it.tdlight</groupId>
|
<groupId>it.tdlight</groupId>
|
||||||
<artifactId>osx_amd64</artifactId>
|
<artifactId>macos_amd64</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
-->
|
-->
|
||||||
|
|
||||||
|
@ -44,6 +44,8 @@ public final class Example {
|
|||||||
|
|
||||||
// Configure the client
|
// Configure the client
|
||||||
TDLibSettings settings = TDLibSettings.create(apiToken);
|
TDLibSettings settings = TDLibSettings.create(apiToken);
|
||||||
|
// This is an example, remove this line to use the real telegram datacenters!
|
||||||
|
settings.setUseTestDatacenter(true);
|
||||||
|
|
||||||
// Configure the session directory
|
// Configure the session directory
|
||||||
Path sessionPath = Paths.get("example-tdlight-session");
|
Path sessionPath = Paths.get("example-tdlight-session");
|
||||||
@ -54,7 +56,7 @@ public final class Example {
|
|||||||
SimpleTelegramClientBuilder clientBuilder = clientFactory.builder(settings);
|
SimpleTelegramClientBuilder clientBuilder = clientFactory.builder(settings);
|
||||||
|
|
||||||
// Configure the authentication info
|
// Configure the authentication info
|
||||||
ConsoleInteractiveAuthenticationData authenticationData = AuthenticationSupplier.consoleLogin();
|
SimpleAuthenticationSupplier<?> authenticationData = AuthenticationSupplier.testUser(3210); // Replace with AuthenticationSupplier.consoleLogin(), or .user(xxx), or .bot(xxx);
|
||||||
|
|
||||||
// Add an example update handler that prints when the bot is started
|
// Add an example update handler that prints when the bot is started
|
||||||
clientBuilder.addUpdateHandler(TdApi.UpdateAuthorizationState.class, Example::onUpdateAuthorizationState);
|
clientBuilder.addUpdateHandler(TdApi.UpdateAuthorizationState.class, Example::onUpdateAuthorizationState);
|
||||||
|
@ -8,6 +8,7 @@ import it.tdlight.util.CleanSupport.CleanableSupport;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.locks.StampedLock;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -92,9 +93,9 @@ public class ClientFactory implements AutoCloseable {
|
|||||||
TdApi.Object[] clientEvents,
|
TdApi.Object[] clientEvents,
|
||||||
int arrayOffset,
|
int arrayOffset,
|
||||||
int arrayLength) {
|
int arrayLength) {
|
||||||
var eventsHandlingLock = state.getEventsHandlingLock();
|
StampedLock eventsHandlingLock = state.getEventsHandlingLock();
|
||||||
boolean closeWriteLockAcquisitionFailed = false;
|
boolean closeWriteLockAcquisitionFailed = false;
|
||||||
var stamp = eventsHandlingLock.readLock();
|
long stamp = eventsHandlingLock.readLock();
|
||||||
try {
|
try {
|
||||||
ClientEventsHandler handler = state.getClientEventsHandler(clientId);
|
ClientEventsHandler handler = state.getClientEventsHandler(clientId);
|
||||||
|
|
||||||
@ -119,7 +120,7 @@ public class ClientFactory implements AutoCloseable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isClosed) {
|
if (isClosed) {
|
||||||
var writeLockStamp = eventsHandlingLock.tryConvertToWriteLock(stamp);
|
long writeLockStamp = eventsHandlingLock.tryConvertToWriteLock(stamp);
|
||||||
if (writeLockStamp != 0L) {
|
if (writeLockStamp != 0L) {
|
||||||
stamp = writeLockStamp;
|
stamp = writeLockStamp;
|
||||||
removeClientEventHandlers(clientId);
|
removeClientEventHandlers(clientId);
|
||||||
|
@ -152,7 +152,7 @@ final class InternalClient implements ClientEventsHandler, TelegramClient {
|
|||||||
private void createAndRegisterClient() {
|
private void createAndRegisterClient() {
|
||||||
InternalClientsState clientManagerState = this.clientManagerState;
|
InternalClientsState clientManagerState = this.clientManagerState;
|
||||||
final StampedLock eventsHandlingLock = clientManagerState.getEventsHandlingLock();
|
final StampedLock eventsHandlingLock = clientManagerState.getEventsHandlingLock();
|
||||||
var stamp = eventsHandlingLock.writeLock();
|
long stamp = eventsHandlingLock.writeLock();
|
||||||
try {
|
try {
|
||||||
if (clientId != null) {
|
if (clientId != null) {
|
||||||
throw new UnsupportedOperationException("Can't initialize the same client twice!");
|
throw new UnsupportedOperationException("Can't initialize the same client twice!");
|
||||||
|
@ -15,6 +15,7 @@ import java.util.concurrent.ScheduledFuture;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.concurrent.locks.StampedLock;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
import org.reactivestreams.Subscription;
|
import org.reactivestreams.Subscription;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -165,8 +166,8 @@ final class InternalReactiveClient implements ClientEventsHandler, ReactiveTeleg
|
|||||||
}
|
}
|
||||||
logger.debug(TG_MARKER, "Creating new client");
|
logger.debug(TG_MARKER, "Creating new client");
|
||||||
clientId = NativeClientAccess.create();
|
clientId = NativeClientAccess.create();
|
||||||
var eventsHandlingLock = clientManagerState.getEventsHandlingLock();
|
StampedLock eventsHandlingLock = clientManagerState.getEventsHandlingLock();
|
||||||
var stamp = eventsHandlingLock.writeLock();
|
long stamp = eventsHandlingLock.writeLock();
|
||||||
try {
|
try {
|
||||||
logger.debug(TG_MARKER, "Registering new client {}", clientId);
|
logger.debug(TG_MARKER, "Registering new client {}", clientId);
|
||||||
clientManagerState.registerClient(clientId, this);
|
clientManagerState.registerClient(clientId, this);
|
||||||
|
@ -10,12 +10,13 @@ final class AuthenticationDataImpl implements SimpleAuthenticationSupplier<Authe
|
|||||||
|
|
||||||
private final String userPhoneNumber;
|
private final String userPhoneNumber;
|
||||||
private final String botToken;
|
private final String botToken;
|
||||||
|
private final boolean test;
|
||||||
/**
|
/**
|
||||||
* Safe string representation of the bot token
|
* Safe string representation of the bot token
|
||||||
*/
|
*/
|
||||||
private final String botTokenId;
|
private final String botTokenId;
|
||||||
|
|
||||||
AuthenticationDataImpl(String userPhoneNumber, String botToken) {
|
AuthenticationDataImpl(String userPhoneNumber, String botToken, boolean test) {
|
||||||
if ((userPhoneNumber == null) == (botToken == null)) {
|
if ((userPhoneNumber == null) == (botToken == null)) {
|
||||||
throw new IllegalArgumentException("Please use either a bot token or a phone number");
|
throw new IllegalArgumentException("Please use either a bot token or a phone number");
|
||||||
}
|
}
|
||||||
@ -26,6 +27,7 @@ final class AuthenticationDataImpl implements SimpleAuthenticationSupplier<Authe
|
|||||||
}
|
}
|
||||||
this.userPhoneNumber = userPhoneNumber;
|
this.userPhoneNumber = userPhoneNumber;
|
||||||
this.botToken = botToken;
|
this.botToken = botToken;
|
||||||
|
this.test = test;
|
||||||
if (botToken != null) {
|
if (botToken != null) {
|
||||||
String[] parts = botToken.split(":", 2);
|
String[] parts = botToken.split(":", 2);
|
||||||
if (parts.length > 0) {
|
if (parts.length > 0) {
|
||||||
@ -48,6 +50,10 @@ final class AuthenticationDataImpl implements SimpleAuthenticationSupplier<Authe
|
|||||||
return botToken != null;
|
return botToken != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isTest() {
|
||||||
|
return test;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUserPhoneNumber() {
|
public String getUserPhoneNumber() {
|
||||||
if (userPhoneNumber == null) {
|
if (userPhoneNumber == null) {
|
||||||
@ -66,10 +72,16 @@ final class AuthenticationDataImpl implements SimpleAuthenticationSupplier<Authe
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
String value;
|
||||||
if (userPhoneNumber != null) {
|
if (userPhoneNumber != null) {
|
||||||
return userPhoneNumber;
|
value = userPhoneNumber;
|
||||||
} else {
|
} else {
|
||||||
return botTokenId;
|
value = botTokenId;
|
||||||
|
}
|
||||||
|
if (test) {
|
||||||
|
return value + " (test)";
|
||||||
|
} else {
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,12 +94,13 @@ final class AuthenticationDataImpl implements SimpleAuthenticationSupplier<Authe
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
AuthenticationDataImpl that = (AuthenticationDataImpl) o;
|
AuthenticationDataImpl that = (AuthenticationDataImpl) o;
|
||||||
return Objects.equals(userPhoneNumber, that.userPhoneNumber) && Objects.equals(botToken, that.botToken);
|
return Objects.equals(userPhoneNumber, that.userPhoneNumber) && Objects.equals(botToken, that.botToken)
|
||||||
|
&& Objects.equals(test, that.test);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(userPhoneNumber, botToken);
|
return Objects.hash(userPhoneNumber, botToken, test);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -4,6 +4,20 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
|
|
||||||
public interface AuthenticationSupplier<T extends AuthenticationData> {
|
public interface AuthenticationSupplier<T extends AuthenticationData> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User used in Telegram Test Servers
|
||||||
|
* @param value any number from 0001 to 9999
|
||||||
|
*/
|
||||||
|
static SimpleAuthenticationSupplier<?> testUser(int value) {
|
||||||
|
if (value < 1) {
|
||||||
|
throw new IllegalArgumentException("value must be greater than 0");
|
||||||
|
}
|
||||||
|
if (value > 9999) {
|
||||||
|
throw new IllegalArgumentException("value must be lower than 10000");
|
||||||
|
}
|
||||||
|
return new AuthenticationDataImpl("999664" + value, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
CompletableFuture<T> get();
|
CompletableFuture<T> get();
|
||||||
|
|
||||||
static SimpleAuthenticationSupplier<?> qrCode() {
|
static SimpleAuthenticationSupplier<?> qrCode() {
|
||||||
@ -19,11 +33,11 @@ public interface AuthenticationSupplier<T extends AuthenticationData> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static SimpleAuthenticationSupplier<?> user(String userPhoneNumber) {
|
static SimpleAuthenticationSupplier<?> user(String userPhoneNumber) {
|
||||||
return new AuthenticationDataImpl(userPhoneNumber, null);
|
return new AuthenticationDataImpl(userPhoneNumber, null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SimpleAuthenticationSupplier<?> bot(String botToken) {
|
static SimpleAuthenticationSupplier<?> bot(String botToken) {
|
||||||
return new AuthenticationDataImpl(null, botToken);
|
return new AuthenticationDataImpl(null, botToken, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConsoleInteractiveAuthenticationData consoleLogin() {
|
static ConsoleInteractiveAuthenticationData consoleLogin() {
|
||||||
|
@ -11,13 +11,16 @@ final class AuthorizationStateWaitCodeHandler implements GenericUpdateHandler<Up
|
|||||||
|
|
||||||
private final TelegramClient client;
|
private final TelegramClient client;
|
||||||
private final ClientInteraction clientInteraction;
|
private final ClientInteraction clientInteraction;
|
||||||
|
private final String testCode;
|
||||||
private final ExceptionHandler exceptionHandler;
|
private final ExceptionHandler exceptionHandler;
|
||||||
|
|
||||||
public AuthorizationStateWaitCodeHandler(TelegramClient client,
|
public AuthorizationStateWaitCodeHandler(TelegramClient client,
|
||||||
ClientInteraction clientInteraction,
|
ClientInteraction clientInteraction,
|
||||||
|
String testCode,
|
||||||
ExceptionHandler exceptionHandler) {
|
ExceptionHandler exceptionHandler) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.clientInteraction = clientInteraction;
|
this.clientInteraction = clientInteraction;
|
||||||
|
this.testCode = testCode;
|
||||||
this.exceptionHandler = exceptionHandler;
|
this.exceptionHandler = exceptionHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,23 +28,31 @@ final class AuthorizationStateWaitCodeHandler implements GenericUpdateHandler<Up
|
|||||||
public void onUpdate(UpdateAuthorizationState update) {
|
public void onUpdate(UpdateAuthorizationState update) {
|
||||||
if (update.authorizationState.getConstructor() == AuthorizationStateWaitCode.CONSTRUCTOR) {
|
if (update.authorizationState.getConstructor() == AuthorizationStateWaitCode.CONSTRUCTOR) {
|
||||||
AuthorizationStateWaitCode authorizationState = (AuthorizationStateWaitCode) update.authorizationState;
|
AuthorizationStateWaitCode authorizationState = (AuthorizationStateWaitCode) update.authorizationState;
|
||||||
ParameterInfo parameterInfo = new ParameterInfoCode(authorizationState.codeInfo.phoneNumber,
|
if (testCode != null) {
|
||||||
authorizationState.codeInfo.nextType,
|
sendCode(testCode);
|
||||||
authorizationState.codeInfo.timeout,
|
} else {
|
||||||
authorizationState.codeInfo.type
|
ParameterInfo parameterInfo = new ParameterInfoCode(authorizationState.codeInfo.phoneNumber,
|
||||||
);
|
authorizationState.codeInfo.nextType,
|
||||||
clientInteraction.onParameterRequest(InputParameter.ASK_CODE, parameterInfo).whenComplete((code, ex) -> {
|
authorizationState.codeInfo.timeout,
|
||||||
if (ex != null) {
|
authorizationState.codeInfo.type
|
||||||
exceptionHandler.onException(ex);
|
);
|
||||||
return;
|
clientInteraction.onParameterRequest(InputParameter.ASK_CODE, parameterInfo).whenComplete((code, ex) -> {
|
||||||
}
|
if (ex != null) {
|
||||||
CheckAuthenticationCode response = new CheckAuthenticationCode(code);
|
exceptionHandler.onException(ex);
|
||||||
client.send(response, ok -> {
|
return;
|
||||||
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {
|
|
||||||
throw new TelegramError((TdApi.Error) ok);
|
|
||||||
}
|
}
|
||||||
}, exceptionHandler);
|
sendCode(code);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendCode(String code) {
|
||||||
|
CheckAuthenticationCode response = new CheckAuthenticationCode(code);
|
||||||
|
client.send(response, ok -> {
|
||||||
|
if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {
|
||||||
|
throw new TelegramError((TdApi.Error) ok);
|
||||||
|
}
|
||||||
|
}, exceptionHandler);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,6 +105,7 @@ public final class SimpleTelegramClient implements Authenticable, MutableTelegra
|
|||||||
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
|
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
|
||||||
new AuthorizationStateWaitCodeHandler(client,
|
new AuthorizationStateWaitCodeHandler(client,
|
||||||
new SimpleTelegramClientInteraction(),
|
new SimpleTelegramClientInteraction(),
|
||||||
|
getTestCode(authenticationData),
|
||||||
this::handleDefaultException
|
this::handleDefaultException
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -119,6 +120,18 @@ public final class SimpleTelegramClient implements Authenticable, MutableTelegra
|
|||||||
client.initialize(this::handleUpdate, this::handleUpdateException, this::handleDefaultException);
|
client.initialize(this::handleUpdate, this::handleUpdateException, this::handleDefaultException);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getTestCode(AuthenticationSupplier<?> authenticationData) {
|
||||||
|
if (authenticationData instanceof AuthenticationDataImpl) {
|
||||||
|
if (!((AuthenticationDataImpl) authenticationData).isBot()
|
||||||
|
&& ((AuthenticationDataImpl) authenticationData).isTest()) {
|
||||||
|
String phoneNumber = ((AuthenticationDataImpl) authenticationData).getUserPhoneNumber();
|
||||||
|
String substring = phoneNumber.substring(5, 6);
|
||||||
|
return substring + substring + substring + substring;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private void handleUpdate(TdApi.Object update) {
|
private void handleUpdate(TdApi.Object update) {
|
||||||
boolean handled = false;
|
boolean handled = false;
|
||||||
for (ResultHandler<TdApi.Update> updateHandler : updateHandlers.keySet()) {
|
for (ResultHandler<TdApi.Update> updateHandler : updateHandlers.keySet()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user