Compare commits

..

20 Commits

Author SHA1 Message Date
Adrien.Malingrey
91129fc073 message d'erreur personnalisable 2026-02-06 15:21:00 +01:00
Adrien.Malingrey
69a965824c html 2026-02-06 11:12:46 +01:00
68d881d830 ignorer les ajouts dans corpus 2026-02-05 20:16:26 +01:00
6c97838158 importer tout le dossier corpus 2026-02-05 20:15:56 +01:00
db8cdb0670 test 2026-02-05 17:08:21 +01:00
ccba64ed35 plusieurs sources, plusieurs phrases 2026-02-05 16:02:30 +01:00
root
b4182cb71f correction imports 2026-02-04 18:39:27 +01:00
root
173bb371eb refactor 2026-02-04 18:35:38 +01:00
MALINGREY Adrien
3712c4ddbd personnalisation du gestionnaire d'erreur 2026-02-04 16:57:27 +00:00
MALINGREY Adrien
dccbaa332a répond en message direct ou dans les salons publics en cas d'interpellation 2026-02-04 16:33:57 +00:00
MALINGREY Adrien
75b0fb91b3 renommage 2026-02-04 16:01:45 +00:00
MALINGREY Adrien
dc9106b8fd Ajout EnvironmentFile 2026-02-04 16:01:18 +00:00
MALINGREY Adrien
df7bd7415c ajout chown 2026-02-04 15:56:50 +00:00
MALINGREY Adrien
835b415bc6 Ajout de commentaires 2026-02-04 14:52:42 +00:00
MALINGREY Adrien
1be9fd8a1c renommage des fichiers en tchap-beta 2026-02-04 14:34:17 +00:00
MALINGREY Adrien
361f22e014 Installation en tant que service 2026-02-04 14:30:26 +00:00
MALINGREY Adrien
76d0627aa9 Add .gitignore 2026-02-04 14:13:23 +00:00
MALINGREY Adrien
f7bd4393f2 Edit README.md 2026-02-04 14:06:01 +00:00
MALINGREY Adrien
caf04ec659 Edit README.md 2026-02-04 12:49:19 +00:00
MALINGREY Adrien
a2c0fece12 premier commit 2026-02-04 12:42:17 +00:00
5 changed files with 63 additions and 85 deletions

7
.env Executable file
View File

@@ -0,0 +1,7 @@
VERBOSE=True
SYSTEMD_LOGGING=True
matrix_home_server="https://matrix.agent.dev-durable.tchap.gouv.fr"
matrix_bot_username="@chat.beta-developpement-durable.gouv.fr:agent.dev-durable.tchap.gouv.fr"
matrix_bot_password="C.5;oTYw+Mksn<,7jtgx/TbWHY2Ugf"
salt=b'I\x87>\xe5\x91\xbdw\x99\xb3\xea7\xc2\xa6\xbf\x8f#'
https_proxy=http://pfrie-std.proxy.e2.rie.gouv.fr:8080/

View File

@@ -1,9 +0,0 @@
## Matrix env variables
VERBOSE=True
SYSTEMD_LOGGING=False
matrix_home_server="https://matrix.agent.ministere_example.tchap.gouv.fr"
matrix_bot_username="jean.quidam@ministere_example.gouv.fr"
matrix_bot_password="test"
join_on_invite=True
#https_proxy=http://adresse.du.proxy:port/

View File

@@ -20,7 +20,7 @@ pip install \
tchap-bot --index-url https://code.peren.fr/api/v4/projects/83/packages/pypi/simple tchap-bot --index-url https://code.peren.fr/api/v4/projects/83/packages/pypi/simple
# Renseignez les informations de connexion : # Renseignez les informations de connexion :
$EDITOR .env editor .env
python -c 'import secrets; print("salt=", secrets.token_bytes(16), sep="")' >> .env python -c 'import secrets; print("salt=", secrets.token_bytes(16), sep="")' >> .env
# Lancer # Lancer

0
corpus/fra_wikipedia_2021_10K-sentences.txt Normal file → Executable file
View File

View File

@@ -1,75 +1,55 @@
import asyncio import asyncio
from nio import MatrixRoom, Event from nio import MatrixRoom, Event
from nio.crypto import ENCRYPTION_ENABLED
from matrix_bot.bot import MatrixBot
from matrix_bot.client import MatrixClient from matrix_bot.bot import MatrixBot
from matrix_bot.eventparser import MessageEventParser, ignore_when_not_concerned, EventNotConcerned from matrix_bot.client import MatrixClient
from matrix_bot.eventparser import MessageEventParser, ignore_when_not_concerned, EventNotConcerned
from config import env_config
from callbacks import properly_fail from config import env_config
from markov import parle from callbacks import properly_fail
from markov import parle
async def non_lus(room_id):
room = tchap_bot.matrix_client.rooms[room_id] # le décorateur @properly_fail va permettre à la commande de laisser un message d'erreur si la commande plante et
if room.unread_notifications: # d'envoyer le message que le bot n'est plus en train d'écrire
response = await tchap_bot.matrix_client.room_messages(room_id=room_id, limit=room.unread_notifications, direction="back", start="") # la fonction va être appelée dans tous les cas, le décorateur @ignore_when_not_concerned
for message in response.chunk: # permet de laisser event_parser gérer le cas où la commande n'est pas concernée
await repond(room, message, tchap_bot.matrix_client) @properly_fail("Oups, j'ai buggué 😿")
@ignore_when_not_concerned
async def repond(room: MatrixRoom, message: Event, matrix_client: MatrixClient):
# le décorateur @properly_fail va permettre à la commande de laisser un message d'erreur si la commande plante et # on initialise un event_parser pour décider à quel message cette commande va répondre
# d'envoyer le message que le bot n'est plus en train d'écrire event_parser = MessageEventParser(room=room, event=message, matrix_client=matrix_client)
# la fonction va être appelée dans tous les cas, le décorateur @ignore_when_not_concerned # il ne va pas répondre à ses propres messages
# permet de laisser event_parser gérer le cas où la commande n'est pas concernée event_parser.do_not_accept_own_message()
@properly_fail("Oups, j'ai buggué 😿")
@ignore_when_not_concerned # ne répond qu'en message direct ou dans un salon s'il est interpellé avec @identifiant.du.bot dans le corps du message
async def repond(room: MatrixRoom, message: Event, matrix_client: MatrixClient): if len(room.users) > 2 and not (message.formatted_body and matrix_client.user_id in message.formatted_body):
# on initialise un event_parser pour décider à quel message cette commande va répondre raise EventNotConcerned
event_parser = MessageEventParser(room=room, event=message, matrix_client=matrix_client)
# il ne va pas répondre à ses propres messages reponse = parle()
event_parser.do_not_accept_own_message() # 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)
if not hasattr(message, "body") or not hasattr(message, "formatted_body"): # il envoie le message
# message non textuel await matrix_client.room_typing(room.room_id, typing_state=False)
raise EventNotConcerned await matrix_client.send_html_message(room.room_id, reponse, "m.notice")
# 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): tchap_bot = MatrixBot(
raise EventNotConcerned env_config.matrix_home_server,
env_config.matrix_bot_username,
# il envoie l'information qu'il est en train d'écrire env_config.matrix_bot_password,
await matrix_client.room_typing(room.room_id) proxy=env_config.https_proxy,
reponse = parle() use_functions=True,
await asyncio.sleep(len(reponse) / 50) ssl=True
# 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) tchap_bot.matrix_client.matrix_config.encryption_enabled = True
tchap_bot.matrix_client.matrix_config.ignore_unverified_devices = True
tchap_bot.matrix_client.matrix_config.join_on_invite = True
@properly_fail("Oups, j'ai buggué 😿")
@ignore_when_not_concerned tchap_bot.callbacks.register_on_message_event(repond)
async def dune(room: MatrixRoom, message: Event, matrix_client: MatrixClient): tchap_bot.run()
event_parser = MessageEventParser(room=room, event=message, matrix_client=matrix_client)
event_parser.do_not_accept_own_message()
if ("Adrien" in message.body or "adrien" in message.body):
await matrix_client.room_typing(room.room_id)
await matrix_client.send_html_message(room.room_id, "<em>Lisan al-Gaib !</em>", "m.notice")
tchap_bot = MatrixBot(
env_config.matrix_home_server,
env_config.matrix_bot_username,
env_config.matrix_bot_password,
proxy=env_config.https_proxy,
use_functions=True,
ssl=True
)
tchap_bot.callbacks.register_on_startup(non_lus)
tchap_bot.callbacks.register_on_message_event(dune)
tchap_bot.callbacks.register_on_message_event(repond)
tchap_bot.run()