File size: 2,115 Bytes
7d72bcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import requests
import logging
from datetime import datetime
import json

logger = logging.getLogger(__name__)

class WebhookManager:
    def __init__(self, customer):
        self.customer = customer
        self.webhook_url = customer.webhook_url

    def send_call_results(self, call_data):
        """Send call analysis results to customer's webhook URL"""
        if not self.webhook_url:
            logger.warning(f"No webhook URL configured for customer {self.customer.id}")
            return False

        try:
            # Prepare the payload
            payload = {
                'call_id': call_data.get('id'),
                'caller_number': call_data.get('caller_number'),
                'called_number': call_data.get('called_number'),
                'transcription': call_data.get('transcription'),
                'summary': call_data.get('summary'),
                'sentiment': call_data.get('sentiment'),
                'keywords': call_data.get('keywords'),
                'timestamp': datetime.utcnow().isoformat(),
                'customer_id': self.customer.id
            }

            # Send POST request to webhook URL
            response = requests.post(
                self.webhook_url,
                json=payload,
                headers={'Content-Type': 'application/json'},
                timeout=10  # 10 seconds timeout
            )

            # Check if request was successful
            if response.status_code in [200, 201, 202]:
                logger.info(f"Successfully sent call results to webhook for customer {self.customer.id}")
                return True
            else:
                logger.error(f"Failed to send call results to webhook. Status code: {response.status_code}")
                return False

        except requests.exceptions.RequestException as e:
            logger.error(f"Error sending webhook for customer {self.customer.id}: {str(e)}")
            return False
        except Exception as e:
            logger.error(f"Unexpected error sending webhook for customer {self.customer.id}: {str(e)}")
            return False