|
7 | 7 | import datetime |
8 | 8 | import random |
9 | 9 | from contextlib import contextmanager |
| 10 | +from time import sleep |
10 | 11 |
|
11 | 12 | # Non STL imports |
12 | 13 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update |
@@ -66,7 +67,10 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE): |
66 | 67 | with get_session() as session: |
67 | 68 | commands = session.query(Command).filter_by(enabled=True).order_by(Command.name).all() |
68 | 69 | for command in commands: |
69 | | - message_text += f"/{command.name} - {command.description}\n" |
| 70 | + if command.description: |
| 71 | + message_text += f"/{command.name} - {command.description}\n" |
| 72 | + else: |
| 73 | + message_text += f"/{command.name}\n" |
70 | 74 | await update.message.reply_text(message_text) |
71 | 75 |
|
72 | 76 |
|
@@ -149,29 +153,20 @@ async def suggest_listable(update: Update, context: ContextTypes.DEFAULT_TYPE, l |
149 | 153 |
|
150 | 154 |
|
151 | 155 | async def sugerirgrupo(update: Update, context: ContextTypes.DEFAULT_TYPE): |
152 | | - await suggest_listable(update, context, Obligatoria) |
| 156 | + await update.message.reply_text("Este comando esta deprecado, para agregar un grupo por favor agregá el bot al grupo y escribí /agregargrupo") |
153 | 157 |
|
154 | 158 |
|
155 | 159 | async def sugeriroptativa(update: Update, context: ContextTypes.DEFAULT_TYPE): |
156 | | - await suggest_listable(update, context, Optativa) |
| 160 | + await update.message.reply_text("Este comando esta deprecado, para agregar un grupo por favor agregá el bot al grupo y escribí /agregaroptativa") |
157 | 161 |
|
158 | 162 |
|
159 | 163 | async def sugerireci(update: Update, context: ContextTypes.DEFAULT_TYPE): |
160 | | - await suggest_listable(update, context, ECI) |
| 164 | + await update.message.reply_text("Este comando esta deprecado, para agregar un grupo por favor agregá el bot al grupo y escribí /agregareci") |
161 | 165 |
|
162 | 166 |
|
163 | 167 | async def sugerirotro(update: Update, context: ContextTypes.DEFAULT_TYPE): |
164 | | - await suggest_listable(update, context, Otro) |
| 168 | + await update.message.reply_text("Este comando esta deprecado, para agregar un grupo por favor agregá el bot al grupo y escribí /agregarotro") |
165 | 169 |
|
166 | | -async def agregargrupo(update: Update, context: ContextTypes.DEFAULT_TYPE): |
167 | | - await update.message.reply_text( |
168 | | - "Para sugerir un grupo, usá uno de los siguientes comandos, dependiendo de la categoría del grupo:\n" |
169 | | - "/sugerirgrupo - Materia obligatoria\n" |
170 | | - "/sugeriroptativa - Materia optativa\n" |
171 | | - "/sugerireci - ECI\n" |
172 | | - "/sugerirotro - Otra categoría\n\n" |
173 | | - "El formato es: /comando <nombre>|<link>" |
174 | | - ) |
175 | 170 |
|
176 | 171 | async def campusvivo(update: Update, context: ContextTypes.DEFAULT_TYPE): |
177 | 172 | msg = await update.message.reply_text("Bancá que me fijo...") |
@@ -320,6 +315,124 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE): |
320 | 315 | await query.edit_message_text(text=message.text + action_text) |
321 | 316 |
|
322 | 317 |
|
| 318 | +async def agregar(update: Update, context: ContextTypes.DEFAULT_TYPE, grouptype, groupString): |
| 319 | + try: |
| 320 | + url = await context.bot.export_chat_invite_link( |
| 321 | + chat_id=update.message.chat.id) |
| 322 | + name = update.message.chat.title |
| 323 | + chat_id = str(update.message.chat.id) |
| 324 | + except: # TODO: filter excepts |
| 325 | + await update.message.reply_text( |
| 326 | + text=f"Mirá, no puedo hacerle un link a este grupo, proba haciendome admin") |
| 327 | + return |
| 328 | + with get_session() as session: |
| 329 | + group = session.query(grouptype).filter_by(chat_id=chat_id).first() |
| 330 | + if group: |
| 331 | + group.url = url |
| 332 | + group.name = name |
| 333 | + await update.message.reply_text( |
| 334 | + text=f"Datos del grupo actualizados") |
| 335 | + return |
| 336 | + group = grouptype(name=name, url=url, chat_id=chat_id) |
| 337 | + session.add(group) |
| 338 | + session.flush() |
| 339 | + group_id = group.id |
| 340 | + keyboard = [ |
| 341 | + [ |
| 342 | + InlineKeyboardButton( |
| 343 | + text="Aceptar", callback_data=f"Listable|{group_id}|1"), |
| 344 | + InlineKeyboardButton( |
| 345 | + text="Rechazar", callback_data=f"Listable|{group_id}|0") |
| 346 | + ] |
| 347 | + ] |
| 348 | + reply_markup = InlineKeyboardMarkup(keyboard) |
| 349 | + await context.bot.send_message(chat_id=ROZEN_CHATID, |
| 350 | + text=f"{groupString}: {name}\n{url}", |
| 351 | + reply_markup=reply_markup) |
| 352 | + await update.message.reply_text("OK, se lo mando a Rozen.") |
| 353 | + |
| 354 | + |
| 355 | +async def agregargrupo(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 356 | + await agregar(update, context, Grupo, "grupo") |
| 357 | + |
| 358 | + |
| 359 | +async def agregaroptativa(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 360 | + await agregar(update, context, GrupoOptativa, "optativa") |
| 361 | + |
| 362 | + |
| 363 | +async def agregarotros(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 364 | + await agregar(update, context, GrupoOtros, "otro") |
| 365 | + |
| 366 | + |
| 367 | +async def agregareci(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 368 | + await agregar(update, context, ECI, "eci") |
| 369 | + |
| 370 | + |
| 371 | +async def sugerirNoticia(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 372 | + user = update.message.from_user |
| 373 | + name = user.first_name |
| 374 | + texto = " ".join(context.args) |
| 375 | + if not texto: |
| 376 | + await update.message.reply_text( |
| 377 | + text="Loc@, pusisiste algo mal, la idea es q pongas:\n " |
| 378 | + "/sugerirNoticia <texto>") |
| 379 | + return |
| 380 | + with get_session() as session: |
| 381 | + noticia = Noticia(text=texto) |
| 382 | + session.add(noticia) |
| 383 | + session.flush() |
| 384 | + noticia_id = noticia.id |
| 385 | + keyboard = [ |
| 386 | + [ |
| 387 | + InlineKeyboardButton("Aceptar", callback_data=f"Noticia|{noticia_id}|1"), |
| 388 | + InlineKeyboardButton("Rechazar", callback_data=f"Noticia|{noticia_id}|0") |
| 389 | + ] |
| 390 | + ] |
| 391 | + reply_markup = InlineKeyboardMarkup(keyboard) |
| 392 | + await context.bot.send_message(chat_id=ROZEN_CHATID, text=f"Noticia-{name}: {texto}", |
| 393 | + reply_markup=reply_markup, parse_mode=ParseMode.MARKDOWN) |
| 394 | + await update.message.reply_text(text="Ok, se lo pregunto a Rozen") |
| 395 | + |
| 396 | + |
| 397 | +async def update_group_url(context: ContextTypes.DEFAULT_TYPE, chat_id: str) -> (str, str, bool): |
| 398 | + try: |
| 399 | + url = await context.bot.export_chat_invite_link(chat_id=chat_id) |
| 400 | + return chat_id, url, True # too GO-like huh? |
| 401 | + except Exception: |
| 402 | + logger.error(f"Could not create invite link for {chat_id}", exc_info=True) |
| 403 | + return None, None, False # too GO-like huh? |
| 404 | + |
| 405 | + |
| 406 | +async def _update_groups(context: ContextTypes.DEFAULT_TYPE): |
| 407 | + logger.info("Starting update_groups job") |
| 408 | + with get_session() as session: |
| 409 | + chats = list(session.query(Listable).filter_by(validated=True).all()) |
| 410 | + logger.info(f"Found {len(chats)} groups to update") |
| 411 | + |
| 412 | + for chat in chats: |
| 413 | + sleep(1) |
| 414 | + chat_id, url, validated = await update_group_url(context, chat.chat_id) |
| 415 | + if not validated: |
| 416 | + logger.warning(f"Failed to update URL for group '{chat.name}'. De-validating.") |
| 417 | + with get_session() as session: |
| 418 | + c = session.query(Listable).filter_by(id=chat.id).first() |
| 419 | + c.validated = False |
| 420 | + await context.bot.send_message(chat_id=DC_GROUP_CHATID, text=f"El grupo {chat.name} murió 💀") |
| 421 | + else: |
| 422 | + logger.info(f"Updating URL for group '{chat.name}'") |
| 423 | + with get_session() as session: |
| 424 | + c = session.query(Listable).filter_by(id=chat.id).first() |
| 425 | + c.url = url |
| 426 | + logger.info("Finished update_groups job") |
| 427 | + |
| 428 | + |
| 429 | +async def actualizar_grupos(update: Update, context: ContextTypes.DEFAULT_TYPE): |
| 430 | + logger.info(f"Manual update of groups triggered by {update.effective_user.id}") |
| 431 | + await update.message.reply_text("Actualizando grupos...") |
| 432 | + await _update_groups(context) |
| 433 | + await update.message.reply_text("¡Grupos actualizados!") |
| 434 | + |
| 435 | + |
323 | 436 | COMMANDS = { |
324 | 437 | 'start': { |
325 | 438 | 'handler': start, |
@@ -353,25 +466,26 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE): |
353 | 466 | 'handler': cubawiki, |
354 | 467 | 'description': 'Devuelve el link a la Cubawiki de la materia (si estás en el grupo de la materia).' |
355 | 468 | }, |
356 | | - 'sugerirgrupo': { |
357 | | - 'handler': sugerirgrupo, |
358 | | - 'description': 'Sugiere un grupo de una materia obligatoria.' |
| 469 | + |
| 470 | + 'agregargrupo': { |
| 471 | + 'handler': agregargrupo, |
| 472 | + 'description': 'Agrega el grupo actual a la lista de grupos de materias obligatorias.' |
359 | 473 | }, |
360 | | - 'sugeriroptativa': { |
361 | | - 'handler': sugeriroptativa, |
362 | | - 'description': 'Sugiere un grupo de una materia optativa.' |
| 474 | + 'agregaroptativa': { |
| 475 | + 'handler': agregaroptativa, |
| 476 | + 'description': 'Agrega el grupo actual a la lista de grupos de materias optativas.' |
363 | 477 | }, |
364 | | - 'sugerireci': { |
365 | | - 'handler': sugerireci, |
366 | | - 'description': 'Sugiere un grupo de una ECI.' |
| 478 | + 'agregareci': { |
| 479 | + 'handler': agregareci, |
| 480 | + 'description': 'Agrega el grupo actual a la lista de grupos de ECI.' |
367 | 481 | }, |
368 | | - 'sugerirotro': { |
369 | | - 'handler': sugerirotro, |
370 | | - 'description': 'Sugiere un grupo de otra categoría.' |
| 482 | + 'agregarotros': { |
| 483 | + 'handler': agregarotros, |
| 484 | + 'description': 'Agrega el grupo actual a la lista de otros grupos.' |
371 | 485 | }, |
372 | | - 'agregargrupo': { |
373 | | - 'handler': agregargrupo, |
374 | | - 'description': 'Muestra ayuda para sugerir un grupo.' |
| 486 | + 'sugerirnoticia': { |
| 487 | + 'handler': sugerirNoticia, |
| 488 | + 'description': 'Sugiere una noticia para el canal de noticias.' |
375 | 489 | }, |
376 | 490 | 'campusvivo': { |
377 | 491 | 'handler': campusvivo, |
@@ -413,4 +527,23 @@ async def button(update: Update, context: ContextTypes.DEFAULT_TYPE): |
413 | 527 | 'handler': colaborar, |
414 | 528 | 'description': 'Muestra el link al repositorio de Github del bot.' |
415 | 529 | }, |
| 530 | + 'actualizar_grupos': { |
| 531 | + 'handler': actualizar_grupos, |
| 532 | + 'description': 'Actualiza los links de todos los grupos.' |
| 533 | + }, |
| 534 | + #deprecated |
| 535 | + 'sugerirgrupo': { |
| 536 | + 'handler': sugerirgrupo, |
| 537 | + }, |
| 538 | + 'sugeriroptativa': { |
| 539 | + 'handler': sugeriroptativa, |
| 540 | + }, |
| 541 | + 'sugerireci': { |
| 542 | + 'handler': sugerireci, |
| 543 | + }, |
| 544 | + 'sugerirotro': { |
| 545 | + 'handler': sugerirotro, |
| 546 | + }, |
| 547 | + #hidden |
| 548 | + |
416 | 549 | } |
0 commit comments