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.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<String> commands;
private Map<Long, Chat> 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<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");
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<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 {
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<JsonNode> 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<JsonNode> 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<JsonNode> 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<JsonNode> replyMessage(Long chatId, String text, Integer repliedMessage) throws UnirestException {
return makeMessage(chatId, text)
.field("reply_to_message_id", repliedMessage)
.asJson();
}
public HttpResponse<JsonNode> getUpdates(Integer offset) throws UnirestException {
return Unirest.post(BASE_URL + "/getUpdates")
return Unirest.post(getUrl() + "/getUpdates")
.field("offset", offset)
.asJson();
}
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("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 {

View file

@ -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");
}
}