|
| 1 | +package de.rieckpil.blog; |
| 2 | + |
| 3 | +import javax.ws.rs.core.UriBuilder; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.http.HttpClient; |
| 6 | +import java.net.http.HttpRequest; |
| 7 | +import java.net.http.HttpResponse; |
| 8 | +import java.time.Duration; |
| 9 | + |
| 10 | +public class TelegramNotifier { |
| 11 | + |
| 12 | + private static final String CHAT_ID = "INSERT_HERE"; |
| 13 | + private static final String TOKEN = "INSERT_HERE"; |
| 14 | + |
| 15 | + public static void main(String[] args) throws IOException, InterruptedException { |
| 16 | + |
| 17 | + String message = "Hello World from Java 11"; |
| 18 | + |
| 19 | + HttpClient client = HttpClient.newBuilder() |
| 20 | + .connectTimeout(Duration.ofSeconds(5)) |
| 21 | + .version(HttpClient.Version.HTTP_2) |
| 22 | + .build(); |
| 23 | + |
| 24 | + UriBuilder builder = UriBuilder |
| 25 | + .fromUri("https://api.telegram.org") |
| 26 | + .path("/{token}/sendMessage") |
| 27 | + .queryParam("chat_id", CHAT_ID) |
| 28 | + .queryParam("text", message); |
| 29 | + |
| 30 | + HttpRequest request = HttpRequest.newBuilder() |
| 31 | + .GET() |
| 32 | + .uri(builder.build("bot" + TOKEN)) |
| 33 | + .timeout(Duration.ofSeconds(5)) |
| 34 | + .build(); |
| 35 | + |
| 36 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 37 | + |
| 38 | + System.out.println(response.statusCode()); |
| 39 | + System.out.println(response.body()); |
| 40 | + } |
| 41 | +} |
0 commit comments