|
| 1 | +import logging |
| 2 | +import datetime |
| 3 | +from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup |
| 4 | +from telegram.ext import ContextTypes |
| 5 | +from telegram.constants import ParseMode |
| 6 | +from tg_ids import CODEPERS_CHATID, ROZEN_CHATID, DGARRO_CHATID |
| 7 | +from models import Noticia |
| 8 | +from handlers.db import get_session |
| 9 | + |
| 10 | +logger = logging.getLogger("DCUBABOT") |
| 11 | +admin_ids = [ROZEN_CHATID, DGARRO_CHATID] |
| 12 | + |
| 13 | +async def checodepers(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 14 | + if not context.args: |
| 15 | + ejemplo = """ Ejemplo de uso: |
| 16 | + /checodepers Hola, tengo un mensaje mucho muy importante que me gustaria que respondan |
| 17 | +""" |
| 18 | + await update.message.reply_text(ejemplo) |
| 19 | + return |
| 20 | + user = update.message.from_user |
| 21 | + try: |
| 22 | + if not user.username: |
| 23 | + raise Exception("not userneim") |
| 24 | + message = " ".join(context.args) |
| 25 | + await context.bot.send_message( |
| 26 | + chat_id=CODEPERS_CHATID, text=f"{user.first_name}(@{user.username}) : {message}") |
| 27 | + except Exception: |
| 28 | + try: |
| 29 | + await context.bot.forward_message( |
| 30 | + CODEPERS_CHATID, update.message.chat_id, update.message.message_id) |
| 31 | + logger.info(f"Malio sal {str(user)}") |
| 32 | + except Exception as e: |
| 33 | + await update.message.reply_text( |
| 34 | + "La verdad me re rompí, avisale a roz asi ve que onda") |
| 35 | + logger.error(e) |
| 36 | + return |
| 37 | + await update.message.reply_text("OK, se lo mando a les codepers.") |
| 38 | + |
| 39 | +async def checodeppers(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 40 | + await checodepers(update, context) |
| 41 | + |
| 42 | +async def sugerirNoticia(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 43 | + user = update.message.from_user |
| 44 | + name = user.first_name |
| 45 | + texto = " ".join(context.args) |
| 46 | + if not texto: |
| 47 | + await update.message.reply_text( |
| 48 | + text="Loc@, pusisiste algo mal, la idea es q pongas:\n " |
| 49 | + "/sugerirNoticia <texto>") |
| 50 | + return |
| 51 | + with get_session() as session: |
| 52 | + noticia = Noticia(text=texto) |
| 53 | + session.add(noticia) |
| 54 | + session.flush() |
| 55 | + noticia_id = noticia.id |
| 56 | + keyboard = [ |
| 57 | + [ |
| 58 | + InlineKeyboardButton("Aceptar", callback_data=f"Noticia|{noticia_id}|1"), |
| 59 | + InlineKeyboardButton("Rechazar", callback_data=f"Noticia|{noticia_id}|0") |
| 60 | + ] |
| 61 | + ] |
| 62 | + reply_markup = InlineKeyboardMarkup(keyboard) |
| 63 | + await context.bot.send_message(chat_id=ROZEN_CHATID, text=f"Noticia-{name}: {texto}", |
| 64 | + reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN) |
| 65 | + await update.message.reply_text(text="Ok, se lo pregunto a Rozen") |
| 66 | + |
| 67 | +async def get_logs(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 68 | + user_id = update.effective_user.id |
| 69 | + if user_id not in admin_ids: |
| 70 | + return |
| 71 | + |
| 72 | + try: |
| 73 | + from google.cloud import logging as gcp_logging |
| 74 | + import json |
| 75 | + import io |
| 76 | + |
| 77 | + await update.message.reply_text("Buscando errores en Google Cloud Logging...") |
| 78 | + client = gcp_logging.Client() |
| 79 | + |
| 80 | + filter_str = 'resource.type="cloud_run_revision" AND severity>=ERROR AND resource.labels.service_name="dcubabot"' |
| 81 | + entries = client.list_entries(filter_=filter_str, order_by=gcp_logging.DESCENDING, max_results=50) |
| 82 | + |
| 83 | + log_msgs = [] |
| 84 | + for entry in entries: |
| 85 | + log_data = { |
| 86 | + "timestamp": entry.timestamp.isoformat(), |
| 87 | + "severity": entry.severity, |
| 88 | + } |
| 89 | + |
| 90 | + payload = entry.payload |
| 91 | + if isinstance(payload, dict): |
| 92 | + log_data["json_payload"] = payload |
| 93 | + elif payload is not None: |
| 94 | + log_data["text_payload"] = str(payload) |
| 95 | + else: |
| 96 | + log_data["resource"] = str(entry.resource) |
| 97 | + |
| 98 | + log_msgs.append(log_data) |
| 99 | + |
| 100 | + if not log_msgs: |
| 101 | + await update.message.reply_text("✅ No se encontraron errores recientes en GCP.") |
| 102 | + return |
| 103 | + |
| 104 | + logs_json_str = json.dumps(log_msgs, indent=2, ensure_ascii=False) |
| 105 | + file_obj = io.BytesIO(logs_json_str.encode('utf-8')) |
| 106 | + file_obj.name = f"gcp_error_logs_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.json" |
| 107 | + |
| 108 | + await context.bot.send_document( |
| 109 | + chat_id=update.effective_chat.id, |
| 110 | + document=file_obj, |
| 111 | + caption="Acá tenés el archivo con los últimos errores registrados en GCP 🕵️♂️" |
| 112 | + ) |
| 113 | + except Exception as e: |
| 114 | + await update.message.reply_text(f"Error al leer logs (¿falta permiso roles/logging.viewer en la Service Account?): {e}") |
0 commit comments