Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.commons.io.FileUtils;
- import org.telegram.telegrambots.bots.TelegramLongPollingBot;
- import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
- import org.telegram.telegrambots.meta.api.objects.Update;
- import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- public class ITTBot extends TelegramLongPollingBot {
- public void onUpdateReceived(Update update) {
- if (!update.hasMessage() && !update.getMessage().hasText()) return;
- long chatId = update.getMessage().getChatId();
- if (update.getMessage().getText().equals("/start")) {
- // asking for the nickname
- SendMessage welcomeMex = new SendMessage().setChatId(chatId).setText("Ciao, sono il bot dell'ITT Dorso." + "\n"
- + "Siccome i tuoi rappresentati d'Istituto rispettano te e la tua privacy, " +
- "sei pregato/a di inserire un nickname in modo tale da rimanere anonimo/a");
- try {
- execute(welcomeMex);
- } catch (TelegramApiException e) {
- e.printStackTrace();
- }
- }
- if (!update.getMessage().getText().startsWith("/")) {
- String username = update.getMessage().getText();
- // checking if the nickname already exists
- write:
- try (FileWriter usernameFile = new FileWriter(Main.getUsernameFile(), true)) {
- if (FileUtils.readFileToString(Main.getUsernameFile()).contains(String.valueOf(chatId))) {
- SendMessage errorName2 = new SendMessage().setChatId(chatId).setText("Hai già uno username. Qual è il problema?");
- execute(errorName2);
- break write;
- } else if (FileUtils.readFileToString(Main.getUsernameFile()).contains(username)) {
- SendMessage errorName = new SendMessage().setChatId(chatId).setText("Questo username non è disponibile, inseriscine un altro");
- execute(errorName);
- break write;
- } else {
- // asking for the problem
- SendMessage problemMex = new SendMessage().setChatId(chatId).setText("Perfetto " + username + ". Qual è il problema?");
- execute(problemMex);
- }
- BufferedWriter bw = new BufferedWriter(usernameFile);
- bw.write(username + "," + chatId);
- bw.close();
- } catch (IOException | TelegramApiException e) {
- e.printStackTrace();
- }
- }
- }
- public String getBotUsername() {
- return "GUIDODORSO_BOT";
- }
- public String getBotToken() {
- return "1005131699:AAH-MAZDIglHvfrNx7Kk_X5w68S2Q1Jps9g";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement