Deadmon commited on
Commit
53ef5b1
·
verified ·
1 Parent(s): b15c8b4

Upload 2 files

Browse files
Files changed (2) hide show
  1. create_webhook.txt +16 -0
  2. webhook_handler.py +90 -0
create_webhook.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ DAILY_API_KEY="your-daily-api-key" # Replace with your Daily.co API key
4
+ WEBHOOK_URL="https://<your-space>.hf.space/webhook" # Replace with your endpoint
5
+ WEBHOOK_SECRET=$(echo -n "your-secret-string" | base64) # Generate a Base64-encoded secret
6
+
7
+ curl --request POST \
8
+ --url https://api.daily.co/v1/webhooks \
9
+ --header "Authorization: Bearer $DAILY_API_KEY" \
10
+ --header "Content-Type: application/json" \
11
+ --data '{
12
+ "endpoint": "'"$WEBHOOK_URL"'",
13
+ "eventTypes": ["dialin.connected", "dialin.stopped"],
14
+ "hmac": "'"$WEBHOOK_SECRET"'",
15
+ "retryType": "circuit-breaker"
16
+ }'
webhook_handler.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import hmac
3
+ import hashlib
4
+ from http.server import BaseHTTPRequestHandler, HTTPServer
5
+ import logging
6
+
7
+ # Configure logging
8
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
9
+ logger = logging.getLogger(__name__)
10
+
11
+ # Webhook secret for verification (set this when creating the webhook)
12
+ WEBHOOK_SECRET = "your-base64-encoded-secret" # Replace with your secret
13
+
14
+ class WebhookHandler(BaseHTTPRequestHandler):
15
+ def do_POST(self):
16
+ try:
17
+ # Read the request body
18
+ content_length = int(self.headers['Content-Length'])
19
+ post_data = self.rfile.read(content_length).decode('utf-8')
20
+ payload = json.loads(post_data)
21
+
22
+ # Verify webhook signature
23
+ signature = self.headers.get('X-Daily-Signature')
24
+ if not self.verify_signature(post_data, signature):
25
+ logger.warning("Invalid webhook signature")
26
+ self.send_response(401)
27
+ self.end_headers()
28
+ return
29
+
30
+ # Handle Daily.co dial-in events
31
+ event_type = payload.get('event')
32
+ if event_type in ['dialin.connected', 'dialin.stopped']:
33
+ logger.info(f"Received {event_type} event: {json.dumps(payload, indent=2)}")
34
+
35
+ # Example: Process dial-in event
36
+ if event_type == 'dialin.connected':
37
+ call_id = payload.get('callId')
38
+ caller = payload.get('From')
39
+ logger.info(f"Dial-in connected: Call ID {call_id} from {caller}")
40
+ # Trigger bot startup (e.g., call /start endpoint)
41
+ self.handle_dialin_connected(payload)
42
+ elif event_type == 'dialin.stopped':
43
+ call_id = payload.get('callId')
44
+ logger.info(f"Dial-in stopped: Call ID {call_id}")
45
+
46
+ # Send 200 OK response
47
+ self.send_response(200)
48
+ self.send_header('Content-type', 'application/json')
49
+ self.end_headers()
50
+ self.wfile.write(b'{"status": "Event received"}')
51
+ else:
52
+ logger.info(f"Ignored event type: {event_type}")
53
+ self.send_response(200)
54
+ self.end_headers()
55
+
56
+ except Exception as e:
57
+ logger.error(f"Error processing webhook: {str(e)}")
58
+ self.send_response(500)
59
+ self.end_headers()
60
+
61
+ def verify_signature(self, payload, signature):
62
+ """Verify the webhook signature using HMAC-SHA256."""
63
+ if not signature or not WEBHOOK_SECRET:
64
+ return False
65
+ computed = hmac.new(
66
+ WEBHOOK_SECRET.encode('utf-8'),
67
+ payload.encode('utf-8'),
68
+ hashlib.sha256
69
+ ).hexdigest()
70
+ return hmac.compare_digest(computed, signature)
71
+
72
+ def handle_dialin_connected(self, payload):
73
+ """Handle dialin.connected event (e.g., trigger bot startup)."""
74
+ # Example: Send payload to /start endpoint (modify URL as needed)
75
+ import requests
76
+ start_url = "https://<your-space>.hf.space/start" # Replace with your endpoint
77
+ try:
78
+ response = requests.post(start_url, json=payload, timeout=5)
79
+ logger.info(f"Sent to /start: {response.status_code} - {response.text}")
80
+ except requests.RequestException as e:
81
+ logger.error(f"Failed to send to /start: {str(e)}")
82
+
83
+ def run(server_class=HTTPServer, handler_class=WebhookHandler, port=8000):
84
+ server_address = ('', port)
85
+ httpd = server_class(server_address, handler_class)
86
+ logger.info(f"Starting webhook server on port {port}...")
87
+ httpd.serve_forever()
88
+
89
+ if __name__ == '__main__':
90
+ run()