-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTelegram.java
More file actions
executable file
·211 lines (176 loc) · 6.18 KB
/
Telegram.java
File metadata and controls
executable file
·211 lines (176 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package de.Linus122.Telegram;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.Linus122.TelegramChat.TelegramChat;
import de.Linus122.TelegramComponents.Chat;
import de.Linus122.TelegramComponents.ChatMessageToMc;
import de.Linus122.TelegramComponents.ChatMessageToTelegram;
import de.Linus122.TelegramComponents.Update;
import org.bukkit.Bukkit;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class Telegram {
public JsonObject authJson;
public boolean connected = false;
static int lastUpdate = 0;
public String token;
private List<TelegramActionListener> listeners = new ArrayList<TelegramActionListener>();
private final String API_URL_GETME = "https://api.telegram.org/bot%s/getMe";
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();
private Set<Long> sentWarnings = ConcurrentHashMap.newKeySet();
public void addListener(TelegramActionListener actionListener) {
listeners.add(actionListener);
}
public boolean auth(String token) {
this.token = token;
return reconnect();
}
public boolean reconnect() {
try {
JsonObject obj = sendGet(String.format(API_URL_GETME, token));
authJson = obj;
System.out.print("[Telegram] Established a connection with the telegram servers.");
connected = true;
return true;
} catch (Exception e) {
connected = false;
System.out.print("[Telegram] Sorry, but could not connect to Telegram servers. The token could be wrong.");
return false;
}
}
public boolean getUpdate() {
JsonObject up;
try {
up = sendGet(String.format(API_URL_GETUPDATES, TelegramChat.getBackend().getToken(), lastUpdate + 1));
} catch (IOException e) {
return false;
}
if (up == null) {
return false;
}
if (up.has("result")) {
for (JsonElement ob : up.getAsJsonArray("result")) {
if (ob.isJsonObject()) {
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()) {
// skip
} else if (!chat.isPrivate()) {
// group chat
long id = chat.getId();
if (!TelegramChat.getBackend().chat_ids.contains(id) && TelegramChat.getBackend().chat_ids.size() < 1)
TelegramChat.getBackend().chat_ids.add(id);
if (update.getMessage().getText() != null) {
String text = update.getMessage().getText();
handleUserMessage(text, update);
}
}
}
}
}
}
return true;
}
public void handleUserMessage(String text, Update update) {
Chat chat = update.getMessage().getChat();
long chatId = update.getMessage().getFrom().getId();
if (TelegramChat.getBackend().getLinkCodes().containsKey(text)) {
// LINK
TelegramChat.link(TelegramChat.getBackend().getUUIDFromLinkCode(text), chatId);
TelegramChat.getBackend().removeLinkCode(text);
} else if (TelegramChat.getBackend().containsChatId(chatId)) {
ChatMessageToMc chatMsg = new ChatMessageToMc(
TelegramChat.getBackend().getUUIDFromUserID(chatId), text, chat.getId());
for (TelegramActionListener actionListener : listeners) {
actionListener.onSendToMinecraft(chatMsg);
}
if(!chatMsg.isCancelled()){
TelegramChat.sendToMC(chatMsg);
}
} else {
if (!sentWarnings.contains(chat.getId())) {
this.sendMsg(chat.getId(), Utils.formatMSG("need-to-link")[0]);
sentWarnings.add(chat.getId());
}
}
}
public void sendMsg(long id, String msg) {
ChatMessageToTelegram chat = new ChatMessageToTelegram();
chat.chat_id = id;
chat.text = msg;
sendMsg(chat);
}
public void sendMsg(ChatMessageToTelegram chat) {
for (TelegramActionListener actionListener : listeners) {
actionListener.onSendToTelegram(chat);
}
Gson gson = new Gson();
if(!chat.isCancelled()){
post("sendMessage", gson.toJson(chat, ChatMessageToTelegram.class));
}
}
public void sendAll(final ChatMessageToTelegram chat) {
Bukkit.getScheduler().runTaskAsynchronously(TelegramChat.getInstance(), () -> {
for (long id : TelegramChat.getBackend().chat_ids) {
chat.chat_id = id;
sendMsg(chat);
}
});
}
public void post(String method, String json) {
try {
String body = json;
URL url = new URL(String.format(API_URL_GENERAL, TelegramChat.getBackend().getToken(), method));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/json; ; Charset=UTF-8");
connection.setRequestProperty("Content-Length", String.valueOf(body.length()));
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
writer.write(body);
writer.close();
wr.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
writer.close();
reader.close();
} catch (Exception e) {
reconnect();
System.out.print("[Telegram] Disconnected from Telegram, reconnect...");
}
}
public JsonObject sendGet(String url) throws IOException {
String a = url;
URL url2 = new URL(a);
URLConnection conn = url2.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String all = "";
String inputLine;
while ((inputLine = br.readLine()) != null) {
all += inputLine;
}
br.close();
JsonParser parser = new JsonParser();
return parser.parse(all).getAsJsonObject();
}
}