# صيغة الويب هوك

Source: https://instatus.com/help/ar/webhooks

فيما يلي صيغة رسائل الويب هوك التي ستصلك إذا اشتركت عبر الويب هوك:

**عند إضافة حادثة أو تحديثها:**

```json
{
  "meta": {
    "unsubscribe": "",
    "documentation": ""
  },
  "page": {
    "id": "",
    "status_indicator": "",
    "status_description": "",
    "url": ""
  },
  "incident": {
    "backfilled": false,
    "created_at": "",
    "impact": "",
    "name": "",
    "resolved_at": "",
    "status": "",
    "updated_at": "",
    "id": "",
    "url": "",
    "incident_updates": [
      {
        "id": "",
        "incident_id": "",
        "body": "",
        "status": "",
        "created_at": "",
        "updated_at": ""
      }
    ]
  }
}
```

**عند إضافة صيانة أو تحديثها:**

```json
{
  "meta": {
    "unsubscribe": "",
    "documentation": ""
  },
  "page": {
    "id": "",
    "status_indicator": "",
    "status_description": "",
    "url": ""
  },
  "maintenance": {
    "backfilled": false,
    "created_at": "",
    "impact": "",
    "name": "",
    "resolved_at": "",
    "status": "",
    "updated_at": "",
    "id": "",
    "url": "",
    "duration": "",
    "maintenance_updates": [
      {
        "id": "",
        "maintenance_id": "",
        "body": "",
        "status": "",
        "created_at": "",
        "updated_at": ""
      }
    ]
  }
}
```

**عند تحديث مكوّن:**

```json
{
  "meta": {
    "unsubscribe": "https://<status-page-domain>/unsubscribe?id=${subscriber.id}&token=${subscriber.unsubscribeToken}",
    "documentation": ""
  },
  "page": {
    "id": "",
    "status_indicator": "",
    "status_description": "",
    "url": ""
  },
  "component_update": {
    "created_at": "",
    "new_status": "",
    "component_id": ""
  },
  "component": {
    "created_at": "",
    "id": "",
    "name": "",
    "status": ""
  }
}
```

### حالات صفحة الحالة الممكنة:

- `UP`
- `HASISSUES`
- `UNDERMAINTENANCE`

### حالات المكوّن الممكنة:

- `OPERATIONAL`
- `UNDERMAINTENANCE`
- `DEGRADEDPERFORMANCE`
- `PARTIALOUTAGE`
- `MAJOROUTAGE`

### حالات الحادثة الممكنة:

- `INVESTIGATING`
- `IDENTIFIED`
- `MONITORING`
- `RESOLVED`

### حالات الصيانة الممكنة:

- `NOTSTARTEDYET`
- `INPROGRESS`
- `COMPLETED`

## التحقق من حمولة الويب هوك

**يُنصح بشدة بالتحقق من حمولة الويب هوك في نقطة نهاية الويب هوك.**

نوقّع حمولات الويب هوك بمفتاح سري ونُرفق التوقيع في ترويسة باسم `x-instatus-webhook-signature`. يتيح لك هذا التوقيع التأكد من أن الويب هوك قادم من Instatus.

### خطوات التحقق من الويب هوك

١. احفظ **المفتاح السري للويب هوك** الذي أنشأته أثناء الاشتراك في الويب هوك (ستجده عند الاشتراك عبر ويب هوك في صفحة ما، ويمكنك أيضًا تخصيصه هناك).

![المفتاح السري للويب هوك](https://instatus.com/help/notification/webhook-subscribe-example.png)

٢. أنشئ نقطة نهاية جديدة على خادمك لاستقبال الويب هوك.

٣. قارن التوقيع بالتوقيع الذي يولّده خادمك للتحقق.

٤. إذا تطابق التوقيعان، عالج الويب هوك.

#### مثال برمجي

```javascript
import crypto from 'crypto'
import express from 'express'

const app = express()
app.use(express.json())

const WEBHOOK_SECRET = 'your-webhook-secret'

function isVerifiedPayload(payload, signature, secret) {
  const hmac = crypto.createHmac('sha256', secret)
  const digest = hmac.update(JSON.stringify(payload)).digest('hex')
  return crypto.timingSafeEqual(Buffer.from(digest), Buffer.from(signature))
}

app.post('/endpoint/to/webhook', (req, res) => {
  const payload = req.body
  const signature = req.header('x-instatus-webhook-signature')

  if (!signature) {
    return res.status(400).send('Signature missing')
  }

  if (!isVerifiedPayload(payload, signature, WEBHOOK_SECRET)) {
    return res.status(401).send('Invalid signature')
  }

  // Process the valid webhook
  res.status(200).send('Webhook received')
})

app.listen(3000, () => console.log('Server running on port 3000'))
```

### لماذا يهم التحقق

- **يضمن الأصالة**: يؤكد أن الويب هوك صادر عن منصتنا.
- **يمنع التلاعب**: يكشف أي تغيير في الحمولة.
- **يعزّز الأمان**: يحمي من هجمات إعادة الإرسال والوصول غير المصرّح به.
