Second commit: using PeppercornBot instead as a base.
This commit is contained in:
parent
bdad9fed04
commit
915dc94955
6 changed files with 334 additions and 523 deletions
|
@ -0,0 +1,9 @@
|
||||||
|
<component name="libraryTable">
|
||||||
|
<library name="unirest-java-1.4.10-SNAPSHOT-jar-with-dependencies">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$PROJECT_DIR$/libraries/unirest-java-1.4.10-SNAPSHOT-jar-with-dependencies.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</component>
|
|
@ -61,54 +61,4 @@
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
<component name="masterDetails">
|
|
||||||
<states>
|
|
||||||
<state key="GlobalLibrariesConfigurable.UI">
|
|
||||||
<settings>
|
|
||||||
<splitter-proportions>
|
|
||||||
<option name="proportions">
|
|
||||||
<list>
|
|
||||||
<option value="0.2" />
|
|
||||||
</list>
|
|
||||||
</option>
|
|
||||||
</splitter-proportions>
|
|
||||||
</settings>
|
|
||||||
</state>
|
|
||||||
<state key="JdkListConfigurable.UI">
|
|
||||||
<settings>
|
|
||||||
<last-edited>1.8</last-edited>
|
|
||||||
<splitter-proportions>
|
|
||||||
<option name="proportions">
|
|
||||||
<list>
|
|
||||||
<option value="0.2" />
|
|
||||||
</list>
|
|
||||||
</option>
|
|
||||||
</splitter-proportions>
|
|
||||||
</settings>
|
|
||||||
</state>
|
|
||||||
<state key="ProjectJDKs.UI">
|
|
||||||
<settings>
|
|
||||||
<last-edited>1.8</last-edited>
|
|
||||||
<splitter-proportions>
|
|
||||||
<option name="proportions">
|
|
||||||
<list>
|
|
||||||
<option value="0.2" />
|
|
||||||
</list>
|
|
||||||
</option>
|
|
||||||
</splitter-proportions>
|
|
||||||
</settings>
|
|
||||||
</state>
|
|
||||||
<state key="ProjectLibrariesConfigurable.UI">
|
|
||||||
<settings>
|
|
||||||
<splitter-proportions>
|
|
||||||
<option name="proportions">
|
|
||||||
<list>
|
|
||||||
<option value="0.2" />
|
|
||||||
</list>
|
|
||||||
</option>
|
|
||||||
</splitter-proportions>
|
|
||||||
</settings>
|
|
||||||
</state>
|
|
||||||
</states>
|
|
||||||
</component>
|
|
||||||
</project>
|
</project>
|
|
@ -7,14 +7,14 @@ import com.mashape.unirest.http.exceptions.UnirestException;
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
VylBot bot = new VylBot();
|
|
||||||
System.out.println("The TestBot awakens.");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
Vylbot bot = new Vylbot();
|
||||||
|
System.out.println("The Bot @" + bot.getUsername() + " awakens.");
|
||||||
|
|
||||||
System.out.println("Bot running.");
|
System.out.println("Bot running.");
|
||||||
bot.run();
|
bot.run();
|
||||||
} catch (UnirestException e) {
|
} catch (UnirestException e) {
|
||||||
System.out.println("Error catched.");
|
System.out.println("Unirest Error catched.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,469 +0,0 @@
|
||||||
package main;
|
|
||||||
|
|
||||||
import com.mashape.unirest.http.HttpResponse;
|
|
||||||
import com.mashape.unirest.http.JsonNode;
|
|
||||||
import com.mashape.unirest.http.Unirest;
|
|
||||||
import com.mashape.unirest.http.exceptions.UnirestException;
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by vylion on 4/1/16.
|
|
||||||
*/
|
|
||||||
public class VylBot {
|
|
||||||
private static final String TOKEN = "208948965:AAH24jpwNdkXk-y2dqIMbRcAuzrfFd4RagE";
|
|
||||||
private static final String LOGTAG = "TESTBOT";
|
|
||||||
private static final String USERNAME = "VylionTestBot";
|
|
||||||
private static final String BASE_URL = "https://api.telegram.org/bot" + TOKEN;
|
|
||||||
private int anacondaCounter;
|
|
||||||
private boolean alive;
|
|
||||||
|
|
||||||
private final String[] names = {
|
|
||||||
USERNAME,
|
|
||||||
myName(),
|
|
||||||
"Vylion's bot",
|
|
||||||
"Vylion Testbot",
|
|
||||||
"Vylion Bot",
|
|
||||||
"VylionBot",
|
|
||||||
"Vyl's Bot",
|
|
||||||
"Vyl Bot",
|
|
||||||
"VylBot",
|
|
||||||
"Bot de Vyl"
|
|
||||||
};
|
|
||||||
|
|
||||||
public VylBot() {
|
|
||||||
anacondaCounter = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
//--- Http handling
|
|
||||||
|
|
||||||
public HttpResponse<JsonNode> sendMessage(Long chatId, String text) throws UnirestException {
|
|
||||||
System.out.println("Sending message: \n" + text + "\n");
|
|
||||||
|
|
||||||
return Unirest.post(BASE_URL + "/sendMessage")
|
|
||||||
.field("chat_id", chatId)
|
|
||||||
.field("text", text)
|
|
||||||
.asJson();
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpResponse<JsonNode> sendMessage(String channel, String text) throws UnirestException {
|
|
||||||
System.out.println("Sending message to " + channel + ": \n" + text + "\n");
|
|
||||||
|
|
||||||
return Unirest.post(BASE_URL + "/sendMessage")
|
|
||||||
.field("chat_id", channel)
|
|
||||||
.field("text", text)
|
|
||||||
.asJson();
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpResponse<JsonNode> replyMessage(Long chatId, Integer repliedMessage, String text) throws UnirestException {
|
|
||||||
System.out.println("Sending message: \n" + text + "\n");
|
|
||||||
|
|
||||||
return Unirest.post(BASE_URL + "/sendMessage")
|
|
||||||
.field("chat_id", chatId)
|
|
||||||
.field("text", text)
|
|
||||||
.field("reply_to_message_id", repliedMessage)
|
|
||||||
.asJson();
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpResponse<JsonNode> getUpdates(Integer offset) throws UnirestException {
|
|
||||||
return Unirest.post(BASE_URL + "/getUpdates")
|
|
||||||
.field("offset", offset)
|
|
||||||
.asJson();
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpResponse<JsonNode> getUpdates(Integer offset, Integer timeout) throws UnirestException {
|
|
||||||
return Unirest.post(BASE_URL + "/getUpdates")
|
|
||||||
.field("offset", offset)
|
|
||||||
.field("timeout", timeout)
|
|
||||||
.asJson();
|
|
||||||
}
|
|
||||||
|
|
||||||
//--- bot thinking
|
|
||||||
|
|
||||||
public void run() throws UnirestException {
|
|
||||||
int last_upd_id = 0;
|
|
||||||
HttpResponse<JsonNode> response;
|
|
||||||
System.out.println("Listening.\n");
|
|
||||||
|
|
||||||
while(true) {
|
|
||||||
response = getUpdates(last_upd_id++);
|
|
||||||
if (response.getStatus() == 200) {
|
|
||||||
JSONArray responses = response.getBody().getObject().getJSONArray("result");
|
|
||||||
if(responses.isNull(0)) continue;
|
|
||||||
else last_upd_id = responses.getJSONObject(responses.length()-1).getInt("update_id")+1;
|
|
||||||
|
|
||||||
for (int i = 0; i < responses.length(); i++) {
|
|
||||||
JSONObject message = responses.getJSONObject(i).getJSONObject("message");
|
|
||||||
|
|
||||||
processMessage(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void processMessage(JSONObject message) throws UnirestException {
|
|
||||||
String user = "blank_username";
|
|
||||||
String name = "blank_name";
|
|
||||||
String chatName = "blank_chatname";
|
|
||||||
|
|
||||||
long chat_id = message.getJSONObject("chat").getLong("id");
|
|
||||||
|
|
||||||
int message_id = message.getInt("message_id");
|
|
||||||
|
|
||||||
if (message.getJSONObject("from").has("username"))
|
|
||||||
user = message.getJSONObject("from").getString("username");
|
|
||||||
|
|
||||||
if (message.getJSONObject("chat").has("title"))
|
|
||||||
chatName = message.getJSONObject("chat").getString("title");
|
|
||||||
else if (!name.equals("blank_name"))
|
|
||||||
chatName = name;
|
|
||||||
else if (!user.equals("blank_username"))
|
|
||||||
chatName = user;
|
|
||||||
else chatName = "chat id " + chat_id;
|
|
||||||
|
|
||||||
System.out.println("Message received from " + chatName + "\n");
|
|
||||||
//System.out.println(message + "\n");
|
|
||||||
|
|
||||||
if (message.getJSONObject("from").has("first_name") || message.getJSONObject("from").has("last_name")) {
|
|
||||||
if (!message.getJSONObject("from").has("first_name"))
|
|
||||||
name = message.getJSONObject("from").getString("last_name");
|
|
||||||
|
|
||||||
else if (!message.getJSONObject("from").has("name"))
|
|
||||||
name = message.getJSONObject("from").getString("first_name");
|
|
||||||
|
|
||||||
else name = message.getJSONObject("from").getString("first_name") + " " +
|
|
||||||
message.getJSONObject("from").getString("last_name");
|
|
||||||
}
|
|
||||||
|
|
||||||
//IGNORE VGA
|
|
||||||
//if(Long.toString(chat_id).equals("-1001049453258")) return;
|
|
||||||
|
|
||||||
if (message.has("text")) {
|
|
||||||
String text = message.getString("text");
|
|
||||||
String reply = "";
|
|
||||||
|
|
||||||
if (text.startsWith("/")) handleCommand(chat_id, message_id, user, name, text);
|
|
||||||
else handleText(chat_id, message_id, user, name, text);
|
|
||||||
return;
|
|
||||||
} else if (message.has("new_chat_participant")) {
|
|
||||||
handleNewParticipant(chat_id, message.getJSONObject("new_chat_participant"));
|
|
||||||
return;
|
|
||||||
} else if (message.has("left_chat_participant")) {
|
|
||||||
handleExpulsion(chat_id, message.getJSONObject("left_chat_participant"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//--- processMessage auxiliar funcitons
|
|
||||||
|
|
||||||
private void handleCommand(long chat_id, int message_id, String user, String name, String text) throws UnirestException {
|
|
||||||
String command;
|
|
||||||
|
|
||||||
//START COMMAND
|
|
||||||
command = "/start";
|
|
||||||
if (text.startsWith(command + "@") && !text.startsWith(command + "@" + USERNAME)) return;
|
|
||||||
if (text.startsWith(command)) {
|
|
||||||
sendMessage(chat_id, "Hola, soy el testbot de Vylion.");
|
|
||||||
|
|
||||||
if((text.length() > command.length()) && (!text.startsWith(command + "@" + USERNAME))) {
|
|
||||||
handleText(chat_id, message_id, user, name, text);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//ECHO COMMAND
|
|
||||||
command = "/echo";
|
|
||||||
if (text.startsWith(command + "@") && !text.startsWith(command + "@" + USERNAME)) return;
|
|
||||||
if (text.startsWith(command)) {
|
|
||||||
if(text.startsWith(command + " " + command)) {
|
|
||||||
sendMessage(chat_id, "Qué manía con los nested /echo");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
text = text.substring(command.length());
|
|
||||||
if(text.startsWith("@")) {
|
|
||||||
if(text.startsWith("@" + USERNAME)) text = text.substring(("@" + USERNAME).length());
|
|
||||||
else return;
|
|
||||||
}
|
|
||||||
text = text.trim();
|
|
||||||
if(text.length() > 0) sendMessage(chat_id, text);
|
|
||||||
else sendMessage(chat_id, "Formato incorrecto. El formato es\n" +
|
|
||||||
command + " mensaje"
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//ME COMMAND
|
|
||||||
command = "/me";
|
|
||||||
if (text.startsWith(command + "@") && !text.startsWith(command + "@" + USERNAME)) return;
|
|
||||||
if (text.startsWith(command)) {
|
|
||||||
text = text.substring(command.length());
|
|
||||||
if(text.startsWith("@")) {
|
|
||||||
if(text.startsWith("@" + USERNAME)) text = text.substring(("@" + USERNAME).length());
|
|
||||||
else return;
|
|
||||||
}
|
|
||||||
if(!text.startsWith(" ")) {
|
|
||||||
sendMessage(chat_id, "Formato incorrecto. El formato es\n" +
|
|
||||||
command + " mensaje"
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
text = text.trim();
|
|
||||||
sendMessage(chat_id, name + " " + text);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//QUOTE COMMAND
|
|
||||||
command = "/quote";
|
|
||||||
if (text.startsWith(command + "@") && !text.startsWith(command + "@" + USERNAME)) return;
|
|
||||||
if (text.startsWith(command)) {
|
|
||||||
text = text.substring(command.length());
|
|
||||||
if(text.startsWith("@")) {
|
|
||||||
if(text.startsWith("@" + USERNAME)) text = text.substring(("@" + USERNAME).length());
|
|
||||||
else return;
|
|
||||||
}
|
|
||||||
if(!text.startsWith(" ")) {
|
|
||||||
sendMessage(chat_id, "Formato incorrecto. El formato es\n" +
|
|
||||||
command + " nombre: mensaje"
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
text = text.trim();
|
|
||||||
String[] quote = text.split(":", 2);
|
|
||||||
if(quote.length < 2 || !quote[1].startsWith(" ")) {
|
|
||||||
sendMessage(chat_id, "formato incorrecto");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sendMessage(chat_id, "\"" + quote[1].substring(1) + "\" - " + quote[0]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//HELP COMMAND
|
|
||||||
command = "/help";
|
|
||||||
if (text.startsWith(command + "@") && !text.startsWith(command + "@" + USERNAME)) return;
|
|
||||||
if (text.startsWith(command)) {
|
|
||||||
text = text.substring(command.length());
|
|
||||||
|
|
||||||
sendMessage(chat_id, "Soy un bot personal de Vylion " +
|
|
||||||
"para la experimentación con el sistema de Bots de Telegram.\n\n" +
|
|
||||||
"Los comandos que acepto son:\n" +
|
|
||||||
"/start\n" +
|
|
||||||
"/help\n" +
|
|
||||||
"/echo seguido de un mensaje\n" +
|
|
||||||
"/me seguido de un mensaje\n" +
|
|
||||||
"/quote nombre: mensaje\n" +
|
|
||||||
"Y también reconozco varias palabras dentro de los mensajes."
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//BYE
|
|
||||||
command = "/bye";
|
|
||||||
if (text.startsWith(command + "@") && !text.startsWith(command + "@" + USERNAME)) return;
|
|
||||||
if (text.startsWith(command)) {
|
|
||||||
bye(chat_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* NO FUNCIONA - No ese pueden enviar PMs directos a @username, sólo a @channel
|
|
||||||
else if(text.startsWith("/send")) {
|
|
||||||
text = text.substring("/send".length());
|
|
||||||
if(!text.startsWith(" @")) sendMessage(chat_id, "formato incorrecto");
|
|
||||||
else {
|
|
||||||
text = text.substring(1);
|
|
||||||
String[] splitText = text.split(" ", 2);
|
|
||||||
|
|
||||||
sendMessage(splitText[0], splitText[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleWords(long chat_id, int message_id, String text, boolean mention) throws UnirestException {
|
|
||||||
|
|
||||||
//MY ANACONDA DON'T
|
|
||||||
if(text.toLowerCase().equals("my anaconda don't")) {
|
|
||||||
anacondaCounter++;
|
|
||||||
if(anacondaCounter >= 3) {
|
|
||||||
sendMessage(chat_id, "Want none unless you've got buns, hun");
|
|
||||||
anacondaCounter = 0;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//FIRE IN THE DISCO
|
|
||||||
if(text.toLowerCase().contains("fire in the") ||
|
|
||||||
text.toLowerCase().contains("fuego en la")) {
|
|
||||||
replyMessage(chat_id, message_id, "Fire in the taco bell");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(text.toLowerCase().contains("danger danger") ||
|
|
||||||
text.toLowerCase().contains("danger, danger")) {
|
|
||||||
replyMessage(chat_id, message_id, "HIGH VOLTAGE");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(text.toLowerCase().equals("when we touch")) {
|
|
||||||
replyMessage(chat_id, message_id, "WHEN WE KISS");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean memes = false;
|
|
||||||
|
|
||||||
//VAPORWAVE AESTHETICS
|
|
||||||
if (text.toUpperCase().contains("AESTHETICS") ||
|
|
||||||
text.toUpperCase().contains("A E S T H E T I C S") ||
|
|
||||||
text.toUpperCase().contains("\uFEFFAESTHETICS")) {
|
|
||||||
sendMessage(chat_id, "\uFEFFVAPORWAVE");
|
|
||||||
memes = true;
|
|
||||||
}
|
|
||||||
if (text.toUpperCase().contains("VAPORWAVE") ||
|
|
||||||
text.toUpperCase().contains("V A P O R W A V E") ||
|
|
||||||
text.toUpperCase().contains("\uFEFFVAPORWAVE")) {
|
|
||||||
sendMessage(chat_id, "\uFEFFAESTHETICS");
|
|
||||||
memes = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//BIENE ALESSIO
|
|
||||||
if (text.toLowerCase().contains("biene")) {
|
|
||||||
sendMessage(chat_id, "Alessio");
|
|
||||||
memes = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//AYY LMAO
|
|
||||||
if(text.toLowerCase().contains("lmao")) {
|
|
||||||
sendMessage(chat_id, "ayy");
|
|
||||||
memes = true;
|
|
||||||
} else if (text.toLowerCase().contains("ayy")) {
|
|
||||||
sendMessage(chat_id, "lmao");
|
|
||||||
memes = true;
|
|
||||||
} else if (text.toLowerCase().contains("qyy")) {
|
|
||||||
replyMessage(chat_id, message_id, ">failing this hard");
|
|
||||||
memes = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//EXODIA
|
|
||||||
if (text.toLowerCase().equals("exodia")) {
|
|
||||||
sendMessage(chat_id, "ANIQUILA");
|
|
||||||
memes = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//H3H3
|
|
||||||
if(text.toLowerCase().contains("h3h3")) {
|
|
||||||
sendMessage(chat_id, "wow ethan, great moves, keep it up! Proud of you");
|
|
||||||
memes = true;
|
|
||||||
}
|
|
||||||
if(text.toLowerCase().contains("imethanbradberry")) {
|
|
||||||
replyMessage(chat_id, message_id, "CALM DOWN *fixes hair*");
|
|
||||||
memes = true;
|
|
||||||
} else if(text.toLowerCase().contains("bradberry")) {
|
|
||||||
sendMessage(chat_id, "IMETHANBRADBERRY");
|
|
||||||
memes = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//REMOVE KEBAB
|
|
||||||
if(text.toLowerCase().contains("kebab")) {
|
|
||||||
sendMessage(chat_id, "https://www.youtube.com/watch?v=ocW3fBqPQkU");
|
|
||||||
}
|
|
||||||
|
|
||||||
//ME VOY DEL WHATSAPP - CIM LABS EXCLUSIVE
|
|
||||||
if(text.toLowerCase().equals("me voy") && (Long.toString(chat_id).equals("-1001036575277"))) {
|
|
||||||
sendMessage(chat_id, "\"Me voy del Whatsapp, adiós.\"");
|
|
||||||
memes = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//POLE - CIM LABS EXCLUSIVE
|
|
||||||
if(text.toLowerCase().contains("pole") && (Long.toString(chat_id).equals("-1001036575277"))) {
|
|
||||||
sendMessage(chat_id, "#VylBot2016 \"Make The Pole Great Again(tm)\"");
|
|
||||||
memes = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(memes) return;
|
|
||||||
|
|
||||||
if(mention) {
|
|
||||||
replyMessage(chat_id, message_id, "Ese soy yo");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleText(long chat_id, int message_id, String user, String name, String text) throws UnirestException {
|
|
||||||
boolean mention = iWasMentioned(text);
|
|
||||||
|
|
||||||
if( (mention) && ( (text.toLowerCase().contains("say goodbye")) ||
|
|
||||||
(text.toLowerCase().contains("despídete")) ||
|
|
||||||
(text.toLowerCase().contains("despidete")) ||
|
|
||||||
(text.toLowerCase().contains("adiós")) ||
|
|
||||||
(text.toLowerCase().contains("adios")) ) ) {
|
|
||||||
System.out.println("Time to say goodbye");
|
|
||||||
bye(chat_id);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
else handleWords(chat_id, message_id, text, mention);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleNewParticipant(long chat_id, JSONObject participant) throws UnirestException {
|
|
||||||
if(participant.has("username")) System.out.println("I see there's a new chat participant called " +
|
|
||||||
participant.getString("first_name") + " of username " +
|
|
||||||
participant.getString("username"));
|
|
||||||
else System.out.println("I see there's a new chat participant called " +
|
|
||||||
participant.getString("first_name") );
|
|
||||||
|
|
||||||
if(participant.has("username") && participant.getString("username").equals(me())) {
|
|
||||||
sendMessage(chat_id, "Encantado de conoceros, please be gentle~");
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
sendMessage(chat_id, "Buenas, " + participant.getString("first_name"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleExpulsion(long chat_id, JSONObject participant) throws UnirestException {
|
|
||||||
if(participant.has("username")) System.out.println("I see that " + participant.getString("first_name") +
|
|
||||||
" of username " + participant.getString("username") +
|
|
||||||
" has been kicked.");
|
|
||||||
else System.out.println("I see that " + participant.getString("first_name") +
|
|
||||||
" (of no username) has been kicked.");
|
|
||||||
|
|
||||||
if(participant.has("username") && participant.getString("username").equals(me())) {
|
|
||||||
complainAboutExpulsion();
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
sendMessage(chat_id, "Rip " + participant.getString("first_name"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void complainAboutExpulsion() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//--- other auxiliar functions
|
|
||||||
|
|
||||||
private boolean iWasMentioned(String text) {
|
|
||||||
for(int i = 0; i < names.length; i++) {
|
|
||||||
if(text.toLowerCase().contains(names[i].toLowerCase())) {
|
|
||||||
System.out.println("They mentioned me");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String me() {
|
|
||||||
return USERNAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String myName() {
|
|
||||||
return "Vylion's Testbot";
|
|
||||||
}
|
|
||||||
|
|
||||||
private String myToken() {
|
|
||||||
return TOKEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void bye(long chat_id) throws UnirestException {
|
|
||||||
sendMessage(chat_id, "Smell ya later, nerds");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
320
src/main/Vylbot.java
Normal file
320
src/main/Vylbot.java
Normal file
|
@ -0,0 +1,320 @@
|
||||||
|
package main;
|
||||||
|
|
||||||
|
import com.mashape.unirest.http.HttpResponse;
|
||||||
|
import com.mashape.unirest.http.JsonNode;
|
||||||
|
import com.mashape.unirest.http.Unirest;
|
||||||
|
import com.mashape.unirest.http.exceptions.UnirestException;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by vylion 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 BASE_URL = "https://api.telegram.org/bot" + TOKEN;
|
||||||
|
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_POLE = COMMAND_HELP + 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_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;
|
||||||
|
private static final int WARNING_NO_GROUP = WARNING_NOT_IMPLEMENTED + 1;
|
||||||
|
private static final int WARNING_COUNT_END = WARNING_NO_GROUP + 1;
|
||||||
|
|
||||||
|
private ArrayList<String> commands;
|
||||||
|
|
||||||
|
public Vylbot() {
|
||||||
|
|
||||||
|
commands.add(COMMAND_START, "/start");
|
||||||
|
commands.add(COMMAND_HELP, "/help");
|
||||||
|
commands.add(COMMAND_POLE, "/pole");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return USERNAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getToken() {
|
||||||
|
return TOKEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String myName() {
|
||||||
|
return "Vylbot";
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- Http handling
|
||||||
|
|
||||||
|
public HttpResponse<JsonNode> sendMessage(Long chatId, String text) throws UnirestException {
|
||||||
|
System.out.println("Sending message: \n" + text + "\n");
|
||||||
|
|
||||||
|
return Unirest.post(BASE_URL + "/sendMessage")
|
||||||
|
.field("chat_id", chatId)
|
||||||
|
.field("text", text)
|
||||||
|
.asJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse<JsonNode> sendMessage(Long chatId, String text, String parseMode) throws UnirestException {
|
||||||
|
System.out.println("Sending message: \n" + text + "\n");
|
||||||
|
|
||||||
|
return Unirest.post(BASE_URL + "/sendMessage")
|
||||||
|
.field("chat_id", chatId)
|
||||||
|
.field("text", text)
|
||||||
|
.field("parse_mode", parseMode)
|
||||||
|
.asJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse<JsonNode> sendMessage(String channel, String text) throws UnirestException {
|
||||||
|
System.out.println("Sending message to " + channel + ": \n" + text + "\n");
|
||||||
|
|
||||||
|
return Unirest.post(BASE_URL + "/sendMessage")
|
||||||
|
.field("chat_id", channel)
|
||||||
|
.field("text", text)
|
||||||
|
.asJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse<JsonNode> sendMessage(String channel, String text, String parseMode) throws UnirestException {
|
||||||
|
System.out.println("Sending message to " + channel + ": \n" + text + "\n");
|
||||||
|
|
||||||
|
return Unirest.post(BASE_URL + "/sendMessage")
|
||||||
|
.field("chat_id", channel)
|
||||||
|
.field("text", text)
|
||||||
|
.field("parse_mode", parseMode)
|
||||||
|
.asJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse<JsonNode> replyMessage(Long chatId, Integer repliedMessage, String text) throws UnirestException {
|
||||||
|
System.out.println("Sending message: \n" + text + "\n");
|
||||||
|
|
||||||
|
return Unirest.post(BASE_URL + "/sendMessage")
|
||||||
|
.field("chat_id", chatId)
|
||||||
|
.field("text", text)
|
||||||
|
.field("reply_to_message_id", repliedMessage)
|
||||||
|
.asJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse<JsonNode> getUpdates(Integer offset) throws UnirestException {
|
||||||
|
return Unirest.post(BASE_URL + "/getUpdates")
|
||||||
|
.field("offset", offset)
|
||||||
|
.asJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse<JsonNode> getUpdates(Integer offset, Integer timeout) throws UnirestException {
|
||||||
|
return Unirest.post(BASE_URL + "/getUpdates")
|
||||||
|
.field("offset", offset)
|
||||||
|
.field("timeout", timeout)
|
||||||
|
.asJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- bot thinking
|
||||||
|
|
||||||
|
public void run() throws UnirestException {
|
||||||
|
int last_upd_id = 0;
|
||||||
|
HttpResponse<JsonNode> response;
|
||||||
|
System.out.println("Listening.\n");
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
response = getUpdates(last_upd_id++);
|
||||||
|
if (response.getStatus() == 200) {
|
||||||
|
JSONArray responses = response.getBody().getObject().getJSONArray("result");
|
||||||
|
if (responses.isNull(0)) continue;
|
||||||
|
else last_upd_id = responses.getJSONObject(responses.length() - 1).getInt("update_id") + 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < responses.length(); i++) {
|
||||||
|
JSONObject message = responses.getJSONObject(i).getJSONObject("message");
|
||||||
|
|
||||||
|
processMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
int message_id = message.getInt("message_id");
|
||||||
|
String chatType = message.getJSONObject("chat").getString("type");
|
||||||
|
String name = message.getJSONObject("from").getString("first_name");
|
||||||
|
|
||||||
|
//Declare basic optional variables
|
||||||
|
String user = 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");
|
||||||
|
|
||||||
|
//Console log
|
||||||
|
boolean full_log = true;
|
||||||
|
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 " + name +
|
||||||
|
" (chat id: " + chat_id + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Actually read the message
|
||||||
|
if (message.has("text")) {
|
||||||
|
String text = message.getString("text");
|
||||||
|
|
||||||
|
if (text.startsWith("/")) {
|
||||||
|
if (user == null) sendMessage(chat_id, getInformation(WARNING_NO_USERNAME));
|
||||||
|
else handleInput(chat_id, chatType.contains("group"), user, text);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--- Command handling
|
||||||
|
|
||||||
|
private void handleInput(long chat_id, boolean group, String user, String text) throws UnirestException {
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
else {
|
||||||
|
if(group) handleCommand(chat_id, i, user, text);
|
||||||
|
else handlePrivateCommand(chat_id, i, user, text);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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_POLE:
|
||||||
|
sendMessage(chat_id, getInformation(WARNING_NOT_IMPLEMENTED));
|
||||||
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
default: 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;
|
||||||
|
|
||||||
|
case COMMAND_HELP:
|
||||||
|
sendMessage(chat_id, getInformation(COMMAND_HELP));
|
||||||
|
return;
|
||||||
|
|
||||||
|
default:
|
||||||
|
sendMessage(chat_id, getInformation(WARNING_NO_GROUP));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*private boolean exists(long chat_id, String user) {
|
||||||
|
return chats.containsKey(chat_id) && chats.get(chat_id).exists(user);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
private String getPole(long chat_id, String user) {
|
||||||
|
|
||||||
|
return "Congratulations " + user + ", you got the daily pole position! " +
|
||||||
|
"You win 200 pepes. Try again in 24 hours.";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*private void saveChat(long chat_id) throws SaveErrorException {
|
||||||
|
Persistence.getInstance().saveChat(chat_id, chats.get(chat_id));
|
||||||
|
chats.get(chat_id).setDirty(false);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
private String getInformation(int command) {
|
||||||
|
switch (command) {
|
||||||
|
case COMMAND_START:
|
||||||
|
return "Hola. Soy el bot personal de Vylion.";
|
||||||
|
|
||||||
|
case COMMAND_HELP:
|
||||||
|
return "Here is a list of the commands I accept:\n\n" +
|
||||||
|
"/register - Open a new vault in this chat under your name\n" +
|
||||||
|
"/newbet - Start the creation of a new bet. _*This will lock the " +
|
||||||
|
"bot in a bet creation until finished*_\n" +
|
||||||
|
"/done - While on a bet creation, it finishes it and asks you to " +
|
||||||
|
"designate the bet moderator. _*Only the bet creator can use " +
|
||||||
|
"this*_\n" +
|
||||||
|
"/checkbet - Shows the current state of the bet.\n" +
|
||||||
|
"/closebet - If there's a current active bet, it finishes it " +
|
||||||
|
"and let's you choose the outcome. _*Only the bet moderator " +
|
||||||
|
"can use this*_\n" +
|
||||||
|
"/cancelbet - If something went wrong, it let's you cancel a bet. " +
|
||||||
|
"_*Only the bet creator (during creation) or the bet moderator " +
|
||||||
|
"(if creation has finished) can use this*_\n" +
|
||||||
|
"/me - Tells your vault's contents.\n" +
|
||||||
|
"/give [username] [amount] - Let's you altruistically give money " +
|
||||||
|
"to another user.\n" +
|
||||||
|
"/ranking - Shows the top 5 richest players.\n" +
|
||||||
|
"\nRemember: this bot is geared towards group chats and you won't " +
|
||||||
|
"be able to use me in a one-on-one chat.\n" +
|
||||||
|
"Also, if I die during the creation of a bet, you can expect me " +
|
||||||
|
"not to remember it.";
|
||||||
|
|
||||||
|
case WARNING_UNKNOWN_ERROR:
|
||||||
|
return "Algo ha ido MUY mal.";
|
||||||
|
|
||||||
|
case WARNING_NOT_IMPLEMENTED:
|
||||||
|
return "Aún no está implementado.";
|
||||||
|
|
||||||
|
case WARNING_NO_GROUP:
|
||||||
|
return "Debes estar en un grupo para poder hacer eso.";
|
||||||
|
|
||||||
|
case WARNING_NO_USERNAME:
|
||||||
|
return "No tienes nombre de usuario. Regístrate con uno en la " +
|
||||||
|
"configuración de Telegram para poder participar.";
|
||||||
|
|
||||||
|
default: return "Entrada incorrecta.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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_POLE:
|
||||||
|
return commands.get(COMMAND_POLE);
|
||||||
|
|
||||||
|
default: return "Entrada incorrecta.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,5 +8,6 @@
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="library" name="unirest-java-1.4.10-SNAPSHOT-jar-with-dependencies" level="project" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
Reference in a new issue