2023-12-02 19:09:34 +01:00
|
|
|
import requests
|
|
|
|
import requests.exceptions
|
|
|
|
import logging
|
2024-04-16 10:34:03 +02:00
|
|
|
from django.dispatch import receiver
|
2023-12-02 19:09:34 +01:00
|
|
|
|
2024-04-16 10:23:06 +02:00
|
|
|
from . import settings
|
2024-04-17 12:02:44 +02:00
|
|
|
from .signals import new_ticket_done, update_ticket_done
|
2024-04-16 10:23:06 +02:00
|
|
|
|
2023-12-02 19:09:34 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-04-16 10:42:03 +02:00
|
|
|
def notify_followup_webhooks(followup):
|
2023-12-02 19:09:34 +01:00
|
|
|
urls = settings.HELPDESK_GET_FOLLOWUP_WEBHOOK_URLS()
|
|
|
|
if not urls:
|
|
|
|
return
|
|
|
|
|
2024-04-16 10:42:03 +02:00
|
|
|
# Serialize the ticket associated with the followup
|
|
|
|
from .serializers import TicketSerializer
|
|
|
|
ticket = followup.ticket
|
|
|
|
ticket.set_custom_field_values()
|
|
|
|
serialized_ticket = TicketSerializer(ticket).data
|
|
|
|
|
|
|
|
# Prepare the data to send
|
|
|
|
data = {
|
|
|
|
'ticket': serialized_ticket,
|
|
|
|
'queue_slug': ticket.queue.slug,
|
|
|
|
'followup_id': followup.id
|
|
|
|
}
|
|
|
|
|
|
|
|
for url in urls:
|
|
|
|
try:
|
|
|
|
requests.post(url, json=data, timeout=settings.HELPDESK_WEBHOOK_TIMEOUT)
|
|
|
|
except requests.exceptions.Timeout:
|
|
|
|
logger.error('Timeout while sending followup webhook to %s', url)
|
|
|
|
|
|
|
|
# listener is loaded via app.py HelpdeskConfig.ready()
|
|
|
|
@receiver(update_ticket_done)
|
|
|
|
def notify_followup_webhooks_receiver(sender, followup, **kwargs):
|
2024-04-16 10:49:11 +02:00
|
|
|
notify_followup_webhooks(followup)
|
2023-12-02 19:09:34 +01:00
|
|
|
|
|
|
|
|
2023-12-27 20:34:56 +01:00
|
|
|
def send_new_ticket_webhook(ticket):
|
2023-12-02 19:09:34 +01:00
|
|
|
urls = settings.HELPDESK_GET_NEW_TICKET_WEBHOOK_URLS()
|
|
|
|
if not urls:
|
|
|
|
return
|
|
|
|
# Serialize the ticket
|
2023-12-27 20:34:56 +01:00
|
|
|
from .serializers import TicketSerializer
|
|
|
|
ticket.set_custom_field_values()
|
|
|
|
serialized_ticket = TicketSerializer(ticket).data
|
2023-12-02 19:09:34 +01:00
|
|
|
|
|
|
|
# Prepare the data to send
|
|
|
|
data = {
|
|
|
|
'ticket': serialized_ticket,
|
2023-12-27 20:34:56 +01:00
|
|
|
'queue_slug': ticket.queue.slug
|
2023-12-02 19:09:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for url in urls:
|
|
|
|
try:
|
|
|
|
requests.post(url, json=data, timeout=settings.HELPDESK_WEBHOOK_TIMEOUT)
|
|
|
|
except requests.exceptions.Timeout:
|
|
|
|
logger.error('Timeout while sending new ticket webhook to %s', url)
|
2024-04-17 12:02:44 +02:00
|
|
|
|
|
|
|
# listener is loaded via app.py HelpdeskConfig.ready()
|
|
|
|
@receiver(new_ticket_done)
|
|
|
|
def send_new_ticket_webhook_receiver(sender, ticket, **kwargs):
|
|
|
|
send_new_ticket_webhook(ticket)
|