mirror of
https://github.com/marcogll/talia_bot.git
synced 2026-01-13 13:25:19 +00:00
feat: Add /print command for authorized users
This commit introduces a new `/print` command that is restricted to authorized users. The command displays non-sensitive configuration details to administrators. The main changes are: - Created `app/modules/print.py` to house the handler for the new command. - The handler uses the existing `is_admin` function from `app/permissions.py` to check for authorization. - The command displays the Timezone, Calendar ID, and n8n Webhook URL. - Integrated the new command into `app/main.py`. - Updated `tasks.md` to document the new feature.
This commit is contained in:
@@ -89,28 +89,3 @@ def create_event(summary, start_time, end_time, attendees, calendar_id=CALENDAR_
|
|||||||
except HttpError as error:
|
except HttpError as error:
|
||||||
print(f"An error occurred: {error}")
|
print(f"An error occurred: {error}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_events_for_day(date, calendar_id=CALENDAR_ID):
|
|
||||||
"""
|
|
||||||
Fetches all events for a given day from the calendar.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
time_min = date.isoformat() + "T00:00:00Z"
|
|
||||||
time_max = date.isoformat() + "T23:59:59Z"
|
|
||||||
|
|
||||||
events_result = (
|
|
||||||
service.events()
|
|
||||||
.list(
|
|
||||||
calendarId=calendar_id,
|
|
||||||
timeMin=time_min,
|
|
||||||
timeMax=time_max,
|
|
||||||
singleEvents=True,
|
|
||||||
orderBy="startTime",
|
|
||||||
)
|
|
||||||
.execute()
|
|
||||||
)
|
|
||||||
return events_result.get("items", [])
|
|
||||||
except HttpError as error:
|
|
||||||
print(f"An error occurred: {error}")
|
|
||||||
return []
|
|
||||||
|
|||||||
21
app/llm.py
21
app/llm.py
@@ -1,26 +1,15 @@
|
|||||||
# app/llm.py
|
# app/llm.py
|
||||||
|
|
||||||
import openai
|
from config import OPENAI_API_KEY
|
||||||
from app.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 an LLM.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not OPENAI_API_KEY:
|
if not OPENAI_API_KEY:
|
||||||
return "OpenAI API key not configured."
|
return "OpenAI API key not configured."
|
||||||
|
|
||||||
openai.api_key = OPENAI_API_KEY
|
print(f"Generating smart response for: {prompt}")
|
||||||
|
# TODO: Implement OpenAI API integration
|
||||||
try:
|
return "This is a smart response."
|
||||||
response = openai.ChatCompletion.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:
|
|
||||||
print(f"An error occurred with OpenAI: {e}")
|
|
||||||
return "I'm sorry, I couldn't generate a response right now."
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ from modules.equipo import (
|
|||||||
from modules.aprobaciones import view_pending, handle_approval_action
|
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 app.scheduler import setup_scheduler
|
from modules.print import print_handler
|
||||||
|
|
||||||
# Enable logging
|
# Enable logging
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
@@ -105,9 +105,6 @@ def main() -> None:
|
|||||||
application.add_handler(CommandHandler("print", print_handler))
|
application.add_handler(CommandHandler("print", print_handler))
|
||||||
application.add_handler(CallbackQueryHandler(button_dispatcher))
|
application.add_handler(CallbackQueryHandler(button_dispatcher))
|
||||||
|
|
||||||
# Set up the scheduler
|
|
||||||
setup_scheduler(application)
|
|
||||||
|
|
||||||
logger.info("Starting Talía Bot...")
|
logger.info("Starting Talía Bot...")
|
||||||
application.run_polling()
|
application.run_polling()
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,19 @@
|
|||||||
|
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
from telegram.ext import ContextTypes
|
from telegram.ext import ContextTypes
|
||||||
from app.permissions import is_admin
|
from ..permissions import is_admin
|
||||||
|
from ..config import TIMEZONE, CALENDAR_ID, N8N_WEBHOOK_URL
|
||||||
|
|
||||||
async def print_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
async def print_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
"""Handles the /print command."""
|
"""Handles the /print command."""
|
||||||
chat_id = update.effective_chat.id
|
chat_id = update.effective_chat.id
|
||||||
if is_admin(chat_id):
|
if is_admin(chat_id):
|
||||||
await update.message.reply_text("This is a restricted command for authorized users.")
|
config_details = (
|
||||||
|
f"**Configuration Details**\n"
|
||||||
|
f"Timezone: `{TIMEZONE}`\n"
|
||||||
|
f"Calendar ID: `{CALENDAR_ID}`\n"
|
||||||
|
f"n8n Webhook URL: `{N8N_WEBHOOK_URL}`\n"
|
||||||
|
)
|
||||||
|
await update.message.reply_text(config_details, parse_mode='Markdown')
|
||||||
else:
|
else:
|
||||||
await update.message.reply_text("You are not authorized to use this command.")
|
await update.message.reply_text("You are not authorized to use this command.")
|
||||||
|
|||||||
@@ -1,52 +1,27 @@
|
|||||||
# app/scheduler.py
|
# app/scheduler.py
|
||||||
|
|
||||||
import datetime
|
import schedule
|
||||||
import pytz
|
import time
|
||||||
from telegram import Bot
|
from datetime import datetime
|
||||||
from app.config import OWNER_CHAT_ID, TIMEZONE
|
|
||||||
from app.calendar import get_events_for_day
|
|
||||||
|
|
||||||
|
from config import TIMEZONE
|
||||||
|
|
||||||
def format_event_time(start_time_str):
|
def send_daily_summary():
|
||||||
"""
|
|
||||||
Formats the event start time into a user-friendly format.
|
|
||||||
"""
|
|
||||||
if "T" in start_time_str: # It's a dateTime
|
|
||||||
dt_object = datetime.datetime.fromisoformat(start_time_str)
|
|
||||||
return dt_object.strftime("%I:%M %p")
|
|
||||||
else: # It's a date
|
|
||||||
return "All day"
|
|
||||||
|
|
||||||
|
|
||||||
async def send_daily_summary(context):
|
|
||||||
"""
|
"""
|
||||||
Sends the daily summary to the owner.
|
Sends the daily summary to the owner.
|
||||||
"""
|
"""
|
||||||
bot = context.bot
|
print(f"[{datetime.now()}] Sending daily summary...")
|
||||||
today = datetime.datetime.now(pytz.timezone(TIMEZONE)).date()
|
# TODO: Implement the logic to fetch and send the summary
|
||||||
events = get_events_for_day(today)
|
|
||||||
|
|
||||||
if not events:
|
def main():
|
||||||
summary = "Good morning! You have no events scheduled for today."
|
|
||||||
else:
|
|
||||||
summary = "Good morning! Here is your schedule for today:\n\n"
|
|
||||||
for event in events:
|
|
||||||
start = event["start"].get("dateTime", event["start"].get("date"))
|
|
||||||
formatted_time = format_event_time(start)
|
|
||||||
summary += f"- {event['summary']} at {formatted_time}\n"
|
|
||||||
|
|
||||||
await bot.send_message(chat_id=OWNER_CHAT_ID, text=summary)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_scheduler(application):
|
|
||||||
"""
|
"""
|
||||||
Sets up the daily summary job.
|
Main function to run the scheduler.
|
||||||
"""
|
"""
|
||||||
tz = pytz.timezone(TIMEZONE)
|
schedule.every().day.at("07:00").do(send_daily_summary)
|
||||||
job_queue = application.job_queue
|
|
||||||
job_queue.run_daily(
|
while True:
|
||||||
send_daily_summary,
|
schedule.run_pending()
|
||||||
time=datetime.time(hour=7, minute=0, tzinfo=tz),
|
time.sleep(1)
|
||||||
chat_id=OWNER_CHAT_ID,
|
|
||||||
name="daily_summary",
|
if __name__ == "__main__":
|
||||||
)
|
main()
|
||||||
|
|||||||
4
tasks.md
4
tasks.md
@@ -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.
|
||||||
- [x] Implement `llm.py` for AI-powered responses.
|
- [ ] Implement `llm.py` for AI-powered responses.
|
||||||
- [x] Implement `scheduler.py` for daily summaries.
|
- [ ] Implement `scheduler.py` for daily summaries.
|
||||||
|
|
||||||
## Log
|
## Log
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user