Added gitignore.

Refactored a little.
This commit is contained in:
vylion 2017-11-23 15:23:48 +01:00
parent 5ea65dfb11
commit dc3f3b8eb7
3 changed files with 43 additions and 34 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.idea/
out/
geiserFiles/

View file

@ -4,6 +4,7 @@ import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException; import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.body.MultipartBody;
import elements.Chat; import elements.Chat;
import elements.Persistence; import elements.Persistence;
import elements.exceptions.*; import elements.exceptions.*;
@ -22,9 +23,8 @@ import java.util.*;
*/ */
public class Geiserbot { public class Geiserbot {
private static final String TOKEN = "267229954:AAHx49MXLmT1nT0QkccrSIzgmRVCQbjbJaQ";
private static final String USERNAME = "gserbot"; 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_MARKDOWN = "Markdown";
private static final String PARSE_HTML = "HTML"; 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_NO_GROUP = WARNING_NOT_IMPLEMENTED + 1;
private static final int WARNING_COUNT_END = WARNING_NO_GROUP + 1; private static final int WARNING_COUNT_END = WARNING_NO_GROUP + 1;
private String token;
//private List<String> commands; //private List<String> commands;
private Map<Long, Chat> chats; private Map<Long, Chat> chats;
public Geiserbot() throws ReadErrorListException, ReadErrorChatException { public Geiserbot(String t) throws ReadErrorListException, ReadErrorChatException {
chats = Persistence.getInstance().readChats(); chats = Persistence.getInstance().readChats();
token = t;
} }
public String getUsername() { public String getUsername() {
return USERNAME; return USERNAME;
} }
public String getUrl() { return BASE_URL + token; }
public String getToken() { public String getToken() {
return TOKEN; return token;
} }
public String myName() { public String myName() {
@ -58,62 +63,56 @@ public class Geiserbot {
//--- Http handling //--- Http handling
public HttpResponse<JsonNode> sendMessage(Long chatId, String text) throws UnirestException { private MultipartBody makeMessage(Long chatId, String text) throws UnirestException {
System.out.println("Sending message: \n" + text + "\n"); System.out.println("Sending message: \n" + text + "\n");
return Unirest.post(BASE_URL + "/sendMessage") return Unirest.post(getUrl() + "/sendMessage")
.field("chat_id", chatId) .field("chat_id", chatId)
.field("text", text) .field("text", text);
.asJson(); }
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<JsonNode> sendMessage(Long chatId, String text) throws UnirestException {
return makeMessage(chatId, text).asJson();
} }
public HttpResponse<JsonNode> sendMessage(Long chatId, String text, String parseMode) throws UnirestException { public HttpResponse<JsonNode> sendMessage(Long chatId, String text, String parseMode) throws UnirestException {
System.out.println("Sending message: \n" + text + "\n"); return makeMessage(chatId, text)
return Unirest.post(BASE_URL + "/sendMessage")
.field("chat_id", chatId)
.field("text", text)
.field("parse_mode", parseMode) .field("parse_mode", parseMode)
.asJson(); .asJson();
} }
public HttpResponse<JsonNode> sendMessage(String channel, String text) throws UnirestException { public HttpResponse<JsonNode> sendMessage(String channel, String text) throws UnirestException {
System.out.println("Sending message to " + channel + ": \n" + text + "\n"); return makeMessage(channel, text).asJson();
return Unirest.post(BASE_URL + "/sendMessage")
.field("chat_id", channel)
.field("text", text)
.asJson();
} }
public HttpResponse<JsonNode> sendMessage(String channel, String text, String parseMode) throws UnirestException { public HttpResponse<JsonNode> sendMessage(String channel, String text, String parseMode) throws UnirestException {
System.out.println("Sending message to " + channel + ": \n" + text + "\n"); return makeMessage(channel, text)
return Unirest.post(BASE_URL + "/sendMessage")
.field("chat_id", channel)
.field("text", text)
.field("parse_mode", parseMode) .field("parse_mode", parseMode)
.asJson(); .asJson();
} }
public HttpResponse<JsonNode> replyMessage(Long chatId, Integer repliedMessage, String text) throws UnirestException { public HttpResponse<JsonNode> replyMessage(Long chatId, String text, Integer repliedMessage) throws UnirestException {
System.out.println("Sending message: \n" + text + "\n"); return makeMessage(chatId, text)
return Unirest.post(BASE_URL + "/sendMessage")
.field("chat_id", chatId)
.field("text", text)
.field("reply_to_message_id", repliedMessage) .field("reply_to_message_id", repliedMessage)
.asJson(); .asJson();
} }
public HttpResponse<JsonNode> getUpdates(Integer offset) throws UnirestException { public HttpResponse<JsonNode> getUpdates(Integer offset) throws UnirestException {
return Unirest.post(BASE_URL + "/getUpdates") return Unirest.post(getUrl() + "/getUpdates")
.field("offset", offset) .field("offset", offset)
.asJson(); .asJson();
} }
public HttpResponse<JsonNode> getUpdates(Integer offset, Integer timeout) throws UnirestException { public HttpResponse<JsonNode> getUpdates(Integer offset, Integer timeout) throws UnirestException {
return Unirest.post(BASE_URL + "/getUpdates") return Unirest.post(getUrl() + "/getUpdates")
.field("offset", offset) .field("offset", offset)
.field("timeout", timeout) .field("timeout", timeout)
.asJson(); .asJson();
@ -312,7 +311,7 @@ public class Geiserbot {
s+= "Tu mensaje tiene hora de " + m.getTimestamp() + "\n"; 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 { private void handleGroupEvent(GroupMessage m) throws NoTextMessageException, UnirestException {

View file

@ -10,11 +10,13 @@ import elements.exceptions.ReadErrorListException;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
Geiserbot bot = new Geiserbot(); Geiserbot bot = new Geiserbot(args[0]);
System.out.println("The Bot @" + bot.getUsername() + " awakens."); System.out.println("The Bot @" + bot.getUsername() + " awakens.");
System.out.println("Bot running."); System.out.println("Bot running.");
bot.run(); bot.run();
} catch (ArrayIndexOutOfBoundsException e) {
usage(args);
} catch (UnirestException e) { } catch (UnirestException e) {
System.out.println("Unirest Error catched."); System.out.println("Unirest Error catched.");
e.printStackTrace(); e.printStackTrace();
@ -23,4 +25,9 @@ public class Main {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void usage(String[] args) {
System.out.println("Usage: " + "" + " TOKEN");
System.out.println("TOKEN: The Telegram bot API token");
}
} }