# 提問端點

> 建立問題、用長輪詢等答案、取消問題，以及接收帶簽章的回呼。

這些端點把問題推到裝置上，再把答案交回來。想從頭了解用法和 MCP 工具，見[提問並等待回答](/zh-Hant/docs/agents/ask)。

| 方法 | 路徑 | 做什麼 |
|---|---|---|
| `POST` | `/ask` | 建立問題並推播 |
| `GET` | `/ask/{question_id}` | 讀取問題，可以等答案 |
| `DELETE` | `/ask/{question_id}` | 取消問題 |

## POST /ask [#post-ask]

建立問題，以時效性通知推到裝置上。收 JSON、表單或 query 參數，query 參數優先於內文。算一則推播。

<TypeTable
  type={{
    device_key: { type: "string", description: "要問哪台裝置。", required: true },
    title: { type: "string", description: "問題本身，顯示成通知標題，最多 200 字。", required: true },
    body: { type: "string", description: "問題下面的補充說明，最多 2,000 字。" },
    options: { type: "string[]", description: "最多 4 個選項，每個最多 30 字，不能重複。表單或 query 用逗號分隔的字串。" },
    allow_text: { type: "boolean", description: "允許打字回答。options 和 allow_text 至少要有一個。" },
    placeholder: { type: "string", description: "文字框的提示，最多 60 字。" },
    timeout: { type: "integer", description: "幾秒後過期，60 到 604800。", default: "3600" },
    callback_url: { type: "string", description: "http 或 https 網址。回答後 Opush 會 POST 一次到這裡。" },
    group: { type: "string", description: "問題在收件匣的分組。" },
  }}
/>

```bash
curl https://opu.sh/ask \
  -H "Content-Type: application/json" \
  -d '{"device_key":"YOUR_KEY","title":"要合併 PR #42 嗎？","options":["合併","關閉"],"callback_url":"https://example.com/opush"}'
```

### 回應：201 Created [#回應201-created]

```json
{
  "question_id": "q_aLvBJaVvMvbingXOp9eapf",
  "answer_url": "https://opu.sh/ask/q_aLvBJaVvMvbingXOp9eapf",
  "expires_at": "2026-11-04T10:30:00Z",
  "callback_secret": "ocs_…"
}
```

| 欄位 | 說明 |
|---|---|
| `question_id` | `q_` 加 22 個字元，同時也是讀取答案的密鑰 |
| `answer_url` | 用來 `GET` 或 `DELETE` 這個問題的網址 |
| `expires_at` | 問題過期的時間 |
| `callback_secret` | 帶了 `callback_url` 才有，用來驗證回呼的簽章 |

## GET /ask/\{question_id\} [#get-askquestion_id]

回傳問題。帶 `wait` 時，請求會一直等到狀態不是 `pending`，或時間到了才回應。

| Query | 說明 |
|---|---|
| `wait` | 要等幾秒，0 到 50。預設 0（馬上回應）。 |

### 回應：200 OK [#回應200-ok]

```json
{
  "question_id": "q_aLvBJaVvMvbingXOp9eapf",
  "status": "answered",
  "title": "要合併 PR #42 嗎？",
  "body": null,
  "options": ["合併", "關閉"],
  "allow_text": false,
  "created_at": "2026-11-04T09:30:00Z",
  "expires_at": "2026-11-04T10:30:00Z",
  "answer": { "option": "合併", "option_index": 0, "text": null, "answered_at": "2026-11-04T09:30:41Z" }
}
```

| 欄位 | 說明 |
|---|---|
| `status` | `pending`、`answered`、`expired` 或 `cancelled` |
| `answer` | 回答之前是 `null`。回答後有 `option` 和 `option_index`（只打字回答時兩個都是 `null`）、`text`（沒有就是 `null`）和 `answered_at` |

只收第一個答案。問題在建立 30 天後刪除（要已經結束），之後回 `404`。

## DELETE /ask/\{question_id\} [#delete-askquestion_id]

取消還在等待的問題，並收掉手機上的通知。回傳的格式和 `GET` 相同。問題已經結束的話不會有變化，回傳目前的狀態。

## 回呼 [#回呼]

問題被回答後，Opush 會對 `callback_url` 送一次 `POST`：

```http
POST /opush HTTP/1.1
Content-Type: application/json
User-Agent: Opush/1.0
X-Opush-Signature: sha256=5c1f…

{"question_id":"q_…","status":"answered","title":"要合併 PR #42 嗎？", …}
```

- 內文和 `GET /ask/{question_id}` 的 JSON 相同。
- `X-Opush-Signature` 是 `sha256=` 加上原始內文的 HMAC-SHA256（十六進位），金鑰是 `callback_secret`。比對時請用固定時間的比較。範例見[提問並等待回答](/zh-Hant/docs/agents/ask)。
- 只送一次，逾時 10 秒，不會重送。漏接的話用 `GET /ask/{question_id}` 輪詢。
- 問題過期或取消時不送回呼。

## 錯誤 [#錯誤]

```json
{ "error": { "code": "invalid_request", "message": "at most 4 options" } }
```

| 狀態碼 | code | 什麼時候 |
|---|---|---|
| `400` | `invalid_request` | 少了 `device_key` 或 `title`、`options` 不對、沒有 `options` 也沒有 `allow_text`、`timeout` 超出範圍、`callback_url` 不對，或內文不是 JSON 物件 |
| `401` | | 企業伺服器開了 Basic Auth 卻沒帶帳號密碼（推播回應格式） |
| `404` | `not_found` | 裝置 key 或問題不存在 |
| `429` | `rate_limited` | 推播太頻繁，等 `Retry-After` |
| `503` | `device_unreachable` | 裝置連不上（在那台裝置打開 Opush） |
| `503` | `upstream_error` | 推播失敗，或伺服器忙碌 |
