feat: Implement LLM and scheduler modules

- Implement `llm.py` with OpenAI API integration for smart responses.
- Implement `scheduler.py` to send a daily summary to the bot owner using `python-telegram-bot`'s `JobQueue`.
- Integrate the scheduler into the main application.
- Add `pytz` as a new dependency.
- Update `tasks.md` to mark all tasks as complete.
This commit is contained in:
google-labs-jules[bot]
2025-12-16 00:24:06 +00:00
parent 9654ba7dd5
commit eb680edc54
5 changed files with 69 additions and 29 deletions

View File

@@ -1,15 +1,23 @@
# app/llm.py # app/llm.py
import openai
from config import OPENAI_API_KEY from config import OPENAI_API_KEY
def get_smart_response(prompt): def get_smart_response(prompt):
""" """
Generates a smart response using an LLM. Generates a smart response using the OpenAI API.
""" """
if not OPENAI_API_KEY: if not OPENAI_API_KEY:
return "OpenAI API key not configured." return "Error: OpenAI API key is not configured."
print(f"Generating smart response for: {prompt}") try:
# TODO: Implement OpenAI API integration client = openai.OpenAI(api_key=OPENAI_API_KEY)
return "This is a smart response." response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
],
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"An error occurred while communicating with OpenAI: {e}"

View File

@@ -29,6 +29,7 @@ from modules.aprobaciones import view_pending, handle_approval_action
from modules.servicios import get_service_info from modules.servicios import get_service_info
from modules.admin import get_system_status from modules.admin import get_system_status
from modules.print import print_handler from modules.print import print_handler
from app.scheduler import schedule_daily_summary
# Enable logging # Enable logging
logging.basicConfig( logging.basicConfig(
@@ -89,6 +90,9 @@ def main() -> None:
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build() application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
# Schedule daily summary
schedule_daily_summary(application)
# Conversation handler for proposing activities # Conversation handler for proposing activities
conv_handler = ConversationHandler( conv_handler = ConversationHandler(
entry_points=[CallbackQueryHandler(propose_activity_start, pattern='^propose_activity$')], entry_points=[CallbackQueryHandler(propose_activity_start, pattern='^propose_activity$')],

View File

@@ -1,27 +1,54 @@
# app/scheduler.py # app/scheduler.py
import logging
from datetime import time
from telegram.ext import ContextTypes
import pytz
import schedule from app.config import OWNER_CHAT_ID, TIMEZONE
import time from app.modules.agenda import get_agenda
from datetime import datetime
from config import TIMEZONE # Enable logging
logger = logging.getLogger(__name__)
def send_daily_summary(): async def send_daily_summary(context: ContextTypes.DEFAULT_TYPE) -> None:
""" """Sends the daily summary to the owner."""
Sends the daily summary to the owner. job = context.job
""" chat_id = job.chat_id
print(f"[{datetime.now()}] Sending daily summary...")
# TODO: Implement the logic to fetch and send the summary
def main(): logger.info(f"Running daily summary job for chat_id: {chat_id}")
"""
Main function to run the scheduler.
"""
schedule.every().day.at("07:00").do(send_daily_summary)
while True: try:
schedule.run_pending() agenda_text = get_agenda()
time.sleep(1) summary_text = f"🔔 *Resumen Diario - Buen día, Marco!*\n\n{agenda_text}"
if __name__ == "__main__": await context.bot.send_message(
main() chat_id=chat_id,
text=summary_text,
parse_mode='Markdown'
)
logger.info(f"Successfully sent daily summary to {chat_id}")
except Exception as e:
logger.error(f"Failed to send daily summary to {chat_id}: {e}")
def schedule_daily_summary(application) -> None:
"""Schedules the daily summary job."""
if not OWNER_CHAT_ID:
logger.warning("OWNER_CHAT_ID not set. Daily summary will not be scheduled.")
return
job_queue = application.job_queue
# Use the timezone from config
tz = pytz.timezone(TIMEZONE)
# Schedule the job to run every day at 7:00 AM
scheduled_time = time(hour=7, minute=0, tzinfo=tz)
job_queue.run_daily(
send_daily_summary,
time=scheduled_time,
chat_id=int(OWNER_CHAT_ID),
name="daily_summary"
)
logger.info(f"Scheduled daily summary for {OWNER_CHAT_ID} at {scheduled_time} {TIMEZONE}")

View File

@@ -5,3 +5,4 @@ google-api-python-client
google-auth-httplib2 google-auth-httplib2
google-auth-oauthlib google-auth-oauthlib
openai openai
pytz

View File

@@ -31,8 +31,8 @@ This file tracks the development tasks for the Talía project.
## Phase 4: Integrations ## Phase 4: Integrations
- [x] Implement `calendar.py` for Google Calendar integration. - [x] Implement `calendar.py` for Google Calendar integration.
- [ ] Implement `llm.py` for AI-powered responses. - [x] Implement `llm.py` for AI-powered responses.
- [ ] Implement `scheduler.py` for daily summaries. - [x] Implement `scheduler.py` for daily summaries.
## Log ## Log