LINE 設定
最後更新
本頁說明如何建立
core/line bot、對外公開它的 webhook,以及 handler 收到的 Input。建構子與選項
- bot, err := line.New(channelSecret, channelAccessToken, "8080",
- line.WithPath("/hooks/line"),
- )
| 參數 | 規則 |
|---|---|
secret | Channel secret,用於驗證 webhook 簽章。空值 → secret is required |
token | Channel access token。空值 → token is required |
port | 監聽 port;server 綁定所有介面的 :<port>。空值 → port is required |
WithPath(path) | Webhook path;預設 /linebot/webhook。path 為空時沿用預設 |
New 以 line-bot-sdk-go/v8 的 legacy linebot 套件建立 client,不會啟動 server。公開 HTTPS endpoint
與 Telegram、Discord 不同,LINE 由平台把事件推給你。請在 LINE Developers Console 把 webhook URL 設為公開的 HTTPS 位址,並轉發到
http://<host>:<port><path>。本機測試可使用 ngrok 等通道。Start(ctx) 先呼叫 GetBotInfo 驗證 token,再同步綁定 port(port 已被占用時回 error),最後在 goroutine 中提供服務。Webhook 處理
| 情況 | HTTP 回應 |
|---|---|
簽章驗證失敗(ErrInvalidSignature) | 400 |
| body 無法解析 | 500 |
| bot 未執行 | 503 |
| 事件處理完畢 | 200,待該 request 內所有事件處理完才回 |
同一 request 內的事件依序處理,共用一個 30 秒 context。LINE 期待快速收到 200,因此慢的工作請移出 handler,或改在 goroutine 執行後再用
Send 推送。Input
| 欄位 | 內容 |
|---|---|
SourceType | user、group 或 room |
UserID / GroupID / RoomID | 來源 ID;依 SourceType 填入 |
Username | 傳送者顯示名稱,依來源呼叫 GetProfile、GetGroupMemberProfile 或 GetRoomMemberProfile 取得;呼叫失敗或 UserID 為空時為 "" |
ReplyToken | 一次性 reply token;handler 回傳非空字串時會被自動回覆用掉 |
MessageID | 訊息 ID(所有類型);媒體訊息傳給 Save |
MessageType | text、image、video、audio 或 file |
Text | 僅 text 訊息 |
FileName | 僅 file 訊息 |
Raw | *linebot.Event |
LINE 事件不帶顯示名稱,因此每則訊息在呼叫 handler 前會多打一次 profile API。