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