Modularized Telegram API, fixed #12

This commit is contained in:
mastercake10 2018-03-05 08:37:48 +01:00
parent 7bbcf58caa
commit 06923bec46
9 changed files with 228 additions and 40 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>TelegramChat</groupId>
<artifactId>TelegramChat</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<name>TelegramChat</name>
<url>https://www.spigotmc.org/resources/telegramchat.16576/</url>
<repositories>

View File

@ -25,7 +25,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import com.google.gson.Gson;
import de.Linus122.Metrics.Metrics;
import de.Linus122.TelegramComponents.Chat;
import de.Linus122.TelegramComponents.ChatMessageToTelegram;
import de.Linus122.TelegramComponents.ChatMessageToMc;
public class Main extends JavaPlugin implements Listener {
@ -44,6 +44,7 @@ public class Main extends JavaPlugin implements Listener {
Bukkit.getPluginCommand("telegram").setExecutor(new TelegramCmd());
Bukkit.getPluginCommand("linktelegram").setExecutor(new LinkTelegramCmd());
Bukkit.getPluginManager().registerEvents(this, this);
File dir = new File("plugins/TelegramChat/");
dir.mkdir();
data = new Data();
@ -60,6 +61,7 @@ public class Main extends JavaPlugin implements Listener {
e.printStackTrace();
}
}
telegramHook = new Telegram();
telegramHook.auth(data.getToken());
@ -155,7 +157,7 @@ public class Main extends JavaPlugin implements Listener {
if (!this.getConfig().getBoolean("enable-joinquitmessages"))
return;
if (telegramHook.connected) {
Chat chat = new Chat();
ChatMessageToTelegram chat = new ChatMessageToTelegram();
chat.parse_mode = "Markdown";
chat.text = Utils.formatMSG("join-message", e.getPlayer().getName())[0];
telegramHook.sendAll(chat);
@ -167,7 +169,7 @@ public class Main extends JavaPlugin implements Listener {
if (!this.getConfig().getBoolean("enable-deathmessages"))
return;
if (telegramHook.connected) {
Chat chat = new Chat();
ChatMessageToTelegram chat = new ChatMessageToTelegram();
chat.parse_mode = "Markdown";
chat.text = Utils.formatMSG("death-message", e.getDeathMessage())[0];
telegramHook.sendAll(chat);
@ -179,7 +181,7 @@ public class Main extends JavaPlugin implements Listener {
if (!this.getConfig().getBoolean("enable-joinquitmessages"))
return;
if (telegramHook.connected) {
Chat chat = new Chat();
ChatMessageToTelegram chat = new ChatMessageToTelegram();
chat.parse_mode = "Markdown";
chat.text = Utils.formatMSG("quit-message", e.getPlayer().getName())[0];
telegramHook.sendAll(chat);
@ -191,7 +193,7 @@ public class Main extends JavaPlugin implements Listener {
if (!this.getConfig().getBoolean("enable-chatmessages"))
return;
if (telegramHook.connected) {
Chat chat = new Chat();
ChatMessageToTelegram chat = new ChatMessageToTelegram();
chat.parse_mode = "Markdown";
chat.text = Utils
.escape(Utils.formatMSG("general-message-to-telegram", e.getPlayer().getName(), e.getMessage())[0])

View File

@ -17,8 +17,10 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.Linus122.TelegramComponents.ChatMessageToTelegram;
import de.Linus122.TelegramComponents.Chat;
import de.Linus122.TelegramComponents.ChatMessageToMc;
import de.Linus122.TelegramComponents.Update;
public class Telegram {
public JsonObject authJson;
@ -33,6 +35,8 @@ public class Telegram {
private final String API_URL_GETUPDATES = "https://api.telegram.org/bot%s/getUpdates?offset=%d";
private final String API_URL_GENERAL = "https://api.telegram.org/bot%s/%s";
private Gson gson = new Gson();
public void addListener(TelegramActionListener actionListener) {
listeners.add(actionListener);
}
@ -69,50 +73,50 @@ public class Telegram {
if (up.has("result")) {
for (JsonElement ob : up.getAsJsonArray("result")) {
if (ob.isJsonObject()) {
JsonObject obj = (JsonObject) ob;
if (obj.has("update_id")) {
lastUpdate = obj.get("update_id").getAsInt();
}
if (obj.has("message")) {
JsonObject chat = obj.getAsJsonObject("message").getAsJsonObject("chat");
if (chat.get("type").getAsString().equals("private")) {
int id = chat.get("id").getAsInt();
if (!Main.getBackend().ids.contains(id))
Main.getBackend().ids.add(id);
Update update = gson.fromJson(ob, Update.class);
if(lastUpdate == update.getUpdate_id()) return true;
lastUpdate = update.getUpdate_id();
if (obj.getAsJsonObject("message").has("text")) {
String text = obj.getAsJsonObject("message").get("text").getAsString();
if (update.getMessage() != null) {
Chat chat = update.getMessage().getChat();
if (chat.isPrivate()) {
if (!Main.getBackend().ids.contains(chat.getId()))
Main.getBackend().ids.add(chat.getId());
if (update.getMessage().getText() != null) {
String text = update.getMessage().getText();
if (text.length() == 0)
return true;
if (text.equals("/start")) {
if (Main.getBackend().isFirstUse()) {
Main.getBackend().setFirstUse(false);
Chat chat2 = new Chat();
chat2.chat_id = id;
ChatMessageToTelegram chat2 = new ChatMessageToTelegram();
chat2.chat_id = chat.getId();
chat2.parse_mode = "Markdown";
chat2.text = Utils.formatMSG("setup-msg")[0];
this.sendMsg(chat2);
}
this.sendMsg(id, Utils.formatMSG("can-see-but-not-chat")[0]);
this.sendMsg(chat.getId(), Utils.formatMSG("can-see-but-not-chat")[0]);
} else if (Main.getBackend().getLinkCodes().containsKey(text)) {
// LINK
Main.link(Main.getBackend().getUUIDFromLinkCode(text), id);
Main.link(Main.getBackend().getUUIDFromLinkCode(text), chat.getId());
Main.getBackend().removeLinkCode(text);
} else if (Main.getBackend().getLinkedChats().containsKey(id)) {
} else if (Main.getBackend().getLinkedChats().containsKey(chat.getId())) {
ChatMessageToMc chatMsg = new ChatMessageToMc(
Main.getBackend().getUUIDFromChatID(id), text, id);
Main.getBackend().getUUIDFromChatID(chat.getId()), text, chat.getId());
for (TelegramActionListener actionListener : listeners) {
actionListener.onSendToMinecraft(chatMsg);
}
Main.sendToMC(chatMsg);
} else {
this.sendMsg(id, Utils.formatMSG("need-to-link")[0]);
this.sendMsg(chat.getId(), Utils.formatMSG("need-to-link")[0]);
}
}
} else if (chat.get("type").getAsString().equals("group")) {
int id = chat.get("id").getAsInt();
} else if (!chat.isPrivate()) {
int id = chat.getId();
if (!Main.getBackend().ids.contains(id))
Main.getBackend().ids.add(id);
}
@ -125,23 +129,23 @@ public class Telegram {
}
public void sendMsg(int id, String msg) {
Chat chat = new Chat();
ChatMessageToTelegram chat = new ChatMessageToTelegram();
chat.chat_id = id;
chat.text = msg;
sendMsg(chat);
}
public void sendMsg(Chat chat) {
public void sendMsg(ChatMessageToTelegram chat) {
for (TelegramActionListener actionListener : listeners) {
actionListener.onSendToTelegram(chat);
}
Gson gson = new Gson();
post("sendMessage", gson.toJson(chat, Chat.class));
post("sendMessage", gson.toJson(chat, ChatMessageToTelegram.class));
}
public void sendAll(final Chat chat) {
public void sendAll(final ChatMessageToTelegram chat) {
new Thread(new Runnable() {
public void run() {
for (int id : Main.getBackend().ids) {

View File

@ -1,10 +1,10 @@
package de.Linus122.TelegramChat;
import de.Linus122.TelegramComponents.Chat;
import de.Linus122.TelegramComponents.ChatMessageToTelegram;
import de.Linus122.TelegramComponents.ChatMessageToMc;
public interface TelegramActionListener {
public void onSendToTelegram(Chat chat);
public void onSendToTelegram(ChatMessageToTelegram chat);
public void onSendToMinecraft(ChatMessageToMc chatMsg);
}

39
src/main/java/de/Linus122/TelegramComponents/Chat.java Executable file → Normal file
View File

@ -1,7 +1,32 @@
package de.Linus122.TelegramComponents;
public class Chat {
public String text;
public int chat_id;
public String parse_mode;
}
package de.Linus122.TelegramComponents;
public class Chat {
private int id;
private String type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
* isPrivate
* @return true for private, false for group chats
*/
public boolean isPrivate(){
return type.equals("private");
}
}

View File

@ -0,0 +1,7 @@
package de.Linus122.TelegramComponents;
public class ChatMessageToTelegram {
public String text;
public int chat_id;
public String parse_mode;
}

View File

@ -0,0 +1,49 @@
package de.Linus122.TelegramComponents;
public class Message {
private int message_id;
private User from;
private int date;
private Chat chat;
private String text;
public int getMessage_id() {
return message_id;
}
public void setMessage_id(int message_id) {
this.message_id = message_id;
}
public User getFrom() {
return from;
}
public void setFrom(User from) {
this.from = from;
}
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
public Chat getChat() {
return chat;
}
public void setChat(Chat chat) {
this.chat = chat;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}

View File

@ -0,0 +1,50 @@
package de.Linus122.TelegramComponents;
public class Update {
private int update_id;
private Message message;
private Message edited_message;
private Message channel_post;
private Message edited_channel_post;
public int getUpdate_id() {
return update_id;
}
public void setUpdate_id(int update_id) {
this.update_id = update_id;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public Message getEdited_message() {
return edited_message;
}
public void setEdited_message(Message edited_message) {
this.edited_message = edited_message;
}
public Message getChannel_post() {
return channel_post;
}
public void setChannel_post(Message channel_post) {
this.channel_post = channel_post;
}
public Message getEdited_channel_post() {
return edited_channel_post;
}
public void setEdited_channel_post(Message edited_channel_post) {
this.edited_channel_post = edited_channel_post;
}
}

View File

@ -0,0 +1,51 @@
package de.Linus122.TelegramComponents;
public class User {
private int id;
private boolean is_bot;
private String first_name;
private String last_name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isIs_bot() {
return is_bot;
}
public void setIs_bot(boolean is_bot) {
this.is_bot = is_bot;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private String username;
}