cambio a html

This commit is contained in:
2025-03-28 16:41:01 +01:00
parent d310fea974
commit 0dce6d7606
3 changed files with 47 additions and 22 deletions

View File

@ -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;

View File

@ -85,6 +85,24 @@ public class TelegramBot extends TelegramLongPollingBot {
}
}
protected void sendMessageHtml(String chatId, String text) {
List<String> 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");
String mensajeInicio = "<i>Analizando noticias...esto puede tardar un poco, ten paciencia...</i>";
String mensajeError = "<b>Lo siento...</b> se me ha licuado el cerebro... Mira los logs para saber más.";
sendMessageHtml(chatId, mensajeInicio);
try {
List<NewsArticle> noticias = scrapper.searchNews(query);
if (noticias.isEmpty()) {
sendMessage(chatId, "No encontré noticias sobre: *" + escapeMarkdown(query) + "*");
sendMessageHtml(chatId, "No encontré noticias sobre: <b>" + query + "</b>");
return;
}
StringBuilder mensaje = new StringBuilder("📰 *Noticias sobre " + escapeMarkdown(query) + "*:\n\n");
StringBuilder mensaje = new StringBuilder("📰 <b>Noticias sobre " + query + ":</b>\n\n");
for (NewsArticle noticia : noticias) {
mensaje.append("🔹 [")
.append(escapeMarkdown(noticia.getTitle()))
.append("](")
mensaje.append("🔹 <a href=\"")
.append(noticia.getUrl())
.append(")\n");
.append("\">")
.append(noticia.getTitle())
.append("</a>\n");
}
sendMessage(chatId, mensaje.toString());
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, "<pre>" + e.toString() + "</pre>");
}
}

View File

@ -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";
}
}
}*/
}