From 0dce6d7606c4298b180be826e6b1dcd4ac41c968 Mon Sep 17 00:00:00 2001 From: imunnic Date: Fri, 28 Mar 2025 16:41:01 +0100 Subject: [PATCH] cambio a html --- .../imunnic/inversionitasBot/NewsArticle.java | 4 ++ .../imunnic/inversionitasBot/TelegramBot.java | 57 +++++++++++++------ .../services/GoogleNewsScraperService.java | 8 +-- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/bot/src/main/java/es/imunnic/inversionitasBot/NewsArticle.java b/bot/src/main/java/es/imunnic/inversionitasBot/NewsArticle.java index 170342a..d910a67 100644 --- a/bot/src/main/java/es/imunnic/inversionitasBot/NewsArticle.java +++ b/bot/src/main/java/es/imunnic/inversionitasBot/NewsArticle.java @@ -6,6 +6,10 @@ public class NewsArticle { private String url; private String content; + public NewsArticle(String title, String description, String url) { + this(title,description,url,null); + } + public NewsArticle(String title, String description, String url, String content) { this.title = title; this.description = description; diff --git a/bot/src/main/java/es/imunnic/inversionitasBot/TelegramBot.java b/bot/src/main/java/es/imunnic/inversionitasBot/TelegramBot.java index d4d2737..c0c8e9d 100644 --- a/bot/src/main/java/es/imunnic/inversionitasBot/TelegramBot.java +++ b/bot/src/main/java/es/imunnic/inversionitasBot/TelegramBot.java @@ -85,6 +85,24 @@ public class TelegramBot extends TelegramLongPollingBot { } } + protected void sendMessageHtml(String chatId, String text) { + List partes = dividirMensaje(text); + + for (String parte : partes) { + SendMessage sendMessage = new SendMessage(); + sendMessage.setChatId(chatId); + sendMessage.setText(parte); + sendMessage.setParseMode("HTML"); + + try { + execute(sendMessage); + } catch (TelegramApiException e) { + e.printStackTrace(); + } + } + } + + @Scheduled(cron = "0 0 13,20 * * ?", zone = "Europe/Paris") private void mensajeProgramado(){ this.construirResumenNoticias(this.CHAT_ID); @@ -154,28 +172,31 @@ public class TelegramBot extends TelegramLongPollingBot { } private void buscarNoticiasYEnviar(String chatId, String query) { - sendMessage(chatId, "Analizando noticias esto puede tardar un poco ten paciencia"); - try{ - List noticias = scrapper.searchNews(query); + 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."; + sendMessageHtml(chatId, mensajeInicio); - if (noticias.isEmpty()) { - sendMessage(chatId, "No encontré noticias sobre: *" + escapeMarkdown(query) + "*"); - return; - } + try { + List noticias = scrapper.searchNews(query); - StringBuilder mensaje = new StringBuilder("📰 *Noticias sobre " + escapeMarkdown(query) + "*:\n\n"); - for (NewsArticle noticia : noticias) { - mensaje.append("🔹 [") - .append(escapeMarkdown(noticia.getTitle())) - .append("](") - .append(noticia.getUrl()) - .append(")\n"); - } + if (noticias.isEmpty()) { + sendMessageHtml(chatId, "No encontré noticias sobre: " + query + ""); + return; + } - sendMessage(chatId, mensaje.toString()); + StringBuilder mensaje = new StringBuilder("📰 Noticias sobre " + query + ":\n\n"); + for (NewsArticle noticia : noticias) { + mensaje.append("🔹 ") + .append(noticia.getTitle()) + .append("\n"); + } + + sendMessageHtml(chatId, mensaje.toString()); } catch (Exception e) { - sendMessage(chatId, "Lo siento se me ha licuado el cerebro Mira los logs para saber más"); - sendMessage(chatId, e.toString()); + sendMessageHtml(chatId, mensajeError); + sendMessageHtml(chatId, "
" + e.toString() + "
"); } } diff --git a/bot/src/main/java/es/imunnic/inversionitasBot/services/GoogleNewsScraperService.java b/bot/src/main/java/es/imunnic/inversionitasBot/services/GoogleNewsScraperService.java index f52dd9a..169867d 100644 --- a/bot/src/main/java/es/imunnic/inversionitasBot/services/GoogleNewsScraperService.java +++ b/bot/src/main/java/es/imunnic/inversionitasBot/services/GoogleNewsScraperService.java @@ -36,9 +36,9 @@ public class GoogleNewsScraperService { String link = entry.getLink(); String description = (entry.getDescription() != null) ? entry.getDescription().getValue() : "Sin descripción"; String finalUrl = resolveFinalUrl(link); - String content = extractArticleContent(finalUrl); + //String content = extractArticleContent(finalUrl); - newsList.add(new NewsArticle(title, description, finalUrl, content)); + newsList.add(new NewsArticle(title, description, finalUrl)); } } catch (Exception e) { e.printStackTrace(); @@ -58,7 +58,7 @@ public class GoogleNewsScraperService { } } - private String extractArticleContent(String url) { + /*private String extractArticleContent(String url) { try { Document doc = Jsoup.connect(url).userAgent(USER_AGENT).get(); Elements paragraphs = doc.select("article p, .post-content p, .entry-content p, .content p, #article-body p"); @@ -70,6 +70,6 @@ public class GoogleNewsScraperService { } catch (Exception e) { return "Error al extraer contenido"; } - } + }*/ }