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

135 lines
4.2 KiB
Java
Raw Normal View History

2020-10-23 04:44:15 +02:00
package it.tdlight.example;
2021-09-27 19:27:13 +02:00
import it.tdlight.client.APIToken;
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-09-27 19:27:13 +02:00
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;
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>
* The documentation of the TDLight functions can be found here: https://tdlight-team.github.io/tdlight-docs
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
*/
private static final long ADMIN_ID = 667900586;
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
APIToken apiToken = APIToken.example();
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Configure the client
TDLibSettings settings = TDLibSettings.create(apiToken);
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Create a client
SimpleTelegramClient client = new SimpleTelegramClient(settings);
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Configure the authentication info
AuthenticationData 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
client.addUpdateHandler(UpdateAuthorizationState.class, update -> printStatus(update.authorizationState));
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Add an example command handler that stops the bot
client.addCommandHandler("stop", new StopCommandHandler(client));
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
client.addUpdateHandler(UpdateNewMessage.class, update -> {
// Get the message content
MessageContent messageContent = update.message.content;
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Get the message text
String text;
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() + ")";
2020-10-23 04:44:15 +02:00
}
2021-09-27 19:27:13 +02:00
// Get the chat title
2021-10-20 23:54:59 +02:00
client.send(new TdApi.GetChat(update.message.chatId), chatIdResult -> {
2021-09-27 19:27:13 +02:00
// Get the chat response
Chat chat = chatIdResult.get();
// Get the chat name
String chatName = chat.title;
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
// Print the message
System.out.println("Received new message from chat " + chatName + ": " + text);
});
});
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-09-27 19:27:13 +02:00
/**
* Print the bot status
*/
private static void printStatus(AuthorizationState 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...");
2020-10-23 04:44:15 +02:00
}
}
2021-09-27 19:27:13 +02:00
/**
* Check if the command sender is admin
*/
private static boolean isAdmin(MessageSender sender) {
if (sender instanceof MessageSenderUser) {
MessageSenderUser senderUser = (MessageSenderUser) sender;
return senderUser.userId == ADMIN_ID;
2020-10-23 04:44:15 +02:00
}
2021-09-27 19:27:13 +02:00
return false;
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
2021-09-27 19:27:13 +02:00
private final SimpleTelegramClient client;
2020-10-23 04:44:15 +02:00
2021-09-27 19:27:13 +02:00
public StopCommandHandler(SimpleTelegramClient client) {
this.client = client;
2020-10-23 04:44:15 +02:00
}
2021-01-16 10:07:08 +01:00
2020-10-23 04:44:15 +02:00
@Override
2021-09-27 19:27:13 +02:00
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();
2020-10-23 04:44:15 +02:00
}
}
}
}