Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Organized events command #25

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"python.analysis.typeCheckingMode": "standard"
"python.analysis.typeCheckingMode": "standard",
"python.analysis.extraPaths": [
"./telegram_bot/events"
]
}
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from telegram_bot.start import start
from telegram_bot.status import ping
from telegram_bot.newmembers import new_members
from telegram_bot.cerveza import events
from telegram_bot.events.events_handler import events
from telegram_bot.help import help
from telegram_bot.pina import pinacolada
from telegram_bot.fiestas import festivos
Expand Down
67 changes: 0 additions & 67 deletions telegram_bot/cerveza.py

This file was deleted.

Empty file added telegram_bot/events/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions telegram_bot/events/event_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def create_message_text(event_name: str, event_date:str, event_time: str, event_link) -> str:
event_message=(
f"Próximo evento de Sysarmy Galicia:\n\n"
f"{event_name}\n\n"
f"Data: {event_date}\n"
f"Hora: {event_time}\n\n"
f"Link: {event_link}\n\n"
)
return event_message
35 changes: 35 additions & 0 deletions telegram_bot/events/events_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from telegram import Update
from telegram.ext import ContextTypes
from datetime import datetime
from telegram_bot.utils.logger import logger
from .get_events import get_next_event
from .event_text import create_message_text
from ..utils.messaging import MessagingService



async def events(update: Update, context: ContextTypes.DEFAULT_TYPE):
thread_id = update.message.message_thread_id if update.message else None
chat_id = update.effective_chat.id if update.effective_chat else None
logger.info(
"Events command received",
user_id=update.effective_user.id if update.effective_user else None,
chat_id=update.effective_chat.id if update.effective_chat else None,
)
messaging = MessagingService(context.bot)
event = get_next_event()

if event:

start_time = str(event['start'].get('local'))
dt = datetime.fromisoformat(start_time)
event_date = dt.strftime("%d/%m")
event_time = dt.strftime("%H:%M")
event_message = create_message_text(event['name'].get('text'),event_date, event_time, event['url'])
if update.effective_chat:
await messaging.send_message(chat_id,event_message,thread_id,)
logger.info("Event message sent", chat_id=update.effective_chat.id, thread_id=thread_id)
else:
await messaging.send_message(
chat_id=chat_id, text="Ocorreu un erro, contacte ao seu Sysadmin mais próximo", thread_id=thread_id,
)
19 changes: 19 additions & 0 deletions telegram_bot/events/get_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os, requests
from telegram_bot.utils.logger import logger

EVENTBRITE_API = os.environ["EVENTBRITE_API"]
EVENTBRITE_ORGANIZATION_ID = os.environ["EVENTBRITE_ORGANIZATION_ID"]
EVENTBRITE_TOKEN = os.environ["EVENTBRITE_TOKEN"]


def get_next_event() -> dict:
response = requests.get(f'https://www.eventbriteapi.com/v3/organizations/{EVENTBRITE_ORGANIZATION_ID}/events/?time_filter=current_future', headers={'Authorization': f'Bearer {EVENTBRITE_TOKEN}'})
logger.debug(f"Request URL: {response.url}")
if response.status_code == 200:
events_result = response.json()
events = events_result.get('events', [])
event = events[0]
else:
logger.error(f"Error retrieving events: {response.status_code} - {response.text}")
event = {}
return event
Loading