f3rnandomoreno / spring-ai-telegram-task-manager

A telegram task manager chatbot using openAI API
0 stars 0 forks source link

send the message to parse the text to markdown in telegram #25

Closed f3rnandomoreno closed 2 weeks ago

f3rnandomoreno commented 2 weeks ago

it is an example, but we want to use the library not this way import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets;

public class TelegramBot {

private static final String TOKEN = "YOUR_BOT_TOKEN";
private static final String CHAT_ID = "YOUR_CHAT_ID";

public static void main(String[] args) {
    try {
        String message = "*Este texto es en negrita*"; // MarkdownV2 para negrita
        sendMessage(message);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void sendMessage(String message) throws Exception {
    String urlString = "https://api.telegram.org/bot" + TOKEN + "/sendMessage";
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");

    String jsonInputString = "{\"chat_id\":\"" + CHAT_ID + "\",\"text\":\"" + message + "\",\"parse_mode\":\"MarkdownV2\"}";

    try (OutputStream os = conn.getOutputStream()) {
        byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
        os.write(input, 0, input.length);
    }

    int code = conn.getResponseCode();
    if (code == HttpURLConnection.HTTP_OK) {
        System.out.println("Mensaje enviado correctamente");
    } else {
        System.out.println("Error al enviar el mensaje");
    }
}

}