refactor
This commit is contained in:
15
.env
Normal file → Executable file
15
.env
Normal file → Executable file
@@ -1,8 +1,7 @@
|
|||||||
## Matrix env variables
|
VERBOSE=True
|
||||||
|
SYSTEMD_LOGGING=True
|
||||||
VERBOSE=True
|
matrix_home_server="https://matrix.agent.dev-durable.tchap.gouv.fr"
|
||||||
SYSTEMD_LOGGING=False
|
matrix_bot_username="@chat.beta-developpement-durable.gouv.fr:agent.dev-durable.tchap.gouv.fr"
|
||||||
matrix_home_server="https://matrix.agent.ministere_example.tchap.gouv.fr"
|
matrix_bot_password="C.5;oTYw+Mksn<,7jtgx/TbWHY2Ugf"
|
||||||
matrix_bot_username="jean.quidam@ministere_example.gouv.fr"
|
salt=b'I\x87>\xe5\x91\xbdw\x99\xb3\xea7\xc2\xa6\xbf\x8f#'
|
||||||
matrix_bot_password="test"
|
https_proxy=http://pfrie-std.proxy.e2.rie.gouv.fr:8080/
|
||||||
#https_proxy=http://adresse.du.proxy:port/
|
|
||||||
|
|||||||
1
.gitignore
vendored
Normal file → Executable file
1
.gitignore
vendored
Normal file → Executable file
@@ -2,3 +2,4 @@
|
|||||||
.venv/
|
.venv/
|
||||||
session.txt
|
session.txt
|
||||||
store/
|
store/
|
||||||
|
__pycache__
|
||||||
|
|||||||
26
callbacks.py
Executable file
26
callbacks.py
Executable file
@@ -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
|
||||||
9
config.py
Executable file
9
config.py
Executable file
@@ -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()
|
||||||
0
fra_wikipedia_2021_10K-sentences.txt
Normal file → Executable file
0
fra_wikipedia_2021_10K-sentences.txt
Normal file → Executable file
38
tchap-beta.py
Normal file → Executable file
38
tchap-beta.py
Normal file → Executable file
@@ -4,44 +4,13 @@ from functools import wraps
|
|||||||
from nio import MatrixRoom, Event
|
from nio import MatrixRoom, Event
|
||||||
from nio.crypto import ENCRYPTION_ENABLED
|
from nio.crypto import ENCRYPTION_ENABLED
|
||||||
|
|
||||||
from pydantic import Field
|
|
||||||
|
|
||||||
from matrix_bot.bot import MatrixBot
|
from matrix_bot.bot import MatrixBot
|
||||||
from matrix_bot.client import MatrixClient
|
from matrix_bot.client import MatrixClient
|
||||||
from matrix_bot.eventparser import MessageEventParser, ignore_when_not_concerned, EventNotConcerned
|
from matrix_bot.eventparser import MessageEventParser, ignore_when_not_concerned, EventNotConcerned
|
||||||
from matrix_bot.config import logger
|
|
||||||
from tchap_bot.config import Config
|
|
||||||
|
|
||||||
|
from .config import env_config
|
||||||
from markov import parle
|
from .callbacks import properly_fail
|
||||||
|
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
|
|
||||||
|
|
||||||
|
|
||||||
# le décorateur @properly_fail va permettre à la commande de laisser un message d'erreur si la commande plante et
|
# 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
|
# il envoie le message
|
||||||
await matrix_client.send_text_message(room.room_id, reponse, "m.notice")
|
await matrix_client.send_text_message(room.room_id, reponse, "m.notice")
|
||||||
|
|
||||||
|
|
||||||
tchap_bot = MatrixBot(
|
tchap_bot = MatrixBot(
|
||||||
env_config.matrix_home_server,
|
env_config.matrix_home_server,
|
||||||
env_config.matrix_bot_username,
|
env_config.matrix_bot_username,
|
||||||
|
|||||||
0
tchap-beta.service
Normal file → Executable file
0
tchap-beta.service
Normal file → Executable file
Reference in New Issue
Block a user