Support java 17 sealed types

This commit is contained in:
Andrea Cavalli 2021-10-23 23:05:08 +02:00
parent 5e7f597c86
commit 40f845cdce
3 changed files with 68 additions and 82 deletions

View File

@ -26,24 +26,24 @@
<dependency> <dependency>
<groupId>it.tdlight</groupId> <groupId>it.tdlight</groupId>
<artifactId>tdlight-java</artifactId> <artifactId>tdlight-java</artifactId>
<version>2.7.8.32</version> <version>2.7.8.33</version>
</dependency> </dependency>
<!-- TDLight natives --> <!-- TDLight natives -->
<dependency> <dependency>
<groupId>it.tdlight</groupId> <groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-linux-amd64</artifactId> <artifactId>tdlight-natives-linux-amd64</artifactId>
<version>4.0.173</version> <version>4.0.175</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>it.tdlight</groupId> <groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-windows-amd64</artifactId> <artifactId>tdlight-natives-windows-amd64</artifactId>
<version>4.0.173</version> <version>4.0.175</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>it.tdlight</groupId> <groupId>it.tdlight</groupId>
<artifactId>tdlight-natives-osx-amd64</artifactId> <artifactId>tdlight-natives-osx-amd64</artifactId>
<version>4.0.173</version> <version>4.0.175</version>
</dependency> </dependency>
<!-- log4j logging --> <!-- log4j logging -->
@ -78,8 +78,8 @@
<version>3.8.1</version> <version>3.8.1</version>
<configuration> <configuration>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
<source>11</source> <source>17</source>
<target>11</target> <target>17</target>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@ -1,6 +1,6 @@
package it.tdlight.example; package it.tdlight.example;
import it.tdlight.client.APIToken; import it.tdlight.client.*;
import it.tdlight.client.AuthenticationData; import it.tdlight.client.AuthenticationData;
import it.tdlight.client.CommandHandler; import it.tdlight.client.CommandHandler;
import it.tdlight.client.SimpleTelegramClient; import it.tdlight.client.SimpleTelegramClient;
@ -8,15 +8,6 @@ import it.tdlight.client.TDLibSettings;
import it.tdlight.common.Init; import it.tdlight.common.Init;
import it.tdlight.common.utils.CantLoadLibrary; import it.tdlight.common.utils.CantLoadLibrary;
import it.tdlight.jni.TdApi; import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.AuthorizationState;
import it.tdlight.jni.TdApi.Chat;
import it.tdlight.jni.TdApi.MessageContent;
import it.tdlight.jni.TdApi.MessageSender;
import it.tdlight.jni.TdApi.MessageSenderUser;
import it.tdlight.jni.TdApi.MessageText;
import it.tdlight.jni.TdApi.UpdateAuthorizationState;
import it.tdlight.jni.TdApi.UpdateNewMessage;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
/** /**
@ -29,61 +20,39 @@ public final class Example {
/** /**
* Admin user id, used by the stop command example * Admin user id, used by the stop command example
*/ */
private static final long ADMIN_ID = 667900586; private static final TdApi.MessageSender ADMIN_ID = new TdApi.MessageSenderUser(667900586);
private static SimpleTelegramClient client;
public static void main(String[] args) throws CantLoadLibrary, InterruptedException { public static void main(String[] args) throws CantLoadLibrary, InterruptedException {
// Initialize TDLight native libraries // Initialize TDLight native libraries
Init.start(); Init.start();
// Obtain the API token // Obtain the API token
APIToken apiToken = APIToken.example(); var apiToken = APIToken.example();
// Configure the client // Configure the client
TDLibSettings settings = TDLibSettings.create(apiToken); var settings = TDLibSettings.create(apiToken);
// Configure the session directory // Configure the session directory
Path sessionPath = Paths.get("example-tdlight-session"); var sessionPath = Paths.get("example-tdlight-session");
settings.setDatabaseDirectoryPath(sessionPath.resolve("data")); settings.setDatabaseDirectoryPath(sessionPath.resolve("data"));
settings.setDownloadedFilesDirectoryPath(sessionPath.resolve("downloads")); settings.setDownloadedFilesDirectoryPath(sessionPath.resolve("downloads"));
// Create a client // Create a client
SimpleTelegramClient client = new SimpleTelegramClient(settings); client = new SimpleTelegramClient(settings);
// Configure the authentication info // Configure the authentication info
AuthenticationData authenticationData = AuthenticationData.consoleLogin(); var authenticationData = AuthenticationData.consoleLogin();
// Add an example update handler that prints when the bot is started // Add an example update handler that prints when the bot is started
client.addUpdateHandler(UpdateAuthorizationState.class, update -> printStatus(update.authorizationState)); client.addUpdateHandler(TdApi.UpdateAuthorizationState.class, Example::onUpdateAuthorizationState);
// Add an example command handler that stops the bot
client.addCommandHandler("stop", new StopCommandHandler(client));
// Add an example update handler that prints every received message // Add an example update handler that prints every received message
client.addUpdateHandler(UpdateNewMessage.class, update -> { client.addUpdateHandler(TdApi.UpdateNewMessage.class, Example::onUpdateNewMessage);
// Get the message content
MessageContent messageContent = update.message.content;
// Get the message text // Add an example command handler that stops the bot
String text; client.addCommandHandler("stop", new StopCommandHandler());
if (messageContent instanceof MessageText) {
// Get the text of the text message
text = ((MessageText) messageContent).text.text;
} else {
// We handle only text messages, the other messages will be printed as their type
text = "(" + messageContent.getClass().getSimpleName() + ")";
}
// Get the chat title
client.send(new TdApi.GetChat(update.message.chatId), chatIdResult -> {
// Get the chat response
Chat chat = chatIdResult.get();
// Get the chat name
String chatName = chat.title;
// Print the message
System.out.println("Received new message from chat " + chatName + ": " + text);
});
});
// Start the client // Start the client
client.start(authenticationData); client.start(authenticationData);
@ -92,10 +61,53 @@ public final class Example {
client.waitForExit(); client.waitForExit();
} }
private static void onUpdateNewMessage(TdApi.UpdateNewMessage update) {
// Get the message content
var messageContent = update.message.content;
// Get the message text
String text;
if (messageContent instanceof TdApi.MessageText messageText) {
// Get the text of the text message
text = messageText.text.text;
} else {
// We handle only text messages, the other messages will be printed as their type
text = "(" + messageContent.getClass().getSimpleName() + ")";
}
// Get the chat title
client.send(new TdApi.GetChat(update.message.chatId), chatIdResult -> {
// Get the chat response
var chat = chatIdResult.get();
// Get the chat name
var chatName = chat.title;
// Print the message
System.out.println("Received new message from chat " + chatName + ": " + text);
});
}
/**
* Close the bot if the /stop command is sent by the administrator
*/
private static class StopCommandHandler implements CommandHandler {
@Override
public void onCommand(TdApi.Chat chat, TdApi.MessageSender commandSender, String arguments) {
// Check if the sender is the admin
if (isAdmin(commandSender)) {
// Stop the client
System.out.println("Received stop command. closing...");
client.sendClose();
}
}
}
/** /**
* Print the bot status * Print the bot status
*/ */
private static void printStatus(AuthorizationState authorizationState) { private static void onUpdateAuthorizationState(TdApi.UpdateAuthorizationState update) {
var authorizationState = update.authorizationState;
if (authorizationState instanceof TdApi.AuthorizationStateReady) { if (authorizationState instanceof TdApi.AuthorizationStateReady) {
System.out.println("Logged in"); System.out.println("Logged in");
} else if (authorizationState instanceof TdApi.AuthorizationStateClosing) { } else if (authorizationState instanceof TdApi.AuthorizationStateClosing) {
@ -110,33 +122,7 @@ public final class Example {
/** /**
* Check if the command sender is admin * Check if the command sender is admin
*/ */
private static boolean isAdmin(MessageSender sender) { private static boolean isAdmin(TdApi.MessageSender sender) {
if (sender instanceof MessageSenderUser) { return sender.equals(ADMIN_ID);
MessageSenderUser senderUser = (MessageSenderUser) sender;
return senderUser.userId == ADMIN_ID;
}
return false;
}
/**
* Close the bot if the /stop command is sent by the administrator
*/
private static class StopCommandHandler implements CommandHandler {
private final SimpleTelegramClient client;
public StopCommandHandler(SimpleTelegramClient client) {
this.client = client;
}
@Override
public void onCommand(Chat chat, MessageSender commandSender, String arguments) {
// Check if the sender is the admin
if (isAdmin(commandSender)) {
// Stop the client
System.out.println("Received stop command. closing...");
client.sendClose();
}
}
} }
} }

View File

@ -5,12 +5,12 @@
<artifactId>tdlight-java-parent</artifactId> <artifactId>tdlight-java-parent</artifactId>
<version>${revision}</version> <version>${revision}</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>TDLib Java Parent</name> <name>TDLight Java Parent</name>
<properties> <properties>
<revision>1.0.0.0-SNAPSHOT</revision> <revision>1.0.0.0-SNAPSHOT</revision>
<nativesRevisionNumber>173</nativesRevisionNumber> <nativesRevisionNumber>175</nativesRevisionNumber>
<nativesRevisionSuffix/> <nativesRevisionSuffix/>
<apiRevisionNumber>171</apiRevisionNumber> <apiRevisionNumber>173</apiRevisionNumber>
<apiRevisionSuffix/> <apiRevisionSuffix/>
</properties> </properties>
<repositories> <repositories>