package com.ruoyi.web.controller.common;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class TelegramMessageSender {

    private static final String BOT_TOKEN = "7787908640:AAHkhyIyf-i6eBYxytpZlOihim-nl9iHITs";
    private static final String CHAT_ID = "@7427893108"; // 替换为你的频道用户名或ID
    private static final String MESSAGE = "Hello from Java POST request!"; // 你的消息内容

    public static void main(String[] args) {
        try {
            // 用于发送消息的Telegram API URL
            String urlString = "https://api.telegram.org/bot" + BOT_TOKEN + "/sendMessage";
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // 设置请求方式为POST
            conn.setRequestMethod("POST");
            // 允许向连接写入数据
            conn.setDoOutput(true);

            // 构造POST请求体
            String postData = "chat_id=" + CHAT_ID + "&text=" + MESSAGE;

            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = postData.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 获取响应码，200表示成功
            int responseCode = conn.getResponseCode();
            System.out.println("Response Code : " + responseCode);

            if(responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println("Message sent successfully.");
            } else {
                System.out.println("Failed to send message.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
