Skip to content

Commit

Permalink
Modularized Telegram API, fixed #12
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercake10 committed Mar 5, 2018
1 parent 7bbcf58 commit 06923be
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/de/Linus122/TelegramChat/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
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 {
Expand All @@ -44,6 +44,7 @@ public void onEnable() {
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();
Expand All @@ -60,6 +61,7 @@ public void onEnable() {
e.printStackTrace();
}
}

telegramHook = new Telegram();
telegramHook.auth(data.getToken());

Expand Down Expand Up @@ -155,7 +157,7 @@ public void onJoin(PlayerJoinEvent e) {
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);
Expand All @@ -167,7 +169,7 @@ public void onDeath(PlayerDeathEvent e) {
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);
Expand All @@ -179,7 +181,7 @@ public void onQuit(PlayerQuitEvent e) {
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);
Expand All @@ -191,7 +193,7 @@ public void onChat(AsyncPlayerChatEvent e) {
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])
Expand Down
56 changes: 30 additions & 26 deletions src/main/java/de/Linus122/TelegramChat/Telegram.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -69,50 +73,50 @@ public boolean getUpdate() {
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);

if (obj.getAsJsonObject("message").has("text")) {
String text = obj.getAsJsonObject("message").get("text").getAsString();
Update update = gson.fromJson(ob, Update.class);

if(lastUpdate == update.getUpdate_id()) return true;
lastUpdate = update.getUpdate_id();

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);
}
Expand All @@ -125,23 +129,23 @@ public boolean getUpdate() {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 changes: 32 additions & 7 deletions src/main/java/de/Linus122/TelegramComponents/Chat.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.Linus122.TelegramComponents;

public class ChatMessageToTelegram {
public String text;
public int chat_id;
public String parse_mode;
}
49 changes: 49 additions & 0 deletions src/main/java/de/Linus122/TelegramComponents/Message.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
50 changes: 50 additions & 0 deletions src/main/java/de/Linus122/TelegramComponents/Update.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Loading

0 comments on commit 06923be

Please sign in to comment.