From 19e0a1d0f21c3cf77c2fe2a9985e392241c5d7fb Mon Sep 17 00:00:00 2001 From: Vylion Date: Sat, 9 Jul 2016 10:34:20 +0200 Subject: [PATCH] Copied whoami command from the testbot. --- src/main/Vylbot.java | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/Vylbot.java b/src/main/Vylbot.java index e72fcb0..144a4b8 100644 --- a/src/main/Vylbot.java +++ b/src/main/Vylbot.java @@ -12,6 +12,7 @@ import elements.exceptions.SaveErrorException; import org.json.JSONArray; import org.json.JSONObject; +import java.time.LocalDate; import java.util.*; /** @@ -157,9 +158,11 @@ public class Vylbot { int message_id = message.getInt("message_id"); String chatType = message.getJSONObject("chat").getString("type"); String name = message.getJSONObject("from").getString("first_name"); + boolean isGroup = chatType.contains("group"); //Declare basic optional variables String user = null; + String chatName = null; //Check for user last name if (message.getJSONObject("from").has("last_name")) @@ -169,6 +172,10 @@ public class Vylbot { if (message.getJSONObject("from").has("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 boolean full_log = false; if (full_log) System.out.println("\n" + message); @@ -188,14 +195,14 @@ public class Vylbot { //Actually read the message if (message.has("text")) { String text = message.getString("text"); - handleInput(chat_id, chatType.contains("group"), user, text); + handleInput(chat_id, isGroup, chatName, name, user, text); return; } } //--- 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") && (!text.substring("/start".length()).startsWith("@") || text.substring("/start".length()).startsWith("@" + USERNAME))) @@ -210,6 +217,11 @@ public class Vylbot { (!text.substring("/pokego".length()).startsWith("@") || text.substring("/pokego".length()).startsWith("@" + USERNAME))) 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 { @@ -223,7 +235,7 @@ public class Vylbot { "/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_"; + "\n_Vylbot v1.2.0_"; sendMessage(chat_id, help, PARSE_MARKDOWN); return; @@ -239,6 +251,18 @@ public class Vylbot { 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) { return chats.containsKey(chat_id) && chats.get(chat_id).exists(user); }