tdlight-java/example/src/main/java/it/tdlight/example/Example.java

146 lines
4.8 KiB
Java
Raw Normal View History

2020-10-23 04:44:15 +02:00
package it.tdlight.example;
2021-10-23 23:05:08 +02:00
import it.tdlight.client.*;
2023-04-27 02:14:39 +02:00
import it.tdlight.client.AuthenticationSupplier;
2021-09-27 19:27:13 +02:00
import it.tdlight.client.CommandHandler;
import it.tdlight.client.SimpleTelegramClient;
import it.tdlight.client.TDLibSettings;
2023-04-27 02:14:39 +02:00
import it.tdlight.Init;
import it.tdlight.jni.TdApi.AuthorizationState;
import it.tdlight.jni.TdApi.Chat;
import it.tdlight.jni.TdApi.MessageContent;
2020-10-23 04:44:15 +02:00
import it.tdlight.jni.TdApi;
2023-05-10 15:26:11 +02:00
import it.tdlight.util.UnsupportedNativeLibraryException;
2023-04-27 02:14:39 +02:00
import java.nio.file.Path;
2021-10-23 03:59:02 +02:00
import java.nio.file.Paths;
2020-10-23 04:44:15 +02:00
/**
2021-09-27 19:27:13 +02:00
* Example class for TDLight Java
2021-10-22 12:54:28 +02:00
* <p>
2022-07-12 17:53:52 +02:00
* <a href="https://tdlight-team.github.io/tdlight-docs">The documentation of the TDLight functions can be found here</a>
2020-10-23 04:44:15 +02:00
*/
public final class Example {
2021-09-27 19:27:13 +02:00
/**
* Admin user id, used by the stop command example
*/
2021-10-23 23:05:08 +02:00
private static final TdApi.MessageSender ADMIN_ID = new TdApi.MessageSenderUser(667900586);
private static SimpleTelegramClient client;
2020-10-23 04:44:15 +02:00
2023-05-10 15:26:11 +02:00
public static void main(String[] args) throws UnsupportedNativeLibraryException, InterruptedException {
2021-09-27 19:27:13 +02:00
// Initialize TDLight native libraries
2023-05-10 15:26:11 +02:00
Init.init();
2020-10-23 04:44:15 +02:00
2023-04-27 02:14:39 +02:00
// Create the client factory
try (SimpleTelegramClientFactory clientFactory = new SimpleTelegramClientFactory()) {
2020-10-23 04:44:15 +02:00
2023-04-27 02:14:39 +02:00
// Obtain the API token
//
// var apiToken = new APIToken(your-api-id-here, "your-api-hash-here");
//
APIToken apiToken = APIToken.example();
2020-10-23 04:44:15 +02:00
2021-10-23 03:59:02 +02:00
2023-04-27 02:14:39 +02:00
// Configure the client
TDLibSettings settings = TDLibSettings.create(apiToken);
2020-10-23 04:44:15 +02:00
2023-04-27 02:14:39 +02:00
// Configure the session directory
Path sessionPath = Paths.get("example-tdlight-session");
settings.setDatabaseDirectoryPath(sessionPath.resolve("data"));
settings.setDownloadedFilesDirectoryPath(sessionPath.resolve("downloads"));
2020-10-23 04:44:15 +02:00
2023-04-27 02:14:39 +02:00
// Prepare a new client builder
SimpleTelegramClientBuilder clientBuilder = clientFactory.builder(settings);
2020-10-23 04:44:15 +02:00
2023-04-27 02:14:39 +02:00
// Configure the authentication info
2023-05-15 00:10:23 +02:00
// Replace with AuthenticationSupplier.consoleLogin(), or .user(xxx), or .bot(xxx);
SimpleAuthenticationSupplier<?> authenticationData = AuthenticationSupplier.testUser(7381);
// This is an example, remove this line to use the real telegram datacenters!
settings.setUseTestDatacenter(true);
2020-10-23 04:44:15 +02:00
2023-04-27 02:14:39 +02:00
// Add an example update handler that prints when the bot is started
clientBuilder.addUpdateHandler(TdApi.UpdateAuthorizationState.class, Example::onUpdateAuthorizationState);
2020-10-23 04:44:15 +02:00
2023-04-27 02:14:39 +02:00
// Add an example update handler that prints every received message
clientBuilder.addUpdateHandler(TdApi.UpdateNewMessage.class, Example::onUpdateNewMessage);
2020-10-23 04:44:15 +02:00
2023-04-27 02:14:39 +02:00
// Add an example command handler that stops the bot
clientBuilder.addCommandHandler("stop", new StopCommandHandler());
// Create and start the client
client = clientBuilder.build(authenticationData);
// Wait for exit
client.waitForExit();
}
2020-10-23 04:44:15 +02:00
}
2021-10-24 21:35:44 +02:00
/**
* Print new messages received via updateNewMessage
*/
2021-10-23 23:05:08 +02:00
private static void onUpdateNewMessage(TdApi.UpdateNewMessage update) {
// Get the message content
2023-04-27 02:14:39 +02:00
MessageContent messageContent = update.message.content;
2021-10-23 23:05:08 +02:00
// 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
2021-10-24 21:35:44 +02:00
text = String.format("(%s)", messageContent.getClass().getSimpleName());
2020-10-23 04:44:15 +02:00
}
2021-10-23 23:05:08 +02:00
// Get the chat title
client.send(new TdApi.GetChat(update.message.chatId), chatIdResult -> {
// Get the chat response
2023-04-27 02:14:39 +02:00
Chat chat = chatIdResult.get();
2021-10-23 23:05:08 +02:00
// Get the chat name
2023-04-27 02:14:39 +02:00
String chatName = chat.title;
2021-10-23 23:05:08 +02:00
// Print the message
2021-10-24 21:35:44 +02:00
System.out.printf("Received new message from chat %s: %s%n", chatName, text);
2021-10-23 23:05:08 +02:00
});
2020-10-23 04:44:15 +02:00
}
2021-09-27 19:27:13 +02:00
/**
* Close the bot if the /stop command is sent by the administrator
*/
private static class StopCommandHandler implements CommandHandler {
2020-10-23 04:44:15 +02:00
@Override
2021-10-23 23:05:08 +02:00
public void onCommand(TdApi.Chat chat, TdApi.MessageSender commandSender, String arguments) {
2021-09-27 19:27:13 +02:00
// Check if the sender is the admin
if (isAdmin(commandSender)) {
// Stop the client
System.out.println("Received stop command. closing...");
client.sendClose();
2020-10-23 04:44:15 +02:00
}
}
}
2021-10-23 23:05:08 +02:00
/**
* Print the bot status
*/
private static void onUpdateAuthorizationState(TdApi.UpdateAuthorizationState update) {
2023-04-27 02:14:39 +02:00
AuthorizationState authorizationState = update.authorizationState;
2021-10-23 23:05:08 +02:00
if (authorizationState instanceof TdApi.AuthorizationStateReady) {
System.out.println("Logged in");
} else if (authorizationState instanceof TdApi.AuthorizationStateClosing) {
System.out.println("Closing...");
} else if (authorizationState instanceof TdApi.AuthorizationStateClosed) {
System.out.println("Closed");
} else if (authorizationState instanceof TdApi.AuthorizationStateLoggingOut) {
System.out.println("Logging out...");
}
}
/**
* Check if the command sender is admin
*/
private static boolean isAdmin(TdApi.MessageSender sender) {
return sender.equals(ADMIN_ID);
}
2020-10-23 04:44:15 +02:00
}