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.