diff --git a/src/elements/ChannelMessage.java b/src/elements/ChannelMessage.java new file mode 100644 index 0000000..10c1a40 --- /dev/null +++ b/src/elements/ChannelMessage.java @@ -0,0 +1,122 @@ +package elements; + +import elements.exceptions.NoChatNameException; +import elements.exceptions.NoUsernameException; +import org.json.JSONObject; + +/** + * Created by Guillermo Serrahima on 10/8/16. + */ +public class ChannelMessage extends Message { + private Long cid; //chat id + private Integer mid; //message id + private Integer uid; //user id + private String chatType; //chat type + private String fullName; //author name + private String userName; //username + private String chatName; //chat name + private String text; //the actual message + private String consoleLog; + + public ChannelMessage(JSONObject message) { + super(message); + cid = message.getJSONObject("chat").getLong("id"); + mid = message.getInt("message_id"); + chatType = message.getJSONObject("chat").getString("type"); + + //Check for User + if(message.has("from")) { + fullName = message.getJSONObject("from").getString("first_name"); + uid = message.getJSONObject("from").getInt("id"); + + //Check for user last name + if (message.getJSONObject("from").has("last_name")) + fullName += " " + message.getJSONObject("from").getString("last_name"); + + //Check for user username + if (message.getJSONObject("from").has("username")) + userName = message.getJSONObject("from").getString("username"); + else userName = null; + } + + else { + fullName = null; + uid = null; + userName = null; + } + + //Get group chat name + if (message.getJSONObject("chat").has("title")) + chatName = message.getJSONObject("chat").getString("title"); + else chatName = null; + + if (message.has("text")) + text = message.getString("text"); + else text = null; + + consoleLog = message.toString(); + } + + public long getCid() { + return cid; + } + + public long getMid() { + return mid; + } + + public int getUid() { + return uid; + } + + public String getAuthor() { + return fullName; + } + + public boolean hasUsername() { + return userName != null; + } + + public String getUsername() throws NoUsernameException { + if(userName == null) throw new NoUsernameException(); + return userName; + } + + public String getChatName() throws NoChatNameException { + if(userName == null) throw new NoChatNameException(); + return chatName; + } + + public boolean hasText() { + return text != null; + } + + public String getText() { + return text; + } + + public boolean isGroup() { + return chatType.contains("group"); + } + + public boolean isBasicGroup() { + return chatType.equals("group"); + } + + public boolean isSupergroup() { + return chatType.equals("supergroup"); + } + + public boolean isChannel() { + return chatType.equals("channel"); + } + + public boolean isPrivate() { + return chatType.equals("private"); + } + + @Override + public String toString() { + return consoleLog; + } +} diff --git a/src/elements/Chat.java b/src/elements/Chat.java index e284e35..91e3519 100644 --- a/src/elements/Chat.java +++ b/src/elements/Chat.java @@ -1,13 +1,12 @@ package elements; -import elements.exceptions.PoleBlockedException; import java.time.LocalDate; import java.time.ZoneId; import java.util.*; /** - * Created by vylion on 4/21/16. + * Created by Guillermo Serrahima on 4/21/16. */ public class Chat { private static final String DATE_TAG = "Date="; @@ -46,6 +45,7 @@ public class Chat { users.put(user, p); } + /* public void winPole(String user, int date) throws PoleBlockedException { if(!users.containsKey(user)) add(user); if(!availablePole()) { @@ -59,6 +59,7 @@ public class Chat { poleWinner = user; } + */ public void choseMode(String mode) { nextMode = mode; diff --git a/src/elements/Message.java b/src/elements/Message.java new file mode 100644 index 0000000..8adeca9 --- /dev/null +++ b/src/elements/Message.java @@ -0,0 +1,121 @@ +package elements; + +import elements.exceptions.NoChatNameException; +import elements.exceptions.NoUserException; +import elements.exceptions.NoUsernameException; +import org.json.JSONObject; + +/** + * Created by Guillermo Serrahima on 10/8/16. + */ +public class Message { + private Long cid; //chat id + private Integer mid; //message id + private Integer uid; //user id + private String chatType; //chat type + private String fullName; //author name + private String userName; //username + private String chatName; //chat name + private String text; //the actual message + private String consoleLog; + + public Message(JSONObject message) { + cid = message.getJSONObject("chat").getLong("id"); + mid = message.getInt("message_id"); + chatType = message.getJSONObject("chat").getString("type"); + + //Check for User + if(message.has("from")) { + fullName = message.getJSONObject("from").getString("first_name"); + uid = message.getJSONObject("from").getInt("id"); + + //Check for user last name + if (message.getJSONObject("from").has("last_name")) + fullName += " " + message.getJSONObject("from").getString("last_name"); + + //Check for user username + if (message.getJSONObject("from").has("username")) + userName = message.getJSONObject("from").getString("username"); + else userName = null; + } + else { + fullName = null; + uid = null; + userName = null; + } + + //Get group chat name + if (message.getJSONObject("chat").has("title")) + chatName = message.getJSONObject("chat").getString("title"); + else chatName = null; + + if (message.has("text")) + text = message.getString("text"); + else text = null; + + consoleLog = message.toString(); + } + + public long getCid() { + return cid; + } + + public long getMid() { + return mid; + } + + public int getUid() { + return uid; + } + + public String getAuthor() { + return fullName; + } + + public boolean hasUsername() { + return userName != null; + } + + public String getUsername() throws NoUsernameException { + if(userName == null) throw new NoUsernameException(); + return userName; + } + + public String getChatName() throws NoChatNameException { + if(userName == null) throw new NoChatNameException(); + return chatName; + } + + public boolean hasText() { + return text != null; + } + + public String getText() { + return text; + } + + public boolean isGroup() { + return chatType.contains("group"); + } + + public boolean isBasicGroup() { + return chatType.equals("group"); + } + + public boolean isSupergroup() { + return chatType.equals("supergroup"); + } + + public boolean isChannel() { + return chatType.equals("channel"); + } + + public boolean isPrivate() { + return chatType.equals("private"); + } + + @Override + public String toString() { + return consoleLog; + } +} diff --git a/src/elements/Persistence.java b/src/elements/Persistence.java index b6b318f..402b6cd 100644 --- a/src/elements/Persistence.java +++ b/src/elements/Persistence.java @@ -1,8 +1,5 @@ package elements; -import elements.exceptions.ReadErrorException; -import elements.exceptions.SaveErrorException; - import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; @@ -14,7 +11,7 @@ import java.util.List; import java.util.stream.Collectors; /** - * Created by vylion on 4/14/16. + * Created by Guillermo Serrahima on 4/14/16. */ public class Persistence { private static Persistence persistence; @@ -28,6 +25,7 @@ public class Persistence { return persistence; } + /* public void saveChat(long chat_id, Chat c) throws SaveErrorException { try { List lines = Arrays.asList(c.toTxt().split("\n")); @@ -42,7 +40,9 @@ public class Persistence { return; } + */ + /* public HashMap readChats() throws ReadErrorException { HashMap chats = new HashMap(); @@ -73,4 +73,5 @@ public class Persistence { return chats; } + */ } diff --git a/src/elements/exceptions/NoChatNameException.java b/src/elements/exceptions/NoChatNameException.java new file mode 100644 index 0000000..a8d68a0 --- /dev/null +++ b/src/elements/exceptions/NoChatNameException.java @@ -0,0 +1,10 @@ +package elements.exceptions; + +/** + * Created by Guillermo Serrahima on 10/8/16. + */ +public class NoChatNameException extends Exception { + public NoChatNameException() { + super("The chat has no title."); + } +} diff --git a/src/elements/exceptions/NoTextMessageException.java b/src/elements/exceptions/NoTextMessageException.java new file mode 100644 index 0000000..240c4b7 --- /dev/null +++ b/src/elements/exceptions/NoTextMessageException.java @@ -0,0 +1,10 @@ +package elements.exceptions; + +/** + * Created by Guillermo Serrahima on 10/8/16. + */ +public class NoTextMessageException extends Exception { + public NoTextMessageException() { + super("The message has no text."); + } +} diff --git a/src/elements/exceptions/NoUserException.java b/src/elements/exceptions/NoUserException.java new file mode 100644 index 0000000..06efefd --- /dev/null +++ b/src/elements/exceptions/NoUserException.java @@ -0,0 +1,10 @@ +package elements.exceptions; + +/** + * Created by Guillermo Serrahima on 10/8/16. + */ +public class NoUserException extends Exception { + public NoUserException() { + super("The message has no user. Is it from a channel?"); + } +} diff --git a/src/elements/exceptions/NoUsernameException.java b/src/elements/exceptions/NoUsernameException.java new file mode 100644 index 0000000..7916455 --- /dev/null +++ b/src/elements/exceptions/NoUsernameException.java @@ -0,0 +1,10 @@ +package elements.exceptions; + +/** + * Created by Guillermo Serrahima on 10/8/16. + */ +public class NoUsernameException extends Exception { + public NoUsernameException() { + super("The user has no username."); + } +} diff --git a/src/elements/exceptions/PoleBlockedException.java b/src/elements/exceptions/PoleBlockedException.java deleted file mode 100644 index 2521fd2..0000000 --- a/src/elements/exceptions/PoleBlockedException.java +++ /dev/null @@ -1,17 +0,0 @@ -package elements.exceptions; - -/** - * Created by vylion on 4/21/16. - */ -public class PoleBlockedException extends Exception { - - public PoleBlockedException() { - super("Today's daily pole position has already been won. " + - "Try again tomorrow."); - } - - public PoleBlockedException(String user) { - super("Today's daily pole position has already been won by " + user +". " + - "Try again tomorrow."); - } -} diff --git a/src/elements/exceptions/ReadErrorException.java b/src/elements/exceptions/ReadErrorException.java deleted file mode 100644 index 7142929..0000000 --- a/src/elements/exceptions/ReadErrorException.java +++ /dev/null @@ -1,26 +0,0 @@ -package elements.exceptions; - -import java.io.IOException; - -/** - * Created by vylion on 4/15/16. - */ -public class ReadErrorException extends IOException { - public ReadErrorException() { - super("Ha ocurrido un error al leer los datos. " + - "Contacta con mi creador o algo. io k se xdxd"); - } - public ReadErrorException(long chat_id) { - super("Ha ocurrido un error al leer los datos del chat " + chat_id + - ". Contacta con mi creador o algo. io k se xdxd"); - } - - @Override - public String getLocalizedMessage() { - return "Couldn't read file. You're on your own on this one."; - } - - public String getLocalizedMessage(long chat_id) { - return "Couldn't read file " + chat_id + ". You're on your own on this one."; - } -} diff --git a/src/elements/exceptions/SaveErrorException.java b/src/elements/exceptions/SaveErrorException.java deleted file mode 100644 index c2a3ee7..0000000 --- a/src/elements/exceptions/SaveErrorException.java +++ /dev/null @@ -1,26 +0,0 @@ -package elements.exceptions; - -import java.io.IOException; - -/** - * Created by vylion on 4/15/16. - */ -public class SaveErrorException extends IOException { - public SaveErrorException() { - super("Ha ocurrido un error al guardar los datos. Contacta con mi creador o algo. io k se xdxd"); - } - - public SaveErrorException(long chat_id) { - super("Ha ocurrido un error al guardar los datos del chat " + chat_id + - ". Contacta con mi creador o algo. io k se xdxd"); - } - - @Override - public String getLocalizedMessage() { - return "Couldn't save to file. You're on your own on this one."; - } - - public String getLocalizedMessage(long chat_id) { - return "Couldn't save " + chat_id + " to file. You're on your own on this one."; - } -} diff --git a/src/elements/exceptions/UserNotExistingException.java b/src/elements/exceptions/UserNotExistingException.java deleted file mode 100644 index 8e9d05e..0000000 --- a/src/elements/exceptions/UserNotExistingException.java +++ /dev/null @@ -1,11 +0,0 @@ -package elements.exceptions; - -/** - * Created by vylion on 4/21/16. - */ -public class UserNotExistingException extends Exception { - - public UserNotExistingException() { - super("No tengo constancia de ninguna persona con ese nombre de usuario."); - } -} diff --git a/src/main/Main.java b/src/main/Main.java index 0714418..3a362a1 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -1,10 +1,9 @@ package main; import com.mashape.unirest.http.exceptions.UnirestException; -import elements.exceptions.ReadErrorException; /** - * Created by vylion on 4/1/16. + * Created by Guillermo Serrahima on 4/1/16. */ public class Main { public static void main(String[] args) { @@ -17,9 +16,6 @@ public class Main { } catch (UnirestException e) { System.out.println("Unirest Error catched."); e.printStackTrace(); - } catch (ReadErrorException e) { - System.out.println(e.getMessage()); - e.printStackTrace(); } } } diff --git a/src/main/Vylbot.java b/src/main/Vylbot.java index 144a4b8..f91d0cb 100644 --- a/src/main/Vylbot.java +++ b/src/main/Vylbot.java @@ -5,10 +5,10 @@ import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.exceptions.UnirestException; import elements.Chat; +import elements.Message; import elements.Persistence; -import elements.exceptions.PoleBlockedException; -import elements.exceptions.ReadErrorException; -import elements.exceptions.SaveErrorException; +import elements.exceptions.NoChatNameException; +import elements.exceptions.NoUsernameException; import org.json.JSONArray; import org.json.JSONObject; @@ -16,12 +16,12 @@ import java.time.LocalDate; import java.util.*; /** - * Created by vylion on 4/1/16. + * Created by Guillermo Serrahima on 4/1/16. */ + public class Vylbot { - private static final String TOKEN = "165232232:AAGVoVm1AA_cP2RNGh3sR4nPX9hQvujr_ls"; - private static final String LOGTAG = "VYLBOT"; - private static final String USERNAME = "VylionBot"; + private static final String TOKEN = "267229954:AAHx49MXLmT1nT0QkccrSIzgmRVCQbjbJaQ"; + private static final String USERNAME = "geiserpbot"; private static final String BASE_URL = "https://api.telegram.org/bot" + TOKEN; private static final String PARSE_MARKDOWN = "Markdown"; private static final String PARSE_HTML = "HTML"; @@ -39,8 +39,8 @@ public class Vylbot { private List commands; private Map chats; - public Vylbot() throws ReadErrorException { - //chats = Persistence.getInstance().readChats(); + public Vylbot() { + } public String getUsername() { @@ -52,7 +52,7 @@ public class Vylbot { } public String myName() { - return "Vylionbot"; + return "R. Geiser Pont"; } //--- Http handling @@ -153,79 +153,70 @@ public class Vylbot { } private void processMessage(JSONObject message) throws UnirestException { - //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"); - 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")) - name += " " + message.getJSONObject("from").getString("last_name"); - - //Check for user username - 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"); + //Process message into a nice and cozy java object + Message m = new Message(message); //Console log - boolean full_log = false; - if (full_log) System.out.println("\n" + message); - else { - if (message.getJSONObject("chat").has("title")) - System.out.println("\nMessage received from chat " + - message.getJSONObject("chat").getString("title") + - " (chat id: " + chat_id + ")"); - else if (user != null) - System.out.println("\nMessage received from user @" + user + - " (chat id: " + chat_id + ")"); - else - System.out.println("\nMessage received from user named " + name + - " (chat id: " + chat_id + ")"); - } + printLog(false, m); //Actually read the message if (message.has("text")) { String text = message.getString("text"); - handleInput(chat_id, isGroup, chatName, name, user, text); + handleInput(m); return; } } + private void printLog(boolean full_log, Message m) { + if (full_log) System.out.println("\n" + m); + else { + String log = "\nMessage "; + + try { + log += "received from chat " + m.getChatName(); + } catch (NoChatNameException e) { + log += "received from a private chat "; + } + log += " (chat id: " + m.getCid() + ") "; + try { + log += "by user @" + m.getUsername() + " "; + } catch (NoUsernameException e) { + log += "by user "; + } + log += "named " + m.getAuthor(); + + System.out.println(log + "."); + } + } + //--- Command handling - 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))) - handleStart(chat_id); + private void handleInput(Message m) throws UnirestException { + if(!m.isChannel()) { + if(m.getText().startsWith("/start") && + (!m.getText().substring("/start".length()).startsWith("@") || + m.getText().substring("/start".length()).startsWith("@" + USERNAME))) + handleStart(m.getCid()); - if(text.startsWith("/help") && - (!text.substring("/help".length()).startsWith("@") || - text.substring("/help".length()).startsWith("@" + USERNAME))) - handleHelp(chat_id); + if(m.getText().startsWith("/help") && + (!m.getText().substring("/help".length()).startsWith("@") || + m.getText().substring("/help".length()).startsWith("@" + USERNAME))) + handleHelp(m.getCid()); - if(text.startsWith("/pokego") && - (!text.substring("/pokego".length()).startsWith("@") || - text.substring("/pokego".length()).startsWith("@" + USERNAME))) - handlePokeGoStatus(chat_id); + if(m.getText().startsWith("/pokego") && + (!m.getText().substring("/pokego".length()).startsWith("@") || + m.getText().substring("/pokego".length()).startsWith("@" + USERNAME))) + handlePokeGoStatus(m.getCid()); - if(text.startsWith("/whoami") && - (!text.substring("/whoami".length()).startsWith("@") || - text.substring("/whoami".length()).startsWith("@" + USERNAME))) - handleWhoami(chat_id, group, chat_name, name, user); + if(m.getText().startsWith("/whoami") && + (!m.getText().substring("/whoami".length()).startsWith("@") || + m.getText().substring("/whoami".length()).startsWith("@" + USERNAME))) + handleWhoami(m); + } } private void handleStart(long chat_id) throws UnirestException { - sendMessage(chat_id, "Hola. Soy el bot personal de Vylion"); + sendMessage(chat_id, "Hola. Soy el bot personal de Guillermo Serrahima."); return; } @@ -233,9 +224,10 @@ public class Vylbot { 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 " + + "/whoami - Muestra información visible sobre ti mismo\n" + + "/pokego - Muestra el status de los servidores de Pokémon Go " + "(según una web de terceros).\n" + - "\n_Vylbot v1.2.0_"; + "\n_GeiserPBot v1.0.0_"; sendMessage(chat_id, help, PARSE_MARKDOWN); return; @@ -251,22 +243,29 @@ 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"; + private void handleWhoami(Message m) throws UnirestException { + String s = "Eres " + m.getAuthor(); + try { + s+= ", con nombre de usuario @" + m.getUsername(); + } catch (NoUsernameException ignored) {} + s += " e id de usuario " + m.getUid(); + s += ", en "; + try { + s+= "el chat " + m.getChatName(); + } catch (NoChatNameException e) { + s+= "un chat privado"; + } + s += " de id " + m.getCid() + ".\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; + sendMessage(m.getCid(), s); } private boolean exists(long chat_id, String user) { return chats.containsKey(chat_id) && chats.get(chat_id).exists(user); } + /* private void saveChat(long chat_id) throws SaveErrorException { try { Persistence.getInstance().saveChat(chat_id, chats.get(chat_id)); @@ -275,6 +274,7 @@ public class Vylbot { throw e; } } + */ private String getWarning(int warning_id) { switch (warning_id) {