Restarted the project.

Added Pokemon Go status check.
This commit is contained in:
Vylion 2016-07-09 10:06:16 +02:00
parent a2b32ae61c
commit 5094a6a1e2

View file

@ -25,16 +25,10 @@ public class Vylbot {
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";
private static final int COMMAND_COUNT_START = 0; private static final int WARNING_COUNT_START = 0;
private static final int COMMAND_START = COMMAND_COUNT_START; private static final int START_MESSAGE = WARNING_COUNT_START;
private static final int COMMAND_HELP = COMMAND_START + 1; private static final int HELP_MESSAGE = START_MESSAGE + 1;
private static final int COMMAND_RANKING = COMMAND_HELP + 1; private static final int WARNING_UNKNOWN_ERROR = HELP_MESSAGE + 1;
private static final int COMMAND_TALIBOT_STATS = COMMAND_RANKING + 1;
private static final int COMMAND_POLE = COMMAND_TALIBOT_STATS + 1;
private static final int COMMAND_COUNT_END = COMMAND_POLE + 1;
private static final int WARNING_COUNT_START = COMMAND_COUNT_END;
private static final int WARNING_UNKNOWN_ERROR = WARNING_COUNT_START;
private static final int WARNING_NO_USERNAME = WARNING_UNKNOWN_ERROR + 1; private static final int WARNING_NO_USERNAME = WARNING_UNKNOWN_ERROR + 1;
private static final int WARNING_POLE_BLOCKED = WARNING_NO_USERNAME + 1; private static final int WARNING_POLE_BLOCKED = WARNING_NO_USERNAME + 1;
private static final int WARNING_NOT_IMPLEMENTED = WARNING_POLE_BLOCKED + 1; private static final int WARNING_NOT_IMPLEMENTED = WARNING_POLE_BLOCKED + 1;
@ -45,14 +39,7 @@ public class Vylbot {
private Map<Long, Chat> chats; private Map<Long, Chat> chats;
public Vylbot() throws ReadErrorException { public Vylbot() throws ReadErrorException {
chats = Persistence.getInstance().readChats(); //chats = Persistence.getInstance().readChats();
commands = new ArrayList<>();
commands.add(COMMAND_START, "/start");
commands.add(COMMAND_HELP, "/help");
commands.add(COMMAND_RANKING, "/ranking");
commands.add(COMMAND_TALIBOT_STATS, "!pole stats");
commands.add(COMMAND_POLE, "/pole");
} }
public String getUsername() { public String getUsername() {
@ -64,7 +51,7 @@ public class Vylbot {
} }
public String myName() { public String myName() {
return "Vylbot"; return "Vylionbot";
} }
//--- Http handling //--- Http handling
@ -130,8 +117,17 @@ public class Vylbot {
.asJson(); .asJson();
} }
public HttpResponse<JsonNode> getPokeGoStatus() throws UnirestException {
return Unirest.get("http://pokemongosource.com/is-pokemon-go-down/json")
.asJson();
}
//--- bot thinking //--- bot thinking
private boolean askPogeGoStatus() throws UnirestException {
return getPokeGoStatus().getBody().getObject().getBoolean("pokemon-go-up");
}
public void run() throws UnirestException { public void run() throws UnirestException {
int last_upd_id = 0; int last_upd_id = 0;
HttpResponse<JsonNode> response; HttpResponse<JsonNode> response;
@ -156,10 +152,8 @@ public class Vylbot {
} }
private void processMessage(JSONObject message) throws UnirestException { private void processMessage(JSONObject message) throws UnirestException {
//Get chat id, the most important thing
long chat_id = message.getJSONObject("chat").getLong("id");
//Get basic compulsory variables //Get basic compulsory variables
long chat_id = message.getJSONObject("chat").getLong("id");
int message_id = message.getInt("message_id"); int message_id = message.getInt("message_id");
String chatType = message.getJSONObject("chat").getString("type"); String chatType = message.getJSONObject("chat").getString("type");
String name = message.getJSONObject("from").getString("first_name"); String name = message.getJSONObject("from").getString("first_name");
@ -176,7 +170,7 @@ public class Vylbot {
user = message.getJSONObject("from").getString("username"); user = message.getJSONObject("from").getString("username");
//Console log //Console log
boolean full_log = true; boolean full_log = false;
if (full_log) System.out.println("\n" + message); if (full_log) System.out.println("\n" + message);
else { else {
if (message.getJSONObject("chat").has("title")) if (message.getJSONObject("chat").has("title"))
@ -194,10 +188,7 @@ public class Vylbot {
//Actually read the message //Actually read the message
if (message.has("text")) { if (message.has("text")) {
String text = message.getString("text"); String text = message.getString("text");
handleInput(chat_id, chatType.contains("group"), user, text);
if (text.startsWith("/") || text.startsWith("!")) handleInput(chat_id, chatType.contains("group"), user, text);
else handleText(chat_id, chatType.contains("group"), user, text);
return; return;
} }
} }
@ -205,62 +196,47 @@ public class Vylbot {
//--- Command handling //--- Command handling
private void handleInput(long chat_id, boolean group, String user, String text) throws UnirestException { private void handleInput(long chat_id, boolean group, String user, String text) throws UnirestException {
if(text.startsWith("/start") &&
(!text.substring("/start".length()).startsWith("@") ||
text.substring("/start".length()).startsWith("@" + USERNAME)))
handleStart(chat_id);
for(int i = COMMAND_COUNT_START; i < COMMAND_COUNT_END; i++) { if(text.startsWith("/help") &&
if (text.startsWith(commands.get(i))) { (!text.substring("/help".length()).startsWith("@") ||
if(text.startsWith(commands.get(i) + "@") && !text.startsWith(commands.get(i) + "@" + USERNAME)) return; text.substring("/help".length()).startsWith("@" + USERNAME)))
handleHelp(chat_id);
else { if(text.startsWith("/pokego") &&
if(group) handleCommand(chat_id, i, user, text); (!text.substring("/pokego".length()).startsWith("@") ||
else handlePrivateCommand(chat_id, i, user, text); text.substring("/pokego".length()).startsWith("@" + USERNAME)))
return; handlePokeGoStatus(chat_id);
}
}
}
} }
private void handleCommand(long chat_id, int command, String user, String text) throws UnirestException { private void handleStart(long chat_id) throws UnirestException {
switch (command) { sendMessage(chat_id, "Hola. Soy el bot personal de Vylion");
case COMMAND_START: return;
sendMessage(chat_id, getInformation(COMMAND_START));
return;
case COMMAND_HELP:
sendMessage(chat_id, getInformation(COMMAND_HELP), PARSE_MARKDOWN);
return;
case COMMAND_TALIBOT_STATS:
case COMMAND_RANKING:
if(!chats.containsKey(chat_id)) sendMessage(chat_id, "Nadie ha conseguido ninguna pole " +
"en este grupo aún.");
else sendMessage(chat_id, chats.get(chat_id).getRanking());
return;
default: return;
}
} }
private void handlePrivateCommand(long chat_id, int command, String user, String text) throws UnirestException { private void handleHelp(long chat_id) throws UnirestException {
switch (command) { String help = "Aquí tienes una lista de comandos que acepto:\n\n" +
case COMMAND_START: "/start - Repite el saludo inicial\n" +
sendMessage(chat_id, getInformation(COMMAND_START)); "/help - Saca esta lista\n" +
return; "/pokego - Muestra el status de los servidores de PokeGo " +
"(según una web de terceros).\n" +
"\n_Vylbot v1.1.0_";
case COMMAND_HELP: sendMessage(chat_id, help, PARSE_MARKDOWN);
sendMessage(chat_id, getInformation(COMMAND_HELP), PARSE_MARKDOWN); return;
return;
case COMMAND_TALIBOT_STATS:
case COMMAND_RANKING:
sendMessage(chat_id, getInformation(WARNING_NO_GROUP));
default:
return;
}
} }
private void handleText(long chat_id, boolean group, String user, String text) throws UnirestException { private void handlePokeGoStatus(long chat_id) throws UnirestException {
String status = "Según mis fuentes, ";
if(askPogeGoStatus())
status += "Pokémon Go funciona";
else
status += "los pokeservidores tienen un pokeproblema";
sendMessage(chat_id, status);
} }
private boolean exists(long chat_id, String user) { private boolean exists(long chat_id, String user) {
@ -276,21 +252,8 @@ public class Vylbot {
} }
} }
private String getInformation(int command) { private String getWarning(int warning_id) {
switch (command) { switch (warning_id) {
case COMMAND_START:
return "Hola. Soy el bot personal de Vylion.";
case COMMAND_HELP:
return "Aquí tienes una lista de comandos que acepto:\n\n" +
"/start - Repite el saludo inicial\n" +
"/help - Saca esta lista\n" +
"/ranking - Muestra la lista de poles conseguidas." +
"\n\n*Golsario:*\n" +
"Poles: sólo se puede conseguir una por chat al día. " +
"Que gane el mejor." +
"\n\n_Vylbot v1.0.4_";
case WARNING_UNKNOWN_ERROR: case WARNING_UNKNOWN_ERROR:
return "Algo ha ido MUY mal."; return "Algo ha ido MUY mal.";
@ -312,20 +275,6 @@ public class Vylbot {
} }
private String getFormatExample(int command) { private String getFormatExample(int command) {
switch (command) { return "Entrada incorrecta.";
case COMMAND_START:
return commands.get(COMMAND_START);
case COMMAND_HELP:
return commands.get(COMMAND_HELP);
case COMMAND_RANKING:
return commands.get(COMMAND_RANKING);
case COMMAND_TALIBOT_STATS:
return commands.get(COMMAND_TALIBOT_STATS);
default: return "Entrada incorrecta.";
}
} }
} }