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) { public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(InversionitasBotApplication.class, args); ApplicationContext context = SpringApplication.run(InversionitasBotApplication.class, args);
InversionitasBotApplication app = context.getBean(InversionitasBotApplication.class); InversionitasBotApplication app = context.getBean(InversionitasBotApplication.class);
/*GoogleNewsScraperService scraper = new GoogleNewsScraperService();
System.out.println(scraper.searchNews("Farma"));
buscarNoticiasYEnviar("Farma", scraper);*/
app.initBot(); 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) { protected void sendMessageHtml(String chatId, String text, Integer threadId) {
List<String> partes = dividirMensaje(text); String parte = this.escapeHtml(text);
for (String parte : partes) {
logger.info(parte);
parte = this.escapeHtml(parte);
SendMessage sendMessage = new SendMessage(); SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(chatId); sendMessage.setChatId(chatId);
sendMessage.setText(parte); sendMessage.setText(parte);
sendMessage.setParseMode("HTML"); // Se debe usar HTML si el contenido es HTML 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) { if (threadId != null) {
sendMessage.setMessageThreadId(threadId); sendMessage.setMessageThreadId(threadId);
} }
@ -113,7 +109,6 @@ public class TelegramBot extends TelegramLongPollingBot {
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
} }
@ -186,34 +181,31 @@ public class TelegramBot extends TelegramLongPollingBot {
} }
private void buscarNoticiasYEnviar(String chatId, String query) { 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."; 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 { try {
List<NewsArticle> noticias = scrapper.searchNews(query); List<NewsArticle> noticias = scrapper.searchNews(query);
if (noticias.isEmpty()) { if (noticias.isEmpty()) {
sendMessage(chatId, "No encontré noticias sobre: " + query); sendMessageHtml(chatId, "No encontré noticias sobre: " + query);
return; 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) { for (NewsArticle noticia : noticias) {
mensaje//.append("[") mensaje.append("🔹 <a href=\"")
.append("🔹 ") .append(noticia.getUrl()) // URL de la noticia
.append(escapeMarkdown(noticia.getTitle())) .append("\">")
.append("\n")// Escapando el título de la noticia .append(escapeHtml(noticia.getTitle())) // Título de la noticia
//.append("](") .append("</a>\n");
//.append(escapeMarkdown(noticia.getUrl())) // Escapando la URL de la noticia
//.append(")\n")
;
} }
sendMessage(chatId, mensaje.toString()); sendMessageHtml(chatId, mensaje.toString());
} catch (Exception e) { } catch (Exception e) {
sendMessage(chatId, escapeMarkdown(mensajeError)); sendMessageHtml(chatId, mensajeError);
} }
} }
@ -226,7 +218,6 @@ public class TelegramBot extends TelegramLongPollingBot {
} }
private String escapeHtml(String text) { private String escapeHtml(String text) {
text = text.replaceAll("<a([^>]+)(?<!/)>", "<a$1></a>"); return text.replace("&", "&amp;");
return text;
} }
} }