Copied whoami command from the testbot.

This commit is contained in:
Vylion 2016-07-09 10:34:20 +02:00
parent 5094a6a1e2
commit 19e0a1d0f2

View file

@ -12,6 +12,7 @@ import elements.exceptions.SaveErrorException;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.time.LocalDate;
import java.util.*; import java.util.*;
/** /**
@ -157,9 +158,11 @@ public class Vylbot {
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");
boolean isGroup = chatType.contains("group");
//Declare basic optional variables //Declare basic optional variables
String user = null; String user = null;
String chatName = null;
//Check for user last name //Check for user last name
if (message.getJSONObject("from").has("last_name")) if (message.getJSONObject("from").has("last_name"))
@ -169,6 +172,10 @@ public class Vylbot {
if (message.getJSONObject("from").has("username")) if (message.getJSONObject("from").has("username"))
user = message.getJSONObject("from").getString("username"); user = message.getJSONObject("from").getString("username");
//Get group chat name
if (message.getJSONObject("chat").has("title"))
chatName = message.getJSONObject("chat").getString("title");
//Console log //Console log
boolean full_log = false; boolean full_log = false;
if (full_log) System.out.println("\n" + message); if (full_log) System.out.println("\n" + message);
@ -188,14 +195,14 @@ 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); handleInput(chat_id, isGroup, chatName, name, user, text);
return; return;
} }
} }
//--- 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 chat_name, String name, String user, String text) throws UnirestException {
if(text.startsWith("/start") && if(text.startsWith("/start") &&
(!text.substring("/start".length()).startsWith("@") || (!text.substring("/start".length()).startsWith("@") ||
text.substring("/start".length()).startsWith("@" + USERNAME))) text.substring("/start".length()).startsWith("@" + USERNAME)))
@ -210,6 +217,11 @@ public class Vylbot {
(!text.substring("/pokego".length()).startsWith("@") || (!text.substring("/pokego".length()).startsWith("@") ||
text.substring("/pokego".length()).startsWith("@" + USERNAME))) text.substring("/pokego".length()).startsWith("@" + USERNAME)))
handlePokeGoStatus(chat_id); handlePokeGoStatus(chat_id);
if(text.startsWith("/whoami") &&
(!text.substring("/whoami".length()).startsWith("@") ||
text.substring("/whoami".length()).startsWith("@" + USERNAME)))
handleWhoami(chat_id, group, chat_name, name, user);
} }
private void handleStart(long chat_id) throws UnirestException { private void handleStart(long chat_id) throws UnirestException {
@ -223,7 +235,7 @@ public class Vylbot {
"/help - Saca esta lista\n" + "/help - Saca esta lista\n" +
"/pokego - Muestra el status de los servidores de PokeGo " + "/pokego - Muestra el status de los servidores de PokeGo " +
"(según una web de terceros).\n" + "(según una web de terceros).\n" +
"\n_Vylbot v1.1.0_"; "\n_Vylbot v1.2.0_";
sendMessage(chat_id, help, PARSE_MARKDOWN); sendMessage(chat_id, help, PARSE_MARKDOWN);
return; return;
@ -239,6 +251,18 @@ public class Vylbot {
sendMessage(chat_id, status); sendMessage(chat_id, status);
} }
private void handleWhoami(long chat_id, boolean group, String chat_name, String user, String name) throws UnirestException {
String s = "Eres " + name;
if(user != "blank_username") s+= ", con nombre de usuario @" + user;
s += ", en el chat";
if(group && chat_name != null) s+= " " + chat_name;
s += " de id " + chat_id + "\n\n";
s += "En el PC que me mantiene, la fecha actual es " + LocalDate.now() +
" en el formato ISO-8601.";
sendMessage(chat_id, s);
return;
}
private boolean exists(long chat_id, String user) { private boolean exists(long chat_id, String user) {
return chats.containsKey(chat_id) && chats.get(chat_id).exists(user); return chats.containsKey(chat_id) && chats.get(chat_id).exists(user);
} }