cambio a html
This commit is contained in:
@ -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;
|
||||
|
@ -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");
|
||||
try{
|
||||
List<NewsArticle> noticias = scrapper.searchNews(query);
|
||||
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);
|
||||
|
||||
if (noticias.isEmpty()) {
|
||||
sendMessage(chatId, "No encontré noticias sobre: *" + escapeMarkdown(query) + "*");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
List<NewsArticle> 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: <b>" + query + "</b>");
|
||||
return;
|
||||
}
|
||||
|
||||
sendMessage(chatId, mensaje.toString());
|
||||
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");
|
||||
}
|
||||
|
||||
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>");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user