ajustes scheduling
This commit is contained in:
@ -10,7 +10,9 @@ Base.metadata.create_all(bind=engine)
|
||||
|
||||
# Configurar el scheduler
|
||||
scheduler = BackgroundScheduler()
|
||||
scheduler.add_job(search_from_keywords_file, "cron", hour=1, minute=0) #Ejecutar a las 01:00
|
||||
scheduler.add_job(search_from_keywords_file, "cron", hour=11, minute=0) #Ejecutar a las 01:00
|
||||
scheduler.add_job(search_from_keywords_file, "cron", hour=18, minute=0) #Ejecutar a las 01:00
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
@ -23,3 +25,4 @@ app = FastAPI(lifespan=lifespan)
|
||||
|
||||
# Incluir rutas
|
||||
app.include_router(router)
|
||||
|
||||
|
@ -94,7 +94,7 @@ def search_news(query):
|
||||
articles = soup.find_all("item")
|
||||
news_list = []
|
||||
|
||||
for article in articles[:20]: # Limitar a los primeros 20 artículos
|
||||
for article in articles[:12]: # Limitar a los primeros 12 artículos
|
||||
try:
|
||||
title = article.title.get_text(strip=True)
|
||||
content = article.description.get_text(strip=True) if article.description else "Sin descripción"
|
||||
@ -184,4 +184,3 @@ def search_from_keywords_file():
|
||||
logging.info(f"Error al leer el archivo 'keywords.txt': {e}")
|
||||
|
||||
# Ejecutar la búsqueda desde el archivo
|
||||
|
||||
|
@ -3,11 +3,13 @@ package es.imunnic.inversionitasBot;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.telegram.telegrambots.meta.TelegramBotsApi;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class InversionitasBotApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -3,6 +3,7 @@ package es.imunnic.inversionitasBot;
|
||||
import es.imunnic.inversionitasBot.services.ApiService;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
@ -11,6 +12,7 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -48,16 +50,25 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
}
|
||||
|
||||
private void sendMessage(String chatId, String text) {
|
||||
try {
|
||||
SendMessage message = new SendMessage();
|
||||
message.setChatId(chatId);
|
||||
message.setText(text);
|
||||
execute(message);
|
||||
} catch (TelegramApiException e) {
|
||||
e.printStackTrace();
|
||||
// Primero dividimos el mensaje si es necesario
|
||||
List<String> partes = dividirMensaje(text);
|
||||
|
||||
// Enviar cada parte por separado
|
||||
for (String parte : partes) {
|
||||
SendMessage sendMessage = new SendMessage();
|
||||
sendMessage.setChatId(chatId); // Asumiendo que ya tienes un chatId
|
||||
sendMessage.setText(parte);
|
||||
sendMessage.setParseMode("MarkdownV2"); // Usamos MarkdownV2, pero también puedes usar HTML si lo prefieres
|
||||
|
||||
try {
|
||||
execute(sendMessage); // Usando el método execute() de Telegram para enviar el mensaje
|
||||
} catch (TelegramApiException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 13,20 * * ?", zone = "Europe/Paris")
|
||||
private void construirResumenNoticias(String chatid) {
|
||||
LocalDate today = LocalDate.now();
|
||||
String fechaInicio = today.minusDays(1).format(DateTimeFormatter.ISO_DATE);
|
||||
@ -99,4 +110,25 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
// Enviar el pie al final
|
||||
sendMessage(chatid, pie);
|
||||
}
|
||||
|
||||
// Función para dividir el mensaje si excede el límite de caracteres de Telegram
|
||||
private List<String> dividirMensaje(String mensaje) {
|
||||
int maxLength = 4096; // Límite de caracteres para un mensaje en Telegram
|
||||
List<String> partes = new ArrayList<>();
|
||||
|
||||
// Divide el mensaje en partes de tamaño adecuado
|
||||
while (mensaje.length() > maxLength) {
|
||||
partes.add(mensaje.substring(0, maxLength));
|
||||
mensaje = mensaje.substring(maxLength);
|
||||
}
|
||||
|
||||
// Agrega la última parte si queda algún mensaje restante
|
||||
if (!mensaje.isEmpty()) {
|
||||
partes.add(mensaje);
|
||||
}
|
||||
|
||||
return partes;
|
||||
}
|
||||
}
|
||||
//TODO ajustar los mensajes para que no se pasen
|
||||
//TODO ajustar la presentación con la negrita
|
||||
|
Reference in New Issue
Block a user