1

Estoy tratando de mandar un simple mensaje con un bot de Telegram pero siempre recibo error. La idea es simple, mandar un mensaje de "hola", pero no hay manera, siempre me sale el mismo mensaje de error.

Lo que tengo creado es una clase llamada Telegram:

public class Telegram {
    private static int   apiKey = #######;
    private static String apiHash="##################################";
    private static String telegramToken = "####################################################";

    public Telegram() {
    }

    public static int sendMessageToChannel(String message) throws IOException {
        String address = "https://api.telegram.org/bot" + telegramToken + "/sendMessage";
        String urlParameters = "chat_id=#######&text=" + URLEncoder.encode(message, "UTF-8");

        URL url = new URL(address);

        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
        httpsURLConnection.setRequestMethod("POST");
        httpsURLConnection.setRequestProperty("User-Agent", "Darilito/1.0");
        httpsURLConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        httpsURLConnection.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(httpsURLConnection.getOutputStream());

        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();
        return httpsURLConnection.getResponseCode();
   }    
}

Desde el Main llamo a ese método:

try {
            Telegram.sendMessageToChannel("hola");
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(Ventana1.class.getName()).log(Level.SEVERE, null, ex);
}

Sin embargo, siempre recibo el mismo error:

run: abr 11, 2019 11:39:30 AM Ventanas.Ventana1 Grave: null java.net.UnknownHostException: api.telegram.org at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618) at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160) at sun.net.NetworkClient.doConnect(NetworkClient.java:180) at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) at sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:275) at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1091) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250) at main.Telegram.sendMessageToChannel(Telegram.java:46) at Ventanas.Ventana1.(Ventana1.java:125) at Ventanas.Ventana1$1.run(Ventana1.java:115) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:703) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

También he probado a realizarlo mediante un hilo a parte, pero el error siempre es el mismo.

¿Alguien puede decirme que ocurre por favor?

Un saludo y gracias.

2
  • ¿Qué te sale si haces desde línea de commandos *ping api.telegram.org"? Parece un problema con la configuración de tu red (¿DNS?)
    – SJuan76
    el 11 abr. 2019 a las 10:05
  • C:\Users\USUARIO>ping api.telegram.org La solicitud de ping no pudo encontrar el host api.telegram.org. Compruebe el nombre y vuelva a intentarlo. Lo estoy haciendo desde una "empresa", no tengo acceso a la configuración DNS...quiza haya algo que este bloqueando la conexión... Haré la misma prueba desde casa luego. Gracias! el 11 abr. 2019 a las 10:51

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.