ajuste del bot
This commit is contained in:
@ -12,6 +12,7 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@ -63,18 +64,20 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
String fechaInicio = today.format(DateTimeFormatter.ISO_DATE);
|
||||
String fechaFin = today.format(DateTimeFormatter.ISO_DATE);
|
||||
|
||||
// Palabras clave de ejemplo
|
||||
String[] keywords = {"política", "economía", "tecnología"};
|
||||
// Obtener las palabras clave de la API
|
||||
List<String> keywords = ApiService.getAllKeywords();
|
||||
|
||||
StringBuilder mensaje = new StringBuilder("📢 *Resumen de noticias del día:*\n\n");
|
||||
for (String keyword : keywords) {
|
||||
String jsonResponse = ApiService.getTitlesByKeyword(keyword, fechaInicio, fechaFin);
|
||||
JSONObject jsonObject = new JSONObject(jsonResponse);
|
||||
// Obtener las noticias para cada keyword (ahora se obtiene una lista de títulos)
|
||||
List<String> titles = ApiService.getTitlesByKeyword(keyword, fechaInicio, fechaFin);
|
||||
|
||||
if (jsonObject.has("titles")) {
|
||||
String titles = jsonObject.getString("titles");
|
||||
if (!titles.isEmpty()) {
|
||||
mensaje.append("🔹 *").append(keyword).append("*:\n");
|
||||
mensaje.append(" - ").append(titles.replace(",", "\n - ")).append("\n\n");
|
||||
for (String title : titles) {
|
||||
mensaje.append(" - ").append(title).append("\n");
|
||||
}
|
||||
mensaje.append("\n");
|
||||
} else {
|
||||
mensaje.append("🔹 *").append(keyword).append("*: No hay noticias disponibles.\n\n");
|
||||
}
|
||||
|
@ -8,6 +8,9 @@ import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import es.imunnic.inversionitasBot.TelegramBot;
|
||||
@ -67,8 +70,30 @@ public class ApiService {
|
||||
}
|
||||
|
||||
// 5️⃣ Obtener todas las keywords
|
||||
public static String getAllKeywords() {
|
||||
return sendGetRequest("/keywords/");
|
||||
public static List<String> getAllKeywords() {
|
||||
String jsonResponse = sendGetRequest("/keywords/");
|
||||
|
||||
// Crear una lista para almacenar las keywords
|
||||
List<String> keywordsList = new ArrayList<>();
|
||||
|
||||
try {
|
||||
// Convertir la respuesta JSON en un objeto JSONObject
|
||||
JSONObject jsonObject = new JSONObject(jsonResponse);
|
||||
|
||||
// Obtener el array de keywords
|
||||
JSONArray keywordsArray = jsonObject.getJSONArray("keywords");
|
||||
|
||||
// Recorrer el array y agregar cada keyword a la lista
|
||||
for (int i = 0; i < keywordsArray.length(); i++) {
|
||||
keywordsList.add(keywordsArray.getString(i));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Manejo de excepciones, puedes hacer más aquí si es necesario
|
||||
logger.error("Error al procesar los datos JSON", e);
|
||||
}
|
||||
|
||||
// Retornar la lista de keywords
|
||||
return keywordsList;
|
||||
}
|
||||
|
||||
// 6️⃣ Contar noticias por fuente con una keyword y rango de fechas
|
||||
@ -78,9 +103,33 @@ public class ApiService {
|
||||
}
|
||||
|
||||
// 7️⃣ Obtener títulos de noticias por keyword en un rango de fechas
|
||||
public static String getTitlesByKeyword(String keyword, String fechaInicio, String fechaFin) {
|
||||
public static List<String> getTitlesByKeyword(String keyword, String fechaInicio, String fechaFin) {
|
||||
String params = "?keyword=" + encodeParam(keyword) + "&fecha_inicio=" + fechaInicio + "&fecha_fin=" + fechaFin;
|
||||
return sendGetRequest("/titles/by-keyword/date-range" + params);
|
||||
String jsonResponse = sendGetRequest("/titles/by-keyword/date-range" + params);
|
||||
|
||||
List<String> titlesList = new ArrayList<>();
|
||||
|
||||
try {
|
||||
// Convertir la respuesta JSON en un objeto JSONObject
|
||||
JSONObject jsonObject = new JSONObject(jsonResponse);
|
||||
|
||||
// Verificar si el JSON contiene el array de títulos
|
||||
if (jsonObject.has("titles")) {
|
||||
// Obtener el array de títulos
|
||||
JSONArray titlesArray = jsonObject.getJSONArray("titles");
|
||||
|
||||
// Recorrer el array y agregar cada título a la lista
|
||||
for (int i = 0; i < titlesArray.length(); i++) {
|
||||
titlesList.add(titlesArray.getString(i));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Manejo de excepciones, puedes hacer más aquí si es necesario
|
||||
logger.error("Error al procesar los títulos de noticias", e);
|
||||
}
|
||||
|
||||
// Retornar la lista de títulos
|
||||
return titlesList;
|
||||
}
|
||||
|
||||
// 8️⃣ Obtener todos los títulos de noticias en un rango de fechas
|
||||
|
Reference in New Issue
Block a user