[ADD] maintenance_service_http_monitoring: webhook_rocketchat_via_n8n + parameters
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 6s

This commit is contained in:
Boris Gallet
2026-03-25 16:41:23 +01:00
parent 30a19649d2
commit 33ccb85594
4 changed files with 106 additions and 1 deletions

View File

@@ -1,6 +1,16 @@
import logging
from datetime import timedelta
from odoo import models, fields, api
try:
import requests as http_requests
except ImportError:
http_requests = None
_logger = logging.getLogger(__name__)
WEBHOOK_TIMEOUT = 10 # seconds
class MaintenanceEquipment(models.Model):
_inherit = 'maintenance.equipment'
@@ -90,8 +100,51 @@ class MaintenanceEquipment(models.Model):
vals['maintenance_team_id'] = team.id
request = self.env['maintenance.request'].create(vals)
self.http_maintenance_request = request.id
self._notify_webhook(request, ko_services)
return request
def _notify_webhook(self, request, ko_services):
"""Send a webhook notification when a new maintenance request is created."""
ICP = self.env['ir.config_parameter'].sudo()
webhook_url = ICP.get_param(
'maintenance_service_http_monitoring.webhook_url', ''
)
if not webhook_url:
return
webhook_user = ICP.get_param(
'maintenance_service_http_monitoring.webhook_user', ''
)
webhook_password = ICP.get_param(
'maintenance_service_http_monitoring.webhook_password', ''
)
base_url = ICP.get_param('web.base.url', '')
link = (
f"{base_url}/web#id={request.id}"
f"&model=maintenance.request&view_type=form"
)
payload = {
'id': request.id,
'name': request.name,
'description': request.description or '',
'equipment': self.name,
'link': link,
}
auth = None
if webhook_user and webhook_password:
auth = (webhook_user, webhook_password)
try:
http_requests.post(
webhook_url,
json=payload,
auth=auth,
timeout=WEBHOOK_TIMEOUT,
)
except Exception as e:
_logger.warning(
"Webhook notification failed for maintenance request %s: %s",
request.id, e,
)
def _build_ko_services_description(self, ko_services):
lines = [f"Service KO: {s.service_url or s.name}" for s in ko_services]
return '\n'.join(lines)