Moved bot to @geiserpbot.
Added Message class. TO DO: distinguish messages by chat types.
This commit is contained in:
parent
19e0a1d0f2
commit
8255cb15f8
14 changed files with 367 additions and 166 deletions
122
src/elements/ChannelMessage.java
Normal file
122
src/elements/ChannelMessage.java
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,13 +1,12 @@
|
||||||
package elements;
|
package elements;
|
||||||
|
|
||||||
import elements.exceptions.PoleBlockedException;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by vylion on 4/21/16.
|
* Created by Guillermo Serrahima on 4/21/16.
|
||||||
*/
|
*/
|
||||||
public class Chat {
|
public class Chat {
|
||||||
private static final String DATE_TAG = "Date=";
|
private static final String DATE_TAG = "Date=";
|
||||||
|
@ -46,6 +45,7 @@ public class Chat {
|
||||||
users.put(user, p);
|
users.put(user, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
public void winPole(String user, int date) throws PoleBlockedException {
|
public void winPole(String user, int date) throws PoleBlockedException {
|
||||||
if(!users.containsKey(user)) add(user);
|
if(!users.containsKey(user)) add(user);
|
||||||
if(!availablePole()) {
|
if(!availablePole()) {
|
||||||
|
@ -59,6 +59,7 @@ public class Chat {
|
||||||
|
|
||||||
poleWinner = user;
|
poleWinner = user;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
public void choseMode(String mode) {
|
public void choseMode(String mode) {
|
||||||
nextMode = mode;
|
nextMode = mode;
|
||||||
|
|
121
src/elements/Message.java
Normal file
121
src/elements/Message.java
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,5 @@
|
||||||
package elements;
|
package elements;
|
||||||
|
|
||||||
import elements.exceptions.ReadErrorException;
|
|
||||||
import elements.exceptions.SaveErrorException;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
@ -14,7 +11,7 @@ import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by vylion on 4/14/16.
|
* Created by Guillermo Serrahima on 4/14/16.
|
||||||
*/
|
*/
|
||||||
public class Persistence {
|
public class Persistence {
|
||||||
private static Persistence persistence;
|
private static Persistence persistence;
|
||||||
|
@ -28,6 +25,7 @@ public class Persistence {
|
||||||
return persistence;
|
return persistence;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
public void saveChat(long chat_id, Chat c) throws SaveErrorException {
|
public void saveChat(long chat_id, Chat c) throws SaveErrorException {
|
||||||
try {
|
try {
|
||||||
List<String> lines = Arrays.asList(c.toTxt().split("\n"));
|
List<String> lines = Arrays.asList(c.toTxt().split("\n"));
|
||||||
|
@ -42,7 +40,9 @@ public class Persistence {
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
public HashMap<Long, Chat> readChats() throws ReadErrorException {
|
public HashMap<Long, Chat> readChats() throws ReadErrorException {
|
||||||
HashMap<Long, Chat> chats = new HashMap<Long, Chat>();
|
HashMap<Long, Chat> chats = new HashMap<Long, Chat>();
|
||||||
|
|
||||||
|
@ -73,4 +73,5 @@ public class Persistence {
|
||||||
|
|
||||||
return chats;
|
return chats;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
10
src/elements/exceptions/NoChatNameException.java
Normal file
10
src/elements/exceptions/NoChatNameException.java
Normal file
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
10
src/elements/exceptions/NoTextMessageException.java
Normal file
10
src/elements/exceptions/NoTextMessageException.java
Normal file
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
10
src/elements/exceptions/NoUserException.java
Normal file
10
src/elements/exceptions/NoUserException.java
Normal file
|
@ -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?");
|
||||||
|
}
|
||||||
|
}
|
10
src/elements/exceptions/NoUsernameException.java
Normal file
10
src/elements/exceptions/NoUsernameException.java
Normal file
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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.";
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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.";
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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.");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,10 +1,9 @@
|
||||||
package main;
|
package main;
|
||||||
|
|
||||||
import com.mashape.unirest.http.exceptions.UnirestException;
|
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 class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -17,9 +16,6 @@ public class Main {
|
||||||
} catch (UnirestException e) {
|
} catch (UnirestException e) {
|
||||||
System.out.println("Unirest Error catched.");
|
System.out.println("Unirest Error catched.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (ReadErrorException e) {
|
|
||||||
System.out.println(e.getMessage());
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,10 @@ import com.mashape.unirest.http.JsonNode;
|
||||||
import com.mashape.unirest.http.Unirest;
|
import com.mashape.unirest.http.Unirest;
|
||||||
import com.mashape.unirest.http.exceptions.UnirestException;
|
import com.mashape.unirest.http.exceptions.UnirestException;
|
||||||
import elements.Chat;
|
import elements.Chat;
|
||||||
|
import elements.Message;
|
||||||
import elements.Persistence;
|
import elements.Persistence;
|
||||||
import elements.exceptions.PoleBlockedException;
|
import elements.exceptions.NoChatNameException;
|
||||||
import elements.exceptions.ReadErrorException;
|
import elements.exceptions.NoUsernameException;
|
||||||
import elements.exceptions.SaveErrorException;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
@ -16,12 +16,12 @@ import java.time.LocalDate;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by vylion on 4/1/16.
|
* Created by Guillermo Serrahima on 4/1/16.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class Vylbot {
|
public class Vylbot {
|
||||||
private static final String TOKEN = "165232232:AAGVoVm1AA_cP2RNGh3sR4nPX9hQvujr_ls";
|
private static final String TOKEN = "267229954:AAHx49MXLmT1nT0QkccrSIzgmRVCQbjbJaQ";
|
||||||
private static final String LOGTAG = "VYLBOT";
|
private static final String USERNAME = "geiserpbot";
|
||||||
private static final String USERNAME = "VylionBot";
|
|
||||||
private static final String BASE_URL = "https://api.telegram.org/bot" + TOKEN;
|
private static final String BASE_URL = "https://api.telegram.org/bot" + TOKEN;
|
||||||
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";
|
||||||
|
@ -39,8 +39,8 @@ public class Vylbot {
|
||||||
private List<String> commands;
|
private List<String> commands;
|
||||||
private Map<Long, Chat> chats;
|
private Map<Long, Chat> chats;
|
||||||
|
|
||||||
public Vylbot() throws ReadErrorException {
|
public Vylbot() {
|
||||||
//chats = Persistence.getInstance().readChats();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
|
@ -52,7 +52,7 @@ public class Vylbot {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String myName() {
|
public String myName() {
|
||||||
return "Vylionbot";
|
return "R. Geiser Pont";
|
||||||
}
|
}
|
||||||
|
|
||||||
//--- Http handling
|
//--- Http handling
|
||||||
|
@ -153,79 +153,70 @@ public class Vylbot {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processMessage(JSONObject message) throws UnirestException {
|
private void processMessage(JSONObject message) throws UnirestException {
|
||||||
//Get basic compulsory variables
|
//Process message into a nice and cozy java object
|
||||||
long chat_id = message.getJSONObject("chat").getLong("id");
|
Message m = new Message(message);
|
||||||
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");
|
|
||||||
|
|
||||||
//Console log
|
//Console log
|
||||||
boolean full_log = false;
|
printLog(false, m);
|
||||||
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 + ")");
|
|
||||||
}
|
|
||||||
|
|
||||||
//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, isGroup, chatName, name, user, text);
|
handleInput(m);
|
||||||
return;
|
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
|
//--- Command handling
|
||||||
|
|
||||||
private void handleInput(long chat_id, boolean group, String chat_name, String name, String user, String text) throws UnirestException {
|
private void handleInput(Message m) throws UnirestException {
|
||||||
if(text.startsWith("/start") &&
|
if(!m.isChannel()) {
|
||||||
(!text.substring("/start".length()).startsWith("@") ||
|
if(m.getText().startsWith("/start") &&
|
||||||
text.substring("/start".length()).startsWith("@" + USERNAME)))
|
(!m.getText().substring("/start".length()).startsWith("@") ||
|
||||||
handleStart(chat_id);
|
m.getText().substring("/start".length()).startsWith("@" + USERNAME)))
|
||||||
|
handleStart(m.getCid());
|
||||||
|
|
||||||
if(text.startsWith("/help") &&
|
if(m.getText().startsWith("/help") &&
|
||||||
(!text.substring("/help".length()).startsWith("@") ||
|
(!m.getText().substring("/help".length()).startsWith("@") ||
|
||||||
text.substring("/help".length()).startsWith("@" + USERNAME)))
|
m.getText().substring("/help".length()).startsWith("@" + USERNAME)))
|
||||||
handleHelp(chat_id);
|
handleHelp(m.getCid());
|
||||||
|
|
||||||
if(text.startsWith("/pokego") &&
|
if(m.getText().startsWith("/pokego") &&
|
||||||
(!text.substring("/pokego".length()).startsWith("@") ||
|
(!m.getText().substring("/pokego".length()).startsWith("@") ||
|
||||||
text.substring("/pokego".length()).startsWith("@" + USERNAME)))
|
m.getText().substring("/pokego".length()).startsWith("@" + USERNAME)))
|
||||||
handlePokeGoStatus(chat_id);
|
handlePokeGoStatus(m.getCid());
|
||||||
|
|
||||||
if(text.startsWith("/whoami") &&
|
if(m.getText().startsWith("/whoami") &&
|
||||||
(!text.substring("/whoami".length()).startsWith("@") ||
|
(!m.getText().substring("/whoami".length()).startsWith("@") ||
|
||||||
text.substring("/whoami".length()).startsWith("@" + USERNAME)))
|
m.getText().substring("/whoami".length()).startsWith("@" + USERNAME)))
|
||||||
handleWhoami(chat_id, group, chat_name, name, user);
|
handleWhoami(m);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleStart(long chat_id) throws UnirestException {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,9 +224,10 @@ public class Vylbot {
|
||||||
String help = "Aquí tienes una lista de comandos que acepto:\n\n" +
|
String help = "Aquí tienes una lista de comandos que acepto:\n\n" +
|
||||||
"/start - Repite el saludo inicial\n" +
|
"/start - Repite el saludo inicial\n" +
|
||||||
"/help - Saca esta lista\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" +
|
"(según una web de terceros).\n" +
|
||||||
"\n_Vylbot v1.2.0_";
|
"\n_GeiserPBot v1.0.0_";
|
||||||
|
|
||||||
sendMessage(chat_id, help, PARSE_MARKDOWN);
|
sendMessage(chat_id, help, PARSE_MARKDOWN);
|
||||||
return;
|
return;
|
||||||
|
@ -251,22 +243,29 @@ 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 {
|
private void handleWhoami(Message m) throws UnirestException {
|
||||||
String s = "Eres " + name;
|
String s = "Eres " + m.getAuthor();
|
||||||
if(user != "blank_username") s+= ", con nombre de usuario @" + user;
|
try {
|
||||||
s += ", en el chat";
|
s+= ", con nombre de usuario @" + m.getUsername();
|
||||||
if(group && chat_name != null) s+= " " + chat_name;
|
} catch (NoUsernameException ignored) {}
|
||||||
s += " de id " + chat_id + "\n\n";
|
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() +
|
s += "En el PC que me mantiene, la fecha actual es " + LocalDate.now() +
|
||||||
" en el formato ISO-8601.";
|
" en el formato ISO-8601.";
|
||||||
sendMessage(chat_id, s);
|
sendMessage(m.getCid(), 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
private void saveChat(long chat_id) throws SaveErrorException {
|
private void saveChat(long chat_id) throws SaveErrorException {
|
||||||
try {
|
try {
|
||||||
Persistence.getInstance().saveChat(chat_id, chats.get(chat_id));
|
Persistence.getInstance().saveChat(chat_id, chats.get(chat_id));
|
||||||
|
@ -275,6 +274,7 @@ public class Vylbot {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
private String getWarning(int warning_id) {
|
private String getWarning(int warning_id) {
|
||||||
switch (warning_id) {
|
switch (warning_id) {
|
||||||
|
|
Reference in a new issue