From dc3f3b8eb7b5d3736a54c79ba8e5d7b692065ab1 Mon Sep 17 00:00:00 2001 From: vylion Date: Thu, 23 Nov 2017 15:23:48 +0100 Subject: [PATCH] Added gitignore. Refactored a little. --- .gitignore | 3 ++ src/main/Geiserbot.java | 65 ++++++++++++++++++++--------------------- src/main/Main.java | 9 +++++- 3 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..379ebd8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +out/ +geiserFiles/ \ No newline at end of file diff --git a/src/main/Geiserbot.java b/src/main/Geiserbot.java index aa6a784..44c5716 100644 --- a/src/main/Geiserbot.java +++ b/src/main/Geiserbot.java @@ -4,6 +4,7 @@ import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.exceptions.UnirestException; +import com.mashape.unirest.request.body.MultipartBody; import elements.Chat; import elements.Persistence; import elements.exceptions.*; @@ -22,9 +23,8 @@ import java.util.*; */ public class Geiserbot { - private static final String TOKEN = "267229954:AAHx49MXLmT1nT0QkccrSIzgmRVCQbjbJaQ"; private static final String USERNAME = "gserbot"; - private static final String BASE_URL = "https://api.telegram.org/bot" + TOKEN; + private static final String BASE_URL = "https://api.telegram.org/bot"; private static final String PARSE_MARKDOWN = "Markdown"; private static final String PARSE_HTML = "HTML"; @@ -37,19 +37,24 @@ public class Geiserbot { private static final int WARNING_NO_GROUP = WARNING_NOT_IMPLEMENTED + 1; private static final int WARNING_COUNT_END = WARNING_NO_GROUP + 1; + private String token; + //private List commands; private Map chats; - public Geiserbot() throws ReadErrorListException, ReadErrorChatException { + public Geiserbot(String t) throws ReadErrorListException, ReadErrorChatException { chats = Persistence.getInstance().readChats(); + token = t; } public String getUsername() { return USERNAME; } + public String getUrl() { return BASE_URL + token; } + public String getToken() { - return TOKEN; + return token; } public String myName() { @@ -58,62 +63,56 @@ public class Geiserbot { //--- Http handling - public HttpResponse sendMessage(Long chatId, String text) throws UnirestException { + private MultipartBody makeMessage(Long chatId, String text) throws UnirestException { System.out.println("Sending message: \n" + text + "\n"); - return Unirest.post(BASE_URL + "/sendMessage") + return Unirest.post(getUrl() + "/sendMessage") .field("chat_id", chatId) - .field("text", text) - .asJson(); + .field("text", text); + } + + private MultipartBody makeMessage(String channel, String text) throws UnirestException { + System.out.println("Sending message to " + channel + ": \n" + text + "\n"); + + return Unirest.post(getUrl() + "/sendMessage") + .field("chat_id", channel) + .field("text", text); + } + + public HttpResponse sendMessage(Long chatId, String text) throws UnirestException { + return makeMessage(chatId, text).asJson(); } public HttpResponse sendMessage(Long chatId, String text, String parseMode) throws UnirestException { - System.out.println("Sending message: \n" + text + "\n"); - - return Unirest.post(BASE_URL + "/sendMessage") - .field("chat_id", chatId) - .field("text", text) + return makeMessage(chatId, text) .field("parse_mode", parseMode) .asJson(); } public HttpResponse sendMessage(String channel, String text) throws UnirestException { - System.out.println("Sending message to " + channel + ": \n" + text + "\n"); - - return Unirest.post(BASE_URL + "/sendMessage") - .field("chat_id", channel) - .field("text", text) - .asJson(); + return makeMessage(channel, text).asJson(); } public HttpResponse sendMessage(String channel, String text, String parseMode) throws UnirestException { - System.out.println("Sending message to " + channel + ": \n" + text + "\n"); - - return Unirest.post(BASE_URL + "/sendMessage") - .field("chat_id", channel) - .field("text", text) + return makeMessage(channel, text) .field("parse_mode", parseMode) .asJson(); } - public HttpResponse replyMessage(Long chatId, Integer repliedMessage, String text) throws UnirestException { - System.out.println("Sending message: \n" + text + "\n"); - - return Unirest.post(BASE_URL + "/sendMessage") - .field("chat_id", chatId) - .field("text", text) + public HttpResponse replyMessage(Long chatId, String text, Integer repliedMessage) throws UnirestException { + return makeMessage(chatId, text) .field("reply_to_message_id", repliedMessage) .asJson(); } public HttpResponse getUpdates(Integer offset) throws UnirestException { - return Unirest.post(BASE_URL + "/getUpdates") + return Unirest.post(getUrl() + "/getUpdates") .field("offset", offset) .asJson(); } public HttpResponse getUpdates(Integer offset, Integer timeout) throws UnirestException { - return Unirest.post(BASE_URL + "/getUpdates") + return Unirest.post(getUrl() + "/getUpdates") .field("offset", offset) .field("timeout", timeout) .asJson(); @@ -312,7 +311,7 @@ public class Geiserbot { s+= "Tu mensaje tiene hora de " + m.getTimestamp() + "\n"; - replyMessage(m.getCid(), m.getMid(), s.trim()); + replyMessage(m.getCid(), s.trim(), m.getMid()); } private void handleGroupEvent(GroupMessage m) throws NoTextMessageException, UnirestException { diff --git a/src/main/Main.java b/src/main/Main.java index b4eda92..e849877 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -10,11 +10,13 @@ import elements.exceptions.ReadErrorListException; public class Main { public static void main(String[] args) { try { - Geiserbot bot = new Geiserbot(); + Geiserbot bot = new Geiserbot(args[0]); System.out.println("The Bot @" + bot.getUsername() + " awakens."); System.out.println("Bot running."); bot.run(); + } catch (ArrayIndexOutOfBoundsException e) { + usage(args); } catch (UnirestException e) { System.out.println("Unirest Error catched."); e.printStackTrace(); @@ -23,4 +25,9 @@ public class Main { e.printStackTrace(); } } + + public static void usage(String[] args) { + System.out.println("Usage: " + "" + " TOKEN"); + System.out.println("TOKEN: The Telegram bot API token"); + } }