Instance de test : préparation et test de bout en bout sans envoi
fil() écarte les activités (privées dans LibreDesk) ; les outils des agents tournent avec le Python courant. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
39fa4cc708
commit
690204a634
8 files changed
+134
-4
No files matched your search
@@ -0,0 +1,69 @@
|
||||
"""Prépare une instance de test neuve : boîte e-mail (SMTP = Mailpit), étiquettes du contrat, rôle « Agent IA », son
|
||||
agent et sa clé, une conversation du contact avec le mail du fichier, étiquetée tri:diagnostic. Écrit
|
||||
instance-de-test/.env (LIBREDESK_URL, LIBREDESK_API_KEY, LIBREDESK_API_SECRET, CONVERSATION).
|
||||
|
||||
python -I instance-de-test/preparer.py <mot de passe System> <fichier du mail>
|
||||
"""
|
||||
import html
|
||||
import http.cookiejar
|
||||
import json
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
B = "http://127.0.0.1:9100"
|
||||
ETIQUETTES = ["tri:diagnostic", "diag:en-cours", "diag:fait", "diag:erreur", "diag:ignore", "diag:relancer"]
|
||||
PERMISSIONS = ["conversations:read", "conversations:read_all", "messages:read", "messages:write_private",
|
||||
"conversations:update_tags"]
|
||||
jar = http.cookiejar.CookieJar()
|
||||
op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))
|
||||
|
||||
|
||||
def req(m, p, body=None):
|
||||
"""Une requête de l'admin (session) ; s'arrête sur une erreur."""
|
||||
r = urllib.request.Request(B + p, method=m, data=json.dumps(body).encode() if body is not None else None,
|
||||
headers={"Content-Type": "application/json", "X-CSRFTOKEN": "t0k3n"})
|
||||
try:
|
||||
with op.open(r) as x:
|
||||
return json.loads(x.read() or b"{}").get("data")
|
||||
except urllib.error.HTTPError as e:
|
||||
sys.exit(f"{m} {p} : {e.code} {e.read().decode()[:300]}")
|
||||
|
||||
|
||||
def main(mot_de_passe: str, fichier: str) -> None:
|
||||
req("POST", "/api/v1/auth/login", {"email": "System", "password": mot_de_passe})
|
||||
for c in jar:
|
||||
c.secure = False # cookies Secure : http local
|
||||
jar.set_cookie(http.cookiejar.Cookie(0, "csrf_token", "t0k3n", None, False, "127.0.0.1", False, False, "/", True,
|
||||
False, None, False, None, None, {})) # le serveur compare cookie et en-tête
|
||||
smtp = {"enabled": True, "host": "mailpit", "port": 1025, "username": "", "password": "", "auth_protocol": "none",
|
||||
"tls_type": "none", "max_conns": 1, "max_msg_retries": 0, "idle_timeout": "5s", "pool_wait_timeout": "5s",
|
||||
"hello_hostname": ""}
|
||||
imap = {"host": "127.0.0.1", "port": 1, "username": "x", "password": "x", "mailbox": "INBOX",
|
||||
"read_interval": "1h", "scan_inbox_since": "1h", "tls_type": "none"}
|
||||
boite = req("POST", "/api/v1/inboxes", {"name": "Test", "channel": "email", "enabled": True, "csat_enabled": False,
|
||||
"from": "Support <support@example.com>",
|
||||
"config": {"auth_type": "password", "from": "Support <support@example.com>",
|
||||
"smtp": [smtp], "imap": [imap]}})
|
||||
for nom in ETIQUETTES:
|
||||
req("POST", "/api/v1/tags", {"name": nom})
|
||||
req("POST", "/api/v1/roles", {"name": "Agent IA", "description": "notes privées seulement",
|
||||
"permissions": PERMISSIONS})
|
||||
agent = req("POST", "/api/v1/agents", {"first_name": "Agent", "last_name": "IA", "email": "agent-ia@example.com",
|
||||
"roles": ["Agent IA"], "send_welcome_email": False, "enabled": True})
|
||||
cle = req("POST", f"/api/v1/agents/{agent['id']}/api-key")
|
||||
texte = Path(fichier).read_text().strip()
|
||||
contenu = "".join(f"<p>{html.escape(p).replace(chr(10), '<br>')}</p>" for p in texte.split("\n\n"))
|
||||
conv = req("POST", "/api/v1/conversations", {"inbox_id": boite["id"], "contact_email": "client@example.com",
|
||||
"first_name": "Client", "last_name": "Test", "initiator": "contact",
|
||||
"subject": "Courbes de la station", "content": contenu})
|
||||
req("POST", f"/api/v1/conversations/{conv['uuid']}/tags", {"tags": ["tri:diagnostic"], "action": "add_tags"})
|
||||
env = Path(__file__).with_name(".env")
|
||||
env.write_text(f"LIBREDESK_URL={B}\nLIBREDESK_API_KEY={cle['api_key']}\nLIBREDESK_API_SECRET={cle['api_secret']}\n"
|
||||
f"CONVERSATION={conv['uuid']}\n")
|
||||
print(f"prête : conversation {conv['uuid']}, réglages dans {env}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(*sys.argv[1:3])
|
||||
Reference in new issue
Block a user