This commit is contained in:
2025-04-01 16:28:10 +02:00
parent 2e78736b63
commit eea2d8ae2f
2 changed files with 14 additions and 55 deletions

View File

@ -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 = "<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.";
System.out.println(mensajeInicio);
try {
List<NewsArticle> noticias = scraper.searchNews(query);
if (noticias.isEmpty()) {
System.out.println("No encontré noticias sobre: <b>" + query + "</b>");
return;
}
StringBuilder mensaje = new StringBuilder("📰 <b>Noticias sobre " + query + ":</b>\n\n");
for (NewsArticle noticia : noticias) {
mensaje.append("<a href=\"")
.append(noticia.getUrl())
.append("\">")
.append(noticia.getTitle())
.append("</a>\n");
}
System.out.println(mensaje.toString());
} catch (Exception e) {
System.out.println(mensajeError);
System.out.println("<pre>" + e.toString() + "</pre>");
}
}
}

View File

@ -93,17 +93,13 @@ public class TelegramBot extends TelegramLongPollingBot {
}
protected void sendMessageHtml(String chatId, String text, Integer threadId) {
List<String> 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);
}
@ -114,7 +110,6 @@ public class TelegramBot extends TelegramLongPollingBot {
e.printStackTrace();
}
}
}
@Scheduled(cron = "0 0 13,20 * * ?", zone = "Europe/Paris")
@ -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<NewsArticle> 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("<b>📰 Noticias sobre " + query + ":</b>\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("🔹 <a href=\"")
.append(noticia.getUrl()) // URL de la noticia
.append("\">")
.append(escapeHtml(noticia.getTitle())) // Título de la noticia
.append("</a>\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("<a([^>]+)(?<!/)>", "<a$1></a>");
return text;
return text.replace("&", "&amp;");
}
}