commit 29fd7d21c894fd95291c2cfe0fba7e5b83fab2e9 Author: ggendron Date: Tue Jul 16 12:29:58 2024 +0000 Téléverser les fichiers vers "/" diff --git a/.env b/.env new file mode 100644 index 0000000..7d8f006 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +VIRTUAL_ENV=C:\Program Files (x86)\wapt\ +PYTHONPATH=C:\Program Files (x86)\wapt\ diff --git a/rocket.ini b/rocket.ini new file mode 100644 index 0000000..0cd23b5 --- /dev/null +++ b/rocket.ini @@ -0,0 +1,2 @@ +[rocket] +url= https://chat.comitari.fr/hooks/64d4d02760b38508f62a5bcb/ncKSYRiLM9oNXagK5c7G3KWX2qEzET3kbFFXKnNAhtfZQEQ9 \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1e496b8 --- /dev/null +++ b/setup.py @@ -0,0 +1,125 @@ +# -*- coding: utf-8 -*- +from setuphelpers import * +import xmlrpc.client +import pickle +import os +from configparser import ConfigParser +import requests +import json + +# Configuration +url = 'https://odoo.comitari.fr' +db = 'comitari' +state_file = 'tickets_state.pkl' +api_key = "182534f9e754ce1016e86b86b4b5b47199659a6f" +headers = { + 'Content-Type': 'application/json', + 'Authorization': f'Bearer {api_key}', +} + +# Endpoint URL +endpoint = f'{url}/jsonrpc' + +# Payload pour récupérer tous les tickets +payload = { + 'jsonrpc': '2.0', + 'method': 'call', + 'params': { + 'model': 'helpdesk.ticket', + 'method': 'search_read', + 'args': [[], ['id', 'write_date']], + }, + 'id': 1, +} + +def install(): + + plugin_inifile = makepath(WAPT.private_dir, "rocket.ini") + + if not isfile(plugin_inifile): + filecopyto("rocket.ini", WAPT.private_dir) + +def audit(): + + try: + # Récupération de l'état actuel de tous les tickets + response = requests.post(endpoint, json=payload, headers=headers) + response.raise_for_status() # Vérifie si la requête a réussi + response_data = response.json() + + # Vérifie si 'result' est présent dans la réponse + if 'result' in response_data: + tickets = response_data['result'] + else: + print("Erreur: La clé 'result' n'est pas présente dans la réponse.") + print("Réponse complète:", response_data) + tickets = [] + except requests.exceptions.RequestException as e: + print(f"Erreur lors de la requête: {e}") + tickets = [] + + # Vérification si un état précédent existe + if os.path.exists(state_file): + with open(state_file, 'rb') as f: + previous_tickets_state = pickle.load(f) + else: + previous_tickets_state = {} + + + # Comparaison et affichage des résultats + tickets_updated = [] + for ticket in tickets: + ticket_id = ticket['id'] + ticket_write_date = ticket['write_date'] + + if ticket_id in previous_tickets_state: + if ticket_write_date != previous_tickets_state[ticket_id]: + tickets_updated.auppend(ticket_id) + else: + tickets_updated.append(ticket_id) + + if tickets_updated: + message = f"Les tickets suivants ont été mis à jour depuis la dernière vérification : {tickets_updated}" + send_to_rocket(message) + + else: + message = "Aucun ticket n'a été mis à jour depuis la dernière vérification." + send_to_rocket(message) + # Sauvegarde de l'état actuel pour la prochaine exécution + current_tickets_state = {ticket['id']: ticket['write_date'] for ticket in tickets} + with open(state_file, 'wb') as f: + pickle.dump(current_tickets_state, f) + return "OK" + + +def send_to_rocket(message_text, attachments=None): + """ + Envoie un message à Rocket.Chat via un webhook. + + :param message_text: Texte du message à envoyer + :param attachments: Liste de pièces jointes (facultatif) + """ + smtp_inifile = makepath(WAPT.private_dir, "rocket.ini") + conf_wapt = ConfigParser() + conf_wapt.read(smtp_inifile) + + webhook_url = conf_wapt.get("rocket", "url") + + # Construire le message + + message = { + 'text': message_text + } + if attachments: + message['attachments'] = attachments + + # Envoyer la requête POST + response = requests.post(webhook_url, data=json.dumps(message), headers={'Content-Type': 'application/json'}) + + # Vérifier la réponse + if response.status_code == 200: + print('Message envoyé avec succès.') + else: + print(f'Échec de l\'envoi du message. Statut de la réponse : {response.status_code}') + print(f'Erreur : {response.text}') + diff --git a/tickets_state.pkl b/tickets_state.pkl new file mode 100644 index 0000000..e2ecf72 --- /dev/null +++ b/tickets_state.pkl @@ -0,0 +1 @@ +€}”. \ No newline at end of file