Advertisement
Guest User

BotTelegram

a guest
May 9th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.33 KB | None | 0 0
  1. package com.Bot;
  2.  
  3.         import org.json.JSONArray;
  4.         import org.json.JSONObject;
  5.  
  6.         import com.mashape.unirest.http.HttpResponse;
  7.         import com.mashape.unirest.http.JsonNode;
  8.         import com.mashape.unirest.http.Unirest;
  9.         import com.mashape.unirest.http.exceptions.UnirestException;
  10.  
  11.         import java.sql.Connection;
  12.         import java.sql.DriverManager;
  13.         import java.sql.ResultSet;
  14.         import java.sql.SQLException;
  15.         import java.sql.Statement;
  16.         import java.util.ArrayList;
  17.  
  18. public class Main {
  19.     private String BASE_URL = "https://api.telegram.org/bot<Your Token there>/";
  20.     // You should take your token from @BotFather. Read more on telegram bots API
  21.     private String POLLING_URL = BASE_URL + "getUpdates";
  22.     private String SENDMESSAGE_URL = BASE_URL + "sendMessage";
  23.  
  24.     private Connection connection = null;
  25.     private Statement statmt;
  26.     private ResultSet resSet;
  27.     private int lines = 0;
  28.     private int lineCount = 0;
  29.     private int chars = 0;
  30.     private int eff = 0;
  31.  
  32.     private  Main() {
  33.         try {
  34.             Class.forName("org.sqlite.JDBC");
  35.             connection = DriverManager.getConnection("jdbc:sqlite:res/db.s3db");
  36.         } catch (ClassNotFoundException e) {
  37.             e.printStackTrace();
  38.         } catch (SQLException e) {
  39.             e.printStackTrace();
  40.         }
  41.         try {
  42.             run();
  43.         } catch (UnirestException e) {
  44.             e.printStackTrace();
  45.         } catch (Exception e) {
  46.             e.printStackTrace();
  47.         }
  48.     }
  49.     public HttpResponse<JsonNode> sendMessage(Integer chatId, String text) throws UnirestException {
  50.         return Unirest
  51.                 .post(SENDMESSAGE_URL)
  52.                 .field("chat_id", chatId)
  53.                 .field("text", text).asJson();
  54.     }
  55.     public HttpResponse<JsonNode> getUpdates(Integer offset) throws UnirestException {
  56.         return Unirest
  57.                 .post(POLLING_URL)
  58.                 .field("offset", offset)
  59.                 .asJson();
  60.     }
  61.     private void run() throws Exception {
  62.         int last_update_id = 0;
  63.         HttpResponse<JsonNode> response;
  64.         statmt = connection.createStatement();
  65.         statmt.execute("CREATE TABLE if not exists 'users' ('id' INTEGER PRIMARY KEY AUTOINCREMENT," +
  66.                 "'first_name' TEXT, 'last_name' TEXT,'chat_id' INT, 'user_id' INT, 'lines' INT," +
  67.                 "'chars' INT, 'eff' INT);");
  68.         boolean check = false; // Check for emptyness of dataset. Maybe SQL can provide this check? Dunno. In any case, eto kostyl
  69.         resSet = statmt.executeQuery("SELECT * from users");
  70.         if (resSet.next()){
  71.             check = true;
  72.         }
  73.         while (true) {
  74.             response = getUpdates(last_update_id++);
  75.             if (response.getStatus() == 200) {
  76.                 JSONArray responses = response
  77.                         .getBody()
  78.                         .getObject()
  79.                         .getJSONArray("result");
  80.                 if (responses.isNull(0)) {
  81.                     continue;
  82.                 } else {
  83.                     last_update_id = responses
  84.                             .getJSONObject(responses.length() - 1)
  85.                             .getInt("update_id") + 1;
  86.                 }
  87.                 for (int i = 0; i < responses.length(); i++) {
  88.                     JSONObject message = responses
  89.                             .getJSONObject(i)
  90.                             .getJSONObject("message");
  91.                     String first_name = "";
  92.                     if (message.getJSONObject("from").has("first_name")) {
  93.                         first_name = message
  94.                                 .getJSONObject("from")
  95.                                 .getString("first_name");
  96.                         first_name = first_name.replace('\"', ' ').replace('\'', ' ').replace(';', ' ');
  97.                     }
  98.                     String last_name = "";
  99.                     if (message.getJSONObject("from").has("last_name")) {
  100.                         last_name = message
  101.                                 .getJSONObject("from")
  102.                                 .getString("last_name");
  103.                         last_name = last_name.replace('\"', ' ').replace('\'', ' ').replace(';', ' ');
  104.                     }
  105.                     int chat_id = message
  106.                             .getJSONObject("chat")
  107.                             .getInt("id");
  108.                     int user_id = message
  109.                             .getJSONObject("from")
  110.                             .getInt("id");
  111.                     if (message.has("text") && chat_id < 0) {
  112.                         System.out.println(first_name + ' ' + last_name);
  113.                         String text = message.getString("text");
  114.                         if (text.contains("\n")) lineCount++;
  115.                         resSet = statmt.executeQuery("SELECT first_name, last_name chat_id, user_id," +
  116.                                 "lines, chars, eff FROM users where chat_id = "
  117.                                 + chat_id + " and user_id = " + user_id + ";");
  118.                         if (resSet.next() && check == true) {
  119.                             lines = resSet.getInt("lines");
  120.                             chars = resSet.getInt("chars");
  121.                             eff = Math.round(chars/lines);
  122.                             statmt.execute("UPDATE users set first_name = '" + first_name + "'," +
  123.                                     "last_name = '" + last_name + "', lines = "
  124.                                     + (lines = lines + 1 + lineCount) + ", chars = " + (chars + text.length()) + "," +
  125.                                     "eff = " + eff + " where chat_id = " + chat_id + " and user_id = "
  126.                                     + user_id + ";");
  127.                         } else {
  128.                             statmt.execute("INSERT into 'users' ('first_name', 'last_name', 'chat_id'," +
  129.                                     " 'user_id', 'lines', 'chars', 'eff') values ('" + first_name + "', '"
  130.                                     + last_name + "', '" + (chat_id) + "', '" + user_id + "', '" + 1
  131.                                     + lineCount + "',  '" + text.length() + "', '" + ((1 + lineCount)/text.length()) + "');");
  132.                             check = true;
  133.  
  134.                         }
  135.                         System.out.print(chat_id + "|" + first_name + " " + last_name + ": " + text + " {"); // Monitoring messages from command line
  136.                         System.out.print(lines + "/" + chars + "=" + eff + "}\n");
  137.                         if (text.startsWith("/mystats")) {
  138.                             sendMessage(chat_id, "Your statistics:\n" +
  139.                                     "Lines|Chars|Eff\n" +
  140.                                     lines + "|" + chars + "|" + eff);
  141.                         }
  142.                         if (text.startsWith("/showtop")) {
  143.                             resSet = statmt.executeQuery("SELECT * FROM users where chat_id = " + chat_id + " order by eff desc;");
  144.                             ArrayList<String> temp1 = new ArrayList<String>();
  145.                             while (resSet.next()) {
  146.                                 temp1.add(resSet.getString("first_name") + " " + resSet.getString("last_name")
  147.                                         + "|" + resSet.getInt("lines") + "|" + resSet.getInt("chars") + "|"
  148.                                         + resSet.getInt("eff"));
  149.                             }
  150.                             String temp = "";
  151.                             for (int h = 0; h < temp1.size(); h++) {
  152.                                 temp = temp + temp1.get(h) + "\n";
  153.                             }
  154.                             sendMessage(chat_id, "Efficiency top for this channel:\n" +
  155.                                     "Name|Lines|Chars|Eff\n" + temp);
  156.  
  157.                         }
  158.                         if (text.startsWith("/help")) {
  159.                             sendMessage(chat_id, "/showtop - Shows top chat members on this channel by efficiency" +
  160.                                     "/mystats - Shows only yours chat statistics on this channel");
  161.                         }
  162.                         lineCount = 0;
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.     }
  168.     public static void main(String args[]) {
  169.         new Main();
  170.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement