Advertisement
Guest User

ae

a guest
Dec 9th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. import org.apache.commons.io.FileUtils;
  2. import org.telegram.telegrambots.bots.TelegramLongPollingBot;
  3. import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
  4. import org.telegram.telegrambots.meta.api.objects.Update;
  5. import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
  6.  
  7. import java.io.BufferedWriter;
  8. import java.io.File;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11.  
  12. public class ITTBot extends TelegramLongPollingBot {
  13.     public void onUpdateReceived(Update update) {
  14.         if (!update.hasMessage() && !update.getMessage().hasText()) return;
  15.  
  16.         long chatId = update.getMessage().getChatId();
  17.  
  18.  
  19.         if (update.getMessage().getText().equals("/start")) {
  20.            
  21.             // asking for the nickname
  22.             SendMessage welcomeMex = new SendMessage().setChatId(chatId).setText("Ciao, sono il bot dell'ITT Dorso." + "\n"
  23.                     + "Siccome i tuoi rappresentati d'Istituto rispettano te e la tua privacy, " +
  24.                     "sei pregato/a di inserire un nickname in modo tale da rimanere anonimo/a");
  25.  
  26.             try {
  27.                 execute(welcomeMex);
  28.             } catch (TelegramApiException e) {
  29.                 e.printStackTrace();
  30.             }
  31.            
  32.         }
  33.  
  34.         if (!update.getMessage().getText().startsWith("/")) {
  35.             String username = update.getMessage().getText();
  36.  
  37.             // checking if the nickname already exists
  38.             write:
  39.             try (FileWriter usernameFile = new FileWriter(Main.getUsernameFile(), true)) {
  40.                 if (FileUtils.readFileToString(Main.getUsernameFile()).contains(String.valueOf(chatId))) {
  41.  
  42.                     SendMessage errorName2 = new SendMessage().setChatId(chatId).setText("Hai già uno username. Qual è il problema?");
  43.  
  44.                     execute(errorName2);
  45.  
  46.                     break write;
  47.                 } else if (FileUtils.readFileToString(Main.getUsernameFile()).contains(username)) {
  48.  
  49.                     SendMessage errorName = new SendMessage().setChatId(chatId).setText("Questo username non è disponibile, inseriscine un altro");
  50.  
  51.                     execute(errorName);
  52.  
  53.                     break write;
  54.  
  55.                 } else {
  56.                    
  57.                     // asking for the problem
  58.                    
  59.                     SendMessage problemMex = new SendMessage().setChatId(chatId).setText("Perfetto " + username + ". Qual è il problema?");
  60.  
  61.                     execute(problemMex);
  62.  
  63.                 }
  64.  
  65.                 BufferedWriter bw = new BufferedWriter(usernameFile);
  66.                 bw.write(username + "," + chatId);
  67.                 bw.close();
  68.  
  69.             } catch (IOException | TelegramApiException e) {
  70.                 e.printStackTrace();
  71.             }
  72.            
  73.         }
  74.        
  75.  
  76.     }
  77.  
  78.     public String getBotUsername() {
  79.         return "GUIDODORSO_BOT";
  80.     }
  81.  
  82.     public String getBotToken() {
  83.         return "1005131699:AAH-MAZDIglHvfrNx7Kk_X5w68S2Q1Jps9g";
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement