From d3ab0e2844add0e283429bca9a2f36e1c9e1abf0 Mon Sep 17 00:00:00 2001 From: "Adrien.Malingrey" Date: Fri, 6 Feb 2026 15:21:00 +0100 Subject: [PATCH] message d'erreur personnalisable --- README.md | 3 ++- callbacks.py | 34 ++++++++++++++++++---------------- tchap-beta.py | 5 +++-- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e675164..55570b3 100755 --- a/README.md +++ b/README.md @@ -42,4 +42,5 @@ sudo chown chatbeta -R /opt/tchap-beta sudo systemctl enable --now tchap-beta.service ```` -(L'emplacement et le nom d'utilisateur sont libres tant que c'est cohérent avec le service.) \ No newline at end of file +(L'emplacement et le nom d'utilisateur sont libres tant que c'est cohérent avec le service.) + diff --git a/callbacks.py b/callbacks.py index fc2e9fd..3fab876 100755 --- a/callbacks.py +++ b/callbacks.py @@ -6,23 +6,25 @@ from matrix_bot.config import logger from matrix_bot.client import MatrixClient -def properly_fail(function): - """use this decorator so that your async callback never crash, log the error and return a message to the room""" +def properly_fail(error_message="failed to answer", style="m.notice"): + def custom_properly_fail(function): + """use this decorator so that your async callback never crash, log the error and return a message to the room""" - @wraps(function) - def decorated(room: MatrixRoom, message: Event, matrix_client: MatrixClient): - function_instance = function(room, message, matrix_client) + @wraps(function) + def decorated(room: MatrixRoom, message: Event, matrix_client: MatrixClient): + function_instance = function(room, message, matrix_client) - async def inner(): - try: - return await function_instance - except Exception as unexpected_exception: # noqa - await matrix_client.send_text_message(room.room_id, "Oups, j'ai buggué 😿", "m.notice") - logger.warning(f"command failed with exception : {unexpected_exception}") - exit() - finally: - await matrix_client.room_typing(room.room_id, typing_state=False) + async def inner(): + try: + return await function_instance + except Exception as unexpected_exception: # noqa + await matrix_client.send_text_message(room.room_id, error_message, style) + logger.warning(f"command failed with exception : {unexpected_exception}") + exit() + finally: + await matrix_client.room_typing(room.room_id, typing_state=False) - return inner() + return inner() - return decorated + return decorated + return custom_properly_fail \ No newline at end of file diff --git a/tchap-beta.py b/tchap-beta.py index 96a21ea..2e636f6 100755 --- a/tchap-beta.py +++ b/tchap-beta.py @@ -16,7 +16,7 @@ from markov import parle # d'envoyer le message que le bot n'est plus en train d'écrire # la fonction va être appelée dans tous les cas, le décorateur @ignore_when_not_concerned # permet de laisser event_parser gérer le cas où la commande n'est pas concernée -@properly_fail +@properly_fail("Oups, j'ai buggué 😿") @ignore_when_not_concerned async def repond(room: MatrixRoom, message: Event, matrix_client: MatrixClient): # on initialise un event_parser pour décider à quel message cette commande va répondre @@ -27,12 +27,13 @@ async def repond(room: MatrixRoom, message: Event, matrix_client: MatrixClient): # ne répond qu'en message direct ou dans un salon s'il est interpellé avec @identifiant.du.bot dans le corps du message if len(room.users) > 2 and not (message.formatted_body and matrix_client.user_id in message.formatted_body): raise EventNotConcerned - + reponse = parle() # il envoie l'information qu'il est en train d'écrire await matrix_client.room_typing(room.room_id) await asyncio.sleep(len(reponse) / 50) # il envoie le message + await matrix_client.room_typing(room.room_id, typing_state=False) await matrix_client.send_html_message(room.room_id, reponse, "m.notice")