Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.teateam.Chat;
- import org.teateam.Main;
- import org.teateam.TelegramApi;
- import org.teateam.User;
- import org.telegram.telegrambots.meta.TelegramBotsApi;
- import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
- public class Core extends Thread{
- @Override
- public void run(){
- System.out.println("Chat flow started");
- // Telegram startup section
- Main.telegramApi = new TelegramApi();
- TelegramBotsApi botsApi = new TelegramBotsApi();
- try {
- botsApi.registerBot(Main.telegramApi);
- }
- catch (TelegramApiException ex){
- ex.printStackTrace();
- }
- // End section
- }
- public static void validateText(String text, long from_id) {
- User user = Main.users.get(from_id);
- if(user == null){
- // If not online
- user = new User(from_id);
- if(user.isRegister()) {
- Main.telegramApi.sendTextMessage(from_id, "Вы зарегистрированы. Nickname: " + user.nickname + ". Tid: " + user.tid);
- }
- else{
- Main.telegramApi.sendTextMessage(from_id, "Вы не зарегистрированы");
- }
- }
- else{
- Main.telegramApi.sendTextMessage(from_id, "Your nick is " + user.nickname);
- }
- }
- }
- package org.teateam;
- import org.teateam.Chat.Core;
- 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;
- public class TelegramApi extends TelegramLongPollingBot {
- private SendMessage message = new SendMessage();
- public TelegramApi(){
- System.out.println("Bot has been started success");
- }
- public void sendTextMessage(long peer_id, String text){
- this.message.setChatId(peer_id).setText(text);
- try {
- execute(this.message);
- }
- catch (TelegramApiException tEx){
- System.out.println("Exception in org.teateam.TelegramApi: " + tEx);
- }
- }
- @Override
- public void onUpdateReceived(Update update) {
- if(update.hasMessage() && update.getMessage().hasText()){
- // If text
- Core.validateText(update.getMessage().getText(), update.getMessage().getChatId());
- System.out.println(" ");
- System.out.println("New message: " + update.getMessage().getText());
- System.out.println("From ID: " + update.getMessage().getChatId());
- System.gc();
- }
- }
- @Override
- public String getBotUsername() {
- return Main.getBotUsername();
- }
- @Override
- public String getBotToken() {
- return Main.getAccess_token();
- }
- }
- package org.teateam;
- import java.util.ArrayList;
- public class User {
- public long tid;
- public String nickname;
- public int accessLevel;
- public int id;
- public boolean online;
- public int room;
- public User(long tid){
- // Add data from database
- String query = "SELECT * FROM `users` WHERE `tid` = '" + tid + "' LIMIT 1;";
- ArrayList<String> result = MySQL.select(query);
- if(!result.isEmpty()) {
- this.tid = Long.parseLong(result.get(0));
- this.nickname = result.get(1);
- this.accessLevel = Integer.parseInt(result.get(2));
- this.id = Integer.parseInt(result.get(3));
- this.online = false;
- this.room = 0;
- }
- }
- public boolean isRegister(){
- return this.nickname != null;
- }
- }
- package org.teateam;
- import java.sql.*;
- import java.util.ArrayList;
- public class MySQL {
- private static final String host = "";
- private static final int port = 3306;
- private static final String user = "";
- private static final String password = "";
- private static final String database = "";
- private static final String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
- private static Connection connection;
- private static Statement statement;
- private static ResultSet resultSet;
- private static ResultSetMetaData resultSetMetaData;
- private static ArrayList<String> result = new ArrayList<>();
- /**
- *
- * @param query For example: SELECT `login` FROM `users` WHERE `id` = 1 LIMIT 1;
- * @return ArrayList<String>
- */
- public static ArrayList<String> select(String query){
- try{
- connection = DriverManager.getConnection(url, user, password);
- statement = connection.createStatement();
- resultSet = statement.executeQuery(query);
- resultSetMetaData = resultSet.getMetaData();
- try {
- while (resultSet.next()) {
- for(int i = 0; i < resultSetMetaData.getColumnCount(); i++){
- result.add(resultSet.getString(i + 1 ));
- System.out.println(resultSet.getString(i + 1));
- }
- }
- }
- catch (SQLException sqlEx){
- sqlEx.printStackTrace();
- return result;
- }
- }
- catch (SQLException sqlEx){
- sqlEx.printStackTrace();
- }
- finally {
- try{ connection.close(); } catch (SQLException se) { /* TODO anything */ se.printStackTrace();}
- try{ statement.close(); } catch (SQLException se) { /* TODO anything */ se.printStackTrace();}
- try{ resultSet.close(); } catch (SQLException se) { /* TODO anything */ se.printStackTrace();}
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement