-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramNotifier.java
More file actions
33 lines (26 loc) · 1.15 KB
/
TelegramNotifier.java
File metadata and controls
33 lines (26 loc) · 1.15 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
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class TelegramNotifier {
private static final String BOT_TOKEN = ""; // from BotFather
private static final String CHAT_ID = ""; // your ID
public static void sendMessage(String message) {
try {
String urlString = "https://api.telegram.org/bot" + BOT_TOKEN + "/sendMessage";
String data = "chat_id=" + CHAT_ID + "&text=" + java.net.URLEncoder.encode(message, "UTF-8");
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
try (OutputStream os = conn.getOutputStream()) {
os.write(data.getBytes(StandardCharsets.UTF_8));
}
conn.getInputStream().close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}