تكامل الويب هوك Webhooks — استلام حالات تسليم رسائل واتساب
احصل على إشعارات فورية بحالات الرسائل: الإرسال (Sent)، التسليم لهاتف العميل (Delivered)، القراءة (Read)، أو الفشل (Failed). يدعم توقيع HMAC-SHA256 المشفر وسياسة إعادة محاولة آلية في حال انقطاع خادمك.
هيكل بيانات الـ Webhook Payload
يُرسل خادم EzyCode طلباً من نوع POST إلى الرابط المحدد في لوحة تحكمك متضمناً تفاصيل الحدث وتكلفة الإرسال:
{
"event": "message.delivered",
"timestamp": "2026-11-04T04:25:00Z",
"data": {
"message_id": "msg_01k9d7s3f82n4",
"recipient": "+201000000000",
"status": "delivered",
"channel": "whatsapp",
"pricing": {
"billed": true,
"cost_usd": 0.009
}
}
}التحقق والمعالجة باستخدام Node.js (Express)
// Node.js (Express) Webhook Handler
const express = require('express');
const crypto = require('crypto');
const app = express();
// Keep raw body for accurate cryptographic signature check
app.use(express.json({
verify: (req, res, buf) => {
req.rawBody = buf;
}
}));
const WEBHOOK_SECRET = process.env.EZYCODE_WEBHOOK_SECRET || 'whsec_xxxxxxxx';
app.post('/api/webhook/ezycode', (req, res) => {
const signature = req.headers['x-ezycode-signature'];
if (!signature) {
return res.status(401).json({ error: 'Missing signature header' });
}
// Calculate HMAC-SHA256 signature
const expectedSignature = crypto
.createHmac('sha256', WEBHOOK_SECRET)
.update(req.rawBody)
.digest('hex');
const isValid = crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(expectedSignature)
);
if (!isValid) {
return res.status(401).json({ error: 'Invalid HMAC signature' });
}
const { event, data, timestamp } = req.body;
console.log(`Received event ${event} for message ${data.message_id} at ${timestamp}`);
switch (event) {
case 'message.delivered':
// Update DB: recipient device received the message
break;
case 'message.read':
// Update DB: blue ticks (recipient opened message)
break;
case 'message.failed':
// Log failure reason: e.g. invalid phone number or unregistered WhatsApp account
console.error('Dispatch failed:', data.error_code, data.error_message);
break;
}
// Always respond with 200 OK promptly
res.status(200).json({ received: true });
});
app.listen(3000, () => console.log('Webhook server running on port 3000'));التحقق والمعالجة باستخدام Native PHP
<?php
// PHP Webhook Handler
$secret = getenv('EZYCODE_WEBHOOK_SECRET') ?: 'whsec_xxxxxxxx';
$rawPayload = file_get_contents('php://input');
$receivedSignature = $_SERVER['HTTP_X_EZYCODE_SIGNATURE'] ?? '';
if (empty($receivedSignature)) {
http_response_code(401);
echo json_encode(['error' => 'Missing signature']);
exit;
}
$expectedSignature = hash_hmac('sha256', $rawPayload, $secret);
if (!hash_equals($expectedSignature, $receivedSignature)) {
http_response_code(401);
echo json_encode(['error' => 'Invalid signature']);
exit;
}
$payload = json_decode($rawPayload, true);
$event = $payload['event'] ?? '';
$messageId = $payload['data']['message_id'] ?? null;
$status = $payload['data']['status'] ?? null;
// Handle idempotent database update
// ...
http_response_code(200);
echo json_encode(['received' => true]);أفضل الممارسات لهندسة الـ Webhooks
1. الرد السريع بـ 200 OK
أرسل كود 200 OK فور استلام الطلب وتأكيد التوقيع. إذا كانت لديك عمليات ثقيلة قم بتمريرها إلى Background Queue لمنع انقضاء المهلة (Timeout).
2. المعالجة الآمنة Idempotency
في حال إعادة المحاولة، قد يصل نفس الحدث مرتين. تحقق من معرّف الرسالة message_id لمنع تكرار الإجراءات في نظامك.
3. التحقق المشفر من التوقيع
لا تثق بأي طلب لا يحمل توقيع X-EzyCode-Signature متطابق مع مفتاح السر الخاص بك، لحماية نظامك من الطلبات الوهمية.
ابدأ في استقبال إشعارات رسائل واتساب الآن
اضبط رابط الـ Webhook الخاص بك في لوحة تحكم EzyCode Connect واستمتع بمراقبة دقيقة لكل رسالة.