Compare commits

..

6 Commits

Author SHA1 Message Date
Adrien.Malingrey
813a66288d messages non lus 2026-02-16 15:36:47 +01:00
484b5f7119 on indique qu'on écrit avant de commencer à réfléchir 2026-02-11 18:18:35 +01:00
2d85f0b5de import inutile 2026-02-11 18:15:43 +01:00
Adrien.Malingrey
81dd3f6bfc Ajout fonction dune 2026-02-09 15:59:01 +01:00
Adrien.Malingrey
9d4080b42f config 2026-02-09 10:11:19 +01:00
c82364cdb5 Actualiser README.md 2026-02-08 20:45:48 +01:00
4 changed files with 77 additions and 56 deletions

View File

@@ -5,4 +5,5 @@ SYSTEMD_LOGGING=False
matrix_home_server="https://matrix.agent.ministere_example.tchap.gouv.fr" matrix_home_server="https://matrix.agent.ministere_example.tchap.gouv.fr"
matrix_bot_username="jean.quidam@ministere_example.gouv.fr" matrix_bot_username="jean.quidam@ministere_example.gouv.fr"
matrix_bot_password="test" matrix_bot_password="test"
join_on_invite=True
#https_proxy=http://adresse.du.proxy:port/ #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 Executable file → Normal file
View File

View File

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