# Reply Handler

本頁說明 `ReplyHandler` 何時執行、回傳值送往何處，以及 panic 如何處理。

## 契約

```go
type ReplyHandler func(ctx context.Context, input Input) string
```

每個平台套件都以自己的 `Input` 宣告此型別。`Reply(handler)` 只保存一個 handler，再次呼叫會取代前一個。收到事件的 SDK goroutine 會呼叫 handler 並等待結果，因此 handler 執行太久會延後該事件的回覆。

| 回傳值 | 結果 |
|---|---|
| 非空字串 | 以純文字回覆送出（目標見下表） |
| `""` | 不送任何訊息；handler 自行送媒體、互動提示或多則訊息時使用 |

## 觸發 handler 的事件

| 平台 | 事件 |
|---|---|
| Telegram | 所有 message update（文字、caption、圖片、文件；含群組，不檢查 mention）；`SendSelect` 按鈕點擊；`SendMultiSelect` 的 **Done** 點擊 |
| Discord | bot 自己以外的所有 `MessageCreate`；`SendInput` 的 modal 送出；`SendSelect`／`SendMultiSelect` 的選取 |
| LINE | text、image、video、audio、file 五種 message event（含 group 與 room，不檢查 mention） |

其他事件類型一律略過。

## 回覆送往何處

| 平台 | 觸發 | 回覆目標 |
|---|---|---|
| Telegram | 訊息 | 同一個 chat，reply 到使用者訊息 |
| Telegram | 單選／多選 | 同一個 chat，reply 到 prompt 訊息 |
| Discord | 訊息 | 同一個 channel，reply 到使用者訊息 |
| Discord | modal 送出／選取 | reply 到傳給 `SendInput`／`SendSelect` 的 `replyTo`；`replyTo` 為 `""` 時改 reply 到 prompt 訊息 |
| LINE | 任何 | 以事件的一次性 reply token 呼叫 `ReplyMessage` |

## Handler 的 context

| 平台 | 傳給 handler 的 `ctx` |
|---|---|
| Telegram | polling 迴圈提供的 context（由 `Start` 的 context 衍生） |
| Discord | `Start` 建立的 bot 內部 context；未執行時收到的事件直接丟棄 |
| LINE | 每個 webhook request 一個：bot context 加 30 秒 timeout，同一 request 內的所有事件共用 |

## Panic

handler 內的 panic 會被 recover 並以 `slog.Warn("reply handler panic")` 記錄；SDK goroutine 持續運作。

| 平台 | panic 值為 `string` | 其他 panic 值 |
|---|---|---|
| Telegram | 該字串作為回覆送出 | 不送訊息 |
| LINE | 該字串作為回覆送出 | 不送訊息 |
| Discord | 不送訊息 | 不送訊息 |

## 相關頁面

- [Bot 生命週期](/zh/core-concepts)
- [Telegram 互動](/zh/telegram-interactions)、[Discord 互動](/zh/discord-interactions)
- [LINE 訊息](/zh/line-messages)
