# 更新 Webhook (PUT)

更新 Webhook 的名称、消息模板、默认级别、操作按钮或活动状态。省略的字段保持当前值不变。

## 端点

```
PUT https://api.pocketalert.app/v1/webhooks/{tid}
```

## 身份验证

> **Info: 必填**
>
> 在请求中包含以下请求头之一：
> - `Token: <your-api-key>` — 从 [API 密钥](https://pocketalert.app/account/apikeys) 获取
> - `Authorization: Bearer <jwt-token>` — 来自 [登录](https://info.pocketalert.app/api/user/login.md)

## 请求

### 路径参数

| 参数 | 类型 | 必填 | 描述 |
|-----------|------|:--------:|-------------|
| `tid` | string | ✅ | Webhook 唯一标识符 |

### 请求头

| 请求头 | 必填 | 描述 |
|--------|:--------:|-------------|
| `Token` | ✅ | 你的 API 密钥 |
| `Authorization` | ✅ | `Bearer <jwt-token>` |

### 请求体参数

| 参数 | 类型 | 必填 | 描述 |
|-----------|------|:--------:|-------------|
| `name` | string | ❌ | 新名称 |
| `url` | string | ❌ | 接收 URL `https://p4a.me/wh/{url}` 的新 slug。旧 URL 将失效。如果 slug 已被占用，返回 `409` |
| `message` | string | ❌ | 新的消息模板 |
| `is_active` | boolean | ❌ | 启用或禁用 Webhook |
| `application_id` | string | ❌ | 新的应用 TID |
| `device_id` | string | ❌ | 新的设备 TID |
| `default_level` | string \| int \| null | ❌ | 当负载中没有 `level` 时，该 Webhook 消息的默认优先级：名称（`silent`…`critical`）或整数（`-2`..`2`）。传入 `null` 将其移除，此时应用的级别生效 |
| `actions` | string | ❌ | 带有 `%field.path%` 占位符的 JSON 操作按钮模板，参见 [创建 Webhook](https://info.pocketalert.app/api/webhooks/create.md#action-buttons-template)。传入 `""` 将移除按钮 |

> **Warning: 禁用 Webhook**
>
> `is_active: false` 的 Webhook 仍会接收传入请求并返回 `200`，但不会创建任何提醒。这些调用会记录在 [Webhook 历史](https://info.pocketalert.app/api/webhooks/history.md) 中，`error_code: inactive`。

### 请求示例

**cURL**

```bash
curl -X PUT "https://api.pocketalert.app/v1/webhooks/kblf7mh9j8jjyeh4fx" \
  -H "Token: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "GitHub Push (Inactive)",
    "is_active": false
  }'
```

**JavaScript**

```javascript
const tid = 'kblf7mh9j8jjyeh4fx';
const response = await fetch(`https://api.pocketalert.app/v1/webhooks/${tid}`, {
  method: 'PUT',
  headers: {
    'Token': 'your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'GitHub Push (Inactive)',
    is_active: false
  })
});
```

**Python**

```python
import requests

tid = 'kblf7mh9j8jjyeh4fx'
response = requests.put(
    f'https://api.pocketalert.app/v1/webhooks/{tid}',
    headers={'Token': 'your-api-key'},
    json={'name': 'GitHub Push (Inactive)', 'is_active': False}
)
print(response.json())
```

**PHP**

```php
$tid = 'kblf7mh9j8jjyeh4fx';
$response = Http::withHeaders([
    'Token' => 'your-api-key',
])->put('https://api.pocketalert.app/v1/webhooks/'.$tid, [
    'name' => 'GitHub Push (Inactive)',
    'is_active' => false,
]);

return $response->json();
```

## 响应

### 成功响应

> **Tip: 200 OK**
>
> Webhook 更新成功

```json
{
  "data": "success"
}
```

### 错误响应

| 状态码 | 描述 |
|:------:|-------------|
| `400` | 请求错误 — `default_level` 或 `actions` 模板无效，或 `name` 或 `message` 为空 |
| `401` | 未授权 — 令牌无效或缺失 |
| `404` | 未找到 — Webhook、应用或设备不存在 |
| `409` | 冲突 — `url` slug 已被占用 |
