Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. package org.teateam.Chat;
  2.  
  3. import org.teateam.Main;
  4. import org.teateam.TelegramApi;
  5. import org.teateam.User;
  6. import org.telegram.telegrambots.meta.TelegramBotsApi;
  7. import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
  8.  
  9. public class Core extends Thread{
  10.  
  11. @Override
  12. public void run(){
  13. System.out.println("Chat flow started");
  14.  
  15. // Telegram startup section
  16. Main.telegramApi = new TelegramApi();
  17.  
  18. TelegramBotsApi botsApi = new TelegramBotsApi();
  19.  
  20. try {
  21. botsApi.registerBot(Main.telegramApi);
  22. }
  23. catch (TelegramApiException ex){
  24. ex.printStackTrace();
  25. }
  26. // End section
  27. }
  28.  
  29. public static void validateText(String text, long from_id) {
  30. User user = Main.users.get(from_id);
  31. if(user == null){
  32. // If not online
  33. user = new User(from_id);
  34. if(user.isRegister()) {
  35. Main.telegramApi.sendTextMessage(from_id, "Вы зарегистрированы. Nickname: " + user.nickname + ". Tid: " + user.tid);
  36. }
  37. else{
  38. Main.telegramApi.sendTextMessage(from_id, "Вы не зарегистрированы");
  39. }
  40. }
  41. else{
  42. Main.telegramApi.sendTextMessage(from_id, "Your nick is " + user.nickname);
  43. }
  44. }
  45. }
  46.  
  47. package org.teateam;
  48.  
  49. import org.teateam.Chat.Core;
  50. import org.telegram.telegrambots.bots.TelegramLongPollingBot;
  51. import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
  52. import org.telegram.telegrambots.meta.api.objects.Update;
  53. import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
  54.  
  55. public class TelegramApi extends TelegramLongPollingBot {
  56. private SendMessage message = new SendMessage();
  57.  
  58. public TelegramApi(){
  59. System.out.println("Bot has been started success");
  60. }
  61.  
  62. public void sendTextMessage(long peer_id, String text){
  63. this.message.setChatId(peer_id).setText(text);
  64. try {
  65. execute(this.message);
  66. }
  67. catch (TelegramApiException tEx){
  68. System.out.println("Exception in org.teateam.TelegramApi: " + tEx);
  69. }
  70. }
  71.  
  72. @Override
  73. public void onUpdateReceived(Update update) {
  74. if(update.hasMessage() && update.getMessage().hasText()){
  75. // If text
  76. Core.validateText(update.getMessage().getText(), update.getMessage().getChatId());
  77. System.out.println(" ");
  78. System.out.println("New message: " + update.getMessage().getText());
  79. System.out.println("From ID: " + update.getMessage().getChatId());
  80. System.gc();
  81. }
  82. }
  83.  
  84. @Override
  85. public String getBotUsername() {
  86. return Main.getBotUsername();
  87. }
  88.  
  89. @Override
  90. public String getBotToken() {
  91. return Main.getAccess_token();
  92. }
  93.  
  94. }
  95.  
  96. package org.teateam;
  97.  
  98. import java.util.ArrayList;
  99.  
  100. public class User {
  101. public long tid;
  102. public String nickname;
  103. public int accessLevel;
  104. public int id;
  105. public boolean online;
  106. public int room;
  107.  
  108. public User(long tid){
  109. // Add data from database
  110. String query = "SELECT * FROM `users` WHERE `tid` = '" + tid + "' LIMIT 1;";
  111. ArrayList<String> result = MySQL.select(query);
  112. if(!result.isEmpty()) {
  113. this.tid = Long.parseLong(result.get(0));
  114. this.nickname = result.get(1);
  115. this.accessLevel = Integer.parseInt(result.get(2));
  116. this.id = Integer.parseInt(result.get(3));
  117. this.online = false;
  118. this.room = 0;
  119. }
  120. }
  121.  
  122. public boolean isRegister(){
  123. return this.nickname != null;
  124. }
  125. }
  126.  
  127. package org.teateam;
  128.  
  129. import java.sql.*;
  130. import java.util.ArrayList;
  131.  
  132. public class MySQL {
  133. private static final String host = "";
  134. private static final int port = 3306;
  135. private static final String user = "";
  136. private static final String password = "";
  137. private static final String database = "";
  138. private static final String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  139.  
  140. private static Connection connection;
  141. private static Statement statement;
  142. private static ResultSet resultSet;
  143. private static ResultSetMetaData resultSetMetaData;
  144.  
  145. private static ArrayList<String> result = new ArrayList<>();
  146.  
  147. /**
  148. *
  149. * @param query For example: SELECT `login` FROM `users` WHERE `id` = 1 LIMIT 1;
  150. * @return ArrayList<String>
  151. */
  152. public static ArrayList<String> select(String query){
  153. try{
  154. connection = DriverManager.getConnection(url, user, password);
  155. statement = connection.createStatement();
  156. resultSet = statement.executeQuery(query);
  157. resultSetMetaData = resultSet.getMetaData();
  158.  
  159. try {
  160. while (resultSet.next()) {
  161. for(int i = 0; i < resultSetMetaData.getColumnCount(); i++){
  162. result.add(resultSet.getString(i + 1 ));
  163. System.out.println(resultSet.getString(i + 1));
  164. }
  165. }
  166. }
  167. catch (SQLException sqlEx){
  168. sqlEx.printStackTrace();
  169. return result;
  170. }
  171. }
  172. catch (SQLException sqlEx){
  173. sqlEx.printStackTrace();
  174. }
  175. finally {
  176. try{ connection.close(); } catch (SQLException se) { /* TODO anything */ se.printStackTrace();}
  177. try{ statement.close(); } catch (SQLException se) { /* TODO anything */ se.printStackTrace();}
  178. try{ resultSet.close(); } catch (SQLException se) { /* TODO anything */ se.printStackTrace();}
  179. }
  180.  
  181. return result;
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement