diff --git a/bot/src/main/java/es/imunnic/inversionitasBot/InversionitasBotApplication.java b/bot/src/main/java/es/imunnic/inversionitasBot/InversionitasBotApplication.java index a149655..b80897b 100644 --- a/bot/src/main/java/es/imunnic/inversionitasBot/InversionitasBotApplication.java +++ b/bot/src/main/java/es/imunnic/inversionitasBot/InversionitasBotApplication.java @@ -21,9 +21,6 @@ public class InversionitasBotApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(InversionitasBotApplication.class, args); InversionitasBotApplication app = context.getBean(InversionitasBotApplication.class); - /*GoogleNewsScraperService scraper = new GoogleNewsScraperService(); - System.out.println(scraper.searchNews("Farma")); - buscarNoticiasYEnviar("Farma", scraper);*/ app.initBot(); } @@ -37,34 +34,5 @@ public class InversionitasBotApplication { } } - private static void buscarNoticiasYEnviar(String query, GoogleNewsScraperService scraper) { - String mensajeInicio = "Analizando noticias...esto puede tardar un poco, ten paciencia..."; - String mensajeError = "Lo siento... se me ha licuado el cerebro... Mira los logs para saber más."; - System.out.println(mensajeInicio); - - try { - List noticias = scraper.searchNews(query); - - if (noticias.isEmpty()) { - System.out.println("No encontré noticias sobre: " + query + ""); - return; - } - - StringBuilder mensaje = new StringBuilder("📰 Noticias sobre " + query + ":\n\n"); - for (NewsArticle noticia : noticias) { - mensaje.append("") - .append(noticia.getTitle()) - .append("\n"); - } - - System.out.println(mensaje.toString()); - } catch (Exception e) { - System.out.println(mensajeError); - System.out.println("
" + e.toString() + "
"); - } - } - } diff --git a/bot/src/main/java/es/imunnic/inversionitasBot/TelegramBot.java b/bot/src/main/java/es/imunnic/inversionitasBot/TelegramBot.java index 29f10e4..f00f81f 100644 --- a/bot/src/main/java/es/imunnic/inversionitasBot/TelegramBot.java +++ b/bot/src/main/java/es/imunnic/inversionitasBot/TelegramBot.java @@ -93,17 +93,13 @@ public class TelegramBot extends TelegramLongPollingBot { } protected void sendMessageHtml(String chatId, String text, Integer threadId) { - List partes = dividirMensaje(text); - - for (String parte : partes) { - logger.info(parte); - parte = this.escapeHtml(parte); + String parte = this.escapeHtml(text); SendMessage sendMessage = new SendMessage(); sendMessage.setChatId(chatId); sendMessage.setText(parte); sendMessage.setParseMode("HTML"); // Se debe usar HTML si el contenido es HTML - // Agregar el ID del tema si es distinto de null + if (threadId != null) { sendMessage.setMessageThreadId(threadId); } @@ -113,7 +109,6 @@ public class TelegramBot extends TelegramLongPollingBot { } catch (TelegramApiException e) { e.printStackTrace(); } - } } @@ -186,34 +181,31 @@ public class TelegramBot extends TelegramLongPollingBot { } private void buscarNoticiasYEnviar(String chatId, String query) { - String mensajeInicio = "Analizando noticias...esto puede tardar un poco, ten paciencia..."; + String mensajeInicio = "Analizando noticias... esto puede tardar un poco, ten paciencia..."; String mensajeError = "Lo siento... se me ha licuado el cerebro... Mira los logs para saber más."; - sendMessage(chatId, escapeMarkdown(mensajeInicio)); + sendMessageHtml(chatId, mensajeInicio); // Enviar mensaje inicial try { List noticias = scrapper.searchNews(query); if (noticias.isEmpty()) { - sendMessage(chatId, "No encontré noticias sobre: " + query); + sendMessageHtml(chatId, "No encontré noticias sobre: " + query); return; } - StringBuilder mensaje = new StringBuilder("📰 Noticias sobre " + query + ":\n\n"); + StringBuilder mensaje = new StringBuilder("📰 Noticias sobre " + query + ":\n\n"); for (NewsArticle noticia : noticias) { - mensaje//.append("[") - .append("🔹 ") - .append(escapeMarkdown(noticia.getTitle())) - .append("\n")// Escapando el título de la noticia - //.append("](") - //.append(escapeMarkdown(noticia.getUrl())) // Escapando la URL de la noticia - //.append(")\n") - ; + mensaje.append("🔹 ") + .append(escapeHtml(noticia.getTitle())) // Título de la noticia + .append("\n"); } - sendMessage(chatId, mensaje.toString()); + sendMessageHtml(chatId, mensaje.toString()); } catch (Exception e) { - sendMessage(chatId, escapeMarkdown(mensajeError)); + sendMessageHtml(chatId, mensajeError); } } @@ -226,7 +218,6 @@ public class TelegramBot extends TelegramLongPollingBot { } private String escapeHtml(String text) { - text = text.replaceAll("]+)(?", ""); - return text; + return text.replace("&", "&"); } }