diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index 5d33623..d2e9859 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .venv/ session.txt store/ +__pycache__ diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/callbacks.py b/callbacks.py new file mode 100755 index 0000000..e2b4be2 --- /dev/null +++ b/callbacks.py @@ -0,0 +1,26 @@ +from nio import MatrixRoom, Event + +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""" + + @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) + + return inner() + + return decorated diff --git a/config.py b/config.py new file mode 100755 index 0000000..5dca58d --- /dev/null +++ b/config.py @@ -0,0 +1,9 @@ +from pydantic import Field + +from tchap_bot.config import Config + + +class ConfigProxy(Config): + https_proxy: str = Field("", description="Proxy URL") + +env_config = ConfigProxy() diff --git a/fra_wikipedia_2021_10K-sentences.txt b/fra_wikipedia_2021_10K-sentences.txt old mode 100644 new mode 100755 diff --git a/markov.py b/markov.py old mode 100644 new mode 100755 diff --git a/tchap-beta.py b/tchap-beta.py old mode 100644 new mode 100755 index b63f463..a7f6ad6 --- a/tchap-beta.py +++ b/tchap-beta.py @@ -4,44 +4,13 @@ from functools import wraps from nio import MatrixRoom, Event from nio.crypto import ENCRYPTION_ENABLED -from pydantic import Field - from matrix_bot.bot import MatrixBot from matrix_bot.client import MatrixClient from matrix_bot.eventparser import MessageEventParser, ignore_when_not_concerned, EventNotConcerned -from matrix_bot.config import logger -from tchap_bot.config import Config - -from markov import parle - - -class ConfigProxy(Config): - https_proxy: str = Field("", description="Proxy URL") - -env_config = ConfigProxy() - - -def 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) - - 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) - - return inner() - - return decorated +from .config import env_config +from .callbacks import properly_fail +from .markov import parle # le décorateur @properly_fail va permettre à la commande de laisser un message d'erreur si la commande plante et @@ -67,6 +36,7 @@ async def repond(room: MatrixRoom, message: Event, matrix_client: MatrixClient): # il envoie le message await matrix_client.send_text_message(room.room_id, reponse, "m.notice") + tchap_bot = MatrixBot( env_config.matrix_home_server, env_config.matrix_bot_username, diff --git a/tchap-beta.service b/tchap-beta.service old mode 100644 new mode 100755