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_HTML = "HTML";
private static final int COMMAND_COUNT_START = 0;
private static final int COMMAND_START = COMMAND_COUNT_START;
private static final int COMMAND_HELP = COMMAND_START + 1;
private static final int COMMAND_RANKING = COMMAND_HELP + 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_COUNT_START = 0;
private static final int START_MESSAGE = WARNING_COUNT_START;
private static final int HELP_MESSAGE = START_MESSAGE + 1;
private static final int WARNING_UNKNOWN_ERROR = HELP_MESSAGE + 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_NOT_IMPLEMENTED = WARNING_POLE_BLOCKED + 1;
@ -45,14 +39,7 @@ public class Vylbot {
private Map<Long, Chat> chats;
public Vylbot() throws ReadErrorException {
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");
//chats = Persistence.getInstance().readChats();
}
public String getUsername() {
@ -64,7 +51,7 @@ public class Vylbot {
}
public String myName() {
return "Vylbot";
return "Vylionbot";
}
//--- Http handling
@ -130,8 +117,17 @@ public class Vylbot {
.asJson();
}
public HttpResponse<JsonNode> getPokeGoStatus() throws UnirestException {
return Unirest.get("http://pokemongosource.com/is-pokemon-go-down/json")
.asJson();
}
//--- bot thinking
private boolean askPogeGoStatus() throws UnirestException {
return getPokeGoStatus().getBody().getObject().getBoolean("pokemon-go-up");
}
public void run() throws UnirestException {
int last_upd_id = 0;
HttpResponse<JsonNode> response;
@ -156,10 +152,8 @@ public class Vylbot {
}
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
long chat_id = message.getJSONObject("chat").getLong("id");
int message_id = message.getInt("message_id");
String chatType = message.getJSONObject("chat").getString("type");
String name = message.getJSONObject("from").getString("first_name");
@ -176,7 +170,7 @@ public class Vylbot {
user = message.getJSONObject("from").getString("username");
//Console log
boolean full_log = true;
boolean full_log = false;
if (full_log) System.out.println("\n" + message);
else {
if (message.getJSONObject("chat").has("title"))
@ -194,10 +188,7 @@ public class Vylbot {
//Actually read the message
if (message.has("text")) {
String text = message.getString("text");
if (text.startsWith("/") || text.startsWith("!")) handleInput(chat_id, chatType.contains("group"), user, text);
else handleText(chat_id, chatType.contains("group"), user, text);
handleInput(chat_id, chatType.contains("group"), user, text);
return;
}
}
@ -205,62 +196,47 @@ public class Vylbot {
//--- Command handling
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(commands.get(i))) {
if(text.startsWith(commands.get(i) + "@") && !text.startsWith(commands.get(i) + "@" + USERNAME)) return;
if(text.startsWith("/help") &&
(!text.substring("/help".length()).startsWith("@") ||
text.substring("/help".length()).startsWith("@" + USERNAME)))
handleHelp(chat_id);
else {
if(group) handleCommand(chat_id, i, user, text);
else handlePrivateCommand(chat_id, i, user, text);
return;
}
}
}
if(text.startsWith("/pokego") &&
(!text.substring("/pokego".length()).startsWith("@") ||
text.substring("/pokego".length()).startsWith("@" + USERNAME)))
handlePokeGoStatus(chat_id);
}
private void handleCommand(long chat_id, int command, String user, String text) throws UnirestException {
switch (command) {
case COMMAND_START:
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 handleStart(long chat_id) throws UnirestException {
sendMessage(chat_id, "Hola. Soy el bot personal de Vylion");
return;
}
private void handlePrivateCommand(long chat_id, int command, String user, String text) throws UnirestException {
switch (command) {
case COMMAND_START:
sendMessage(chat_id, getInformation(COMMAND_START));
return;
private void handleHelp(long chat_id) throws UnirestException {
String help = "Aquí tienes una lista de comandos que acepto:\n\n" +
"/start - Repite el saludo inicial\n" +
"/help - Saca esta lista\n" +
"/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, getInformation(COMMAND_HELP), PARSE_MARKDOWN);
return;
case COMMAND_TALIBOT_STATS:
case COMMAND_RANKING:
sendMessage(chat_id, getInformation(WARNING_NO_GROUP));
default:
return;
}
sendMessage(chat_id, help, PARSE_MARKDOWN);
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) {
@ -276,21 +252,8 @@ public class Vylbot {
}
}
private String getInformation(int command) {
switch (command) {
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_";
private String getWarning(int warning_id) {
switch (warning_id) {
case WARNING_UNKNOWN_ERROR:
return "Algo ha ido MUY mal.";
@ -312,20 +275,6 @@ public class Vylbot {
}
private String getFormatExample(int command) {
switch (command) {
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.";
}
return "Entrada incorrecta.";
}
}