Browse Source

send notifications to webhooks

Guilhem Saurel 6 years ago
parent
commit
ee16316349
4 changed files with 16 additions and 1 deletions
  1. 2 0
      djadhere/settings.py
  2. 11 0
      djadhere/utils.py
  3. 1 0
      requirements.txt
  4. 2 1
      services/utils/notifications.py

+ 2 - 0
djadhere/settings.py

@@ -155,3 +155,5 @@ LEAFLET_CONFIG = {
 }
 
 CHECK_MK_URL = 'https://nagios.tetaneutral.net/ttnn/check_mk/index.py?start_url=%2Fttnn%2Fcheck_mk%2Fview.py%3Fhost%3D{host}%26site%3Dttnn%26view_name%3Dhoststatus'
+
+WEBHOOK = False

+ 11 - 0
djadhere/utils.py

@@ -90,6 +90,17 @@ def send_notification(subject, message, recipients, **kwargs):
     mail.send()
 
 
+def send_web_notification(subject, message):
+    if settings.WEBHOOK:
+        import requests
+        requests.post(
+            settings.WEBHOOK_URL,
+            data=json.dumps({
+                'text': '%s\n%s' % (subject, message),
+                'key': settings.WEBHOOK_KEY,
+            }))
+
+
 def from_livestatus(get, query=[], columns=[]):
     query = ['GET %s' % get] + query
     if columns:

+ 1 - 0
requirements.txt

@@ -4,3 +4,4 @@ django-leaflet
 django-geojson
 django-mptt
 ipaddress
+requests

+ 2 - 1
services/utils/notifications.py

@@ -1,7 +1,7 @@
 from django.urls import reverse
 from django.conf import settings
 
-from djadhere.utils import send_notification
+from djadhere.utils import send_notification, send_web_notification
 
 
 # À simplifier : seul la route peut changer maintenant
@@ -45,3 +45,4 @@ def notify_allocation(request, new_alloc, old_alloc=None):
     if sujet:
         sujet += ' {} sur {}'.format(new_alloc.resource, new_alloc.route)
         send_notification(sujet, message, settings.ALLOCATIONS_EMAILS, cc=[benevole])
+        send_web_notification(sujet, message)