-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_handler_example.php
More file actions
189 lines (154 loc) · 5.74 KB
/
webhook_handler_example.php
File metadata and controls
189 lines (154 loc) · 5.74 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
require 'vendor/autoload.php';
use CloudContactAI\CCAI\CCAI;
use CloudContactAI\CCAI\WebhookEventType;
use CloudContactAI\CCAI\Webhook;
use CloudContactAI\CCAI\MessageSentEvent;
use CloudContactAI\CCAI\MessageReceivedEvent;
// Example webhook handler for different PHP frameworks
// 1. Simple PHP webhook handler (can be used in any framework)
function handleWebhookPayload($payload) {
echo "Received webhook payload:\n";
$eventType = $payload['type'] ?? null;
if ($eventType === WebhookEventType::MESSAGE_SENT) {
$event = new MessageSentEvent($payload);
echo "✅ Message sent event:\n";
echo " Campaign: {$event->campaign->title} (ID: {$event->campaign->id})\n";
echo " From: {$event->from}\n";
echo " To: {$event->to}\n";
echo " Message: {$event->message}\n";
// Add your custom logic here
// For example: update database, send notifications, etc.
} elseif ($eventType === WebhookEventType::MESSAGE_RECEIVED) {
$event = new MessageReceivedEvent($payload);
echo "📨 Message received event:\n";
echo " Campaign: {$event->campaign->title} (ID: {$event->campaign->id})\n";
echo " From: {$event->from}\n";
echo " To: {$event->to}\n";
echo " Message: {$event->message}\n";
// Add your custom logic here
// For example: auto-reply, update CRM, etc.
}
return ['received' => true];
}
// 2. Using the built-in webhook handler
$handlers = [
'onMessageSent' => function($event) {
echo "🚀 Handler: Message sent to {$event->to}\n";
echo " Campaign: {$event->campaign->title}\n";
echo " Content: {$event->message}\n";
},
'onMessageReceived' => function($event) {
echo "📥 Handler: Message received from {$event->from}\n";
echo " Campaign: {$event->campaign->title}\n";
echo " Content: {$event->message}\n";
}
];
$webhookHandler = Webhook::createHandler($handlers);
// Test with sample payloads
echo "=== Testing Webhook Handler ===\n\n";
// Test message sent event
$messageSentPayload = [
'type' => WebhookEventType::MESSAGE_SENT,
'campaign' => [
'id' => 12345,
'title' => 'Welcome Campaign',
'message' => 'Welcome to our service!',
'senderPhone' => '+15551234567',
'createdAt' => '2025-01-17T10:00:00Z',
'runAt' => '2025-01-17T10:00:00Z'
],
'from' => '+15551234567',
'to' => '+14156961732',
'message' => 'Hello Andreas, welcome to our service!'
];
echo "1. Testing MESSAGE_SENT event:\n";
handleWebhookPayload($messageSentPayload);
echo "\n";
echo "2. Testing with built-in handler:\n";
$result = $webhookHandler($messageSentPayload);
var_dump($result);
echo "\n";
// Test message received event
$messageReceivedPayload = [
'type' => WebhookEventType::MESSAGE_RECEIVED,
'campaign' => [
'id' => 12345,
'title' => 'Welcome Campaign',
'message' => 'Welcome to our service!',
'senderPhone' => '+15551234567',
'createdAt' => '2025-01-17T10:00:00Z',
'runAt' => '2025-01-17T10:00:00Z'
],
'from' => '+14156961732',
'to' => '+15551234567',
'message' => 'Thank you! This looks great.'
];
echo "3. Testing MESSAGE_RECEIVED event:\n";
handleWebhookPayload($messageReceivedPayload);
echo "\n";
echo "4. Testing with built-in handler:\n";
$result = $webhookHandler($messageReceivedPayload);
var_dump($result);
echo "\n=== Webhook Handler Examples ===\n\n";
// Example for different PHP frameworks:
echo "// Example 1: Plain PHP webhook endpoint (webhook.php)\n";
echo '<?php
require "vendor/autoload.php";
use CloudContactAI\CCAI\Webhook;
$handlers = [
"onMessageSent" => function($event) {
// Log or process sent message
error_log("Message sent: " . $event->message);
},
"onMessageReceived" => function($event) {
// Process received message
error_log("Reply received: " . $event->message);
}
];
$webhookHandler = Webhook::createHandler($handlers);
// Get the raw POST data
$payload = json_decode(file_get_contents("php://input"), true);
// Process the webhook
$result = $webhookHandler($payload);
// Return JSON response
header("Content-Type: application/json");
echo json_encode($result);
';
echo "\n\n// Example 2: Laravel webhook route\n";
echo '// In routes/api.php
Route::post("/ccai-webhook", function (Request $request) {
$handlers = [
"onMessageSent" => function($event) {
Log::info("Message sent", ["to" => $event->to, "message" => $event->message]);
},
"onMessageReceived" => function($event) {
Log::info("Message received", ["from" => $event->from, "message" => $event->message]);
}
];
$webhookHandler = Webhook::createHandler($handlers);
$result = $webhookHandler($request->all());
return response()->json($result);
});
';
echo "\n\n// Example 3: Symfony webhook controller\n";
echo '// In your controller
public function webhook(Request $request): JsonResponse
{
$handlers = [
"onMessageSent" => function($event) {
$this->logger->info("Message sent", ["event" => $event]);
},
"onMessageReceived" => function($event) {
$this->logger->info("Message received", ["event" => $event]);
}
];
$webhookHandler = Webhook::createHandler($handlers);
$payload = json_decode($request->getContent(), true);
$result = $webhookHandler($payload);
return new JsonResponse($result);
}
';
echo "\n\nWebhook handler examples completed!\n";
echo "Note: The webhook management API (/webhooks endpoint) is not available yet,\n";
echo "but the webhook handler functionality works perfectly for processing incoming webhooks.\n";