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

131 lines
4.1 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.*;
2021-09-27 19:27:13 +02:00
import it.tdlight.client.AuthenticationData;
import it.tdlight.client.CommandHandler;
import it.tdlight.client.SimpleTelegramClient;
import it.tdlight.client.TDLibSettings;
2020-10-23 04:45:49 +02:00
import it.tdlight.common.Init;
import it.tdlight.common.utils.CantLoadLibrary;
2020-10-23 04:44:15 +02:00
import it.tdlight.jni.TdApi;
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
2021-09-27 19:27:13 +02:00
public static void main(String[] args) throws CantLoadLibrary, InterruptedException {
// Initialize TDLight native libraries
Init.start();
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Obtain the API token
2021-10-23 23:05:08 +02:00
var apiToken = APIToken.example();
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Configure the client
2021-10-23 23:05:08 +02:00
var settings = TDLibSettings.create(apiToken);
2020-10-23 04:44:15 +02:00
2021-10-23 03:59:02 +02:00
// Configure the session directory
2021-10-23 23:05:08 +02:00
var sessionPath = Paths.get("example-tdlight-session");
2021-10-23 03:59:02 +02:00
settings.setDatabaseDirectoryPath(sessionPath.resolve("data"));
settings.setDownloadedFilesDirectoryPath(sessionPath.resolve("downloads"));
2021-09-27 19:27:13 +02:00
// Create a client
2021-10-23 23:05:08 +02:00
client = new SimpleTelegramClient(settings);
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Configure the authentication info
2021-10-24 01:03:16 +02:00
var authenticationData = AuthenticationData.consoleLogin();
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Add an example update handler that prints when the bot is started
2021-10-23 23:05:08 +02:00
client.addUpdateHandler(TdApi.UpdateAuthorizationState.class, Example::onUpdateAuthorizationState);
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Add an example update handler that prints every received message
2021-10-23 23:05:08 +02:00
client.addUpdateHandler(TdApi.UpdateNewMessage.class, Example::onUpdateNewMessage);
2020-10-23 04:44:15 +02:00
2021-10-23 23:05:08 +02:00
// Add an example command handler that stops the bot
client.addCommandHandler("stop", new StopCommandHandler());
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Start the client
client.start(authenticationData);
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// 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
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
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
var chat = chatIdResult.get();
// Get the chat name
var chatName = chat.title;
// 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) {
var authorizationState = update.authorizationState;
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
}