# LINE Bot 學習手冊

LINE Bot 是掛在 LINE 官方帳號上的自動回覆程式：使用者傳訊息給官方帳號，LINE 平台把訊息轉送到你寫的 webhook 程式，程式決定要回什麼。本站用 **Python（Flask）** 與 **Google Apps Script** 兩種做法，帶你從建立帳號、寫出第一個 Echo Bot，一路做到記帳機器人、圖文選單、LIFF 網頁與 LINE Pay 付款，最後部署上線。

## 核心概念

做 LINE Bot 只需要先掌握四件事，其他功能都是在這四件事上疊加：

- **Channel 與金鑰**

  ---

  在 LINE Developers 建立 Messaging API Channel，取得 **Channel secret**（驗證請求）與 **Channel access token**（呼叫 API）。
- **Webhook**

  ---

  一個公開的 HTTPS 網址。使用者每做一件事（傳訊息、加好友、按按鈕），LINE 都會把「事件」POST 到這裡。
- **Reply 與 Push**

  ---

  收到事件後用 **reply token** 回覆（不算訊息費），或主動用 **Push** 推播給使用者（計入方案則數）。
- **訊息與介面**

  ---

  除了文字，還有貼圖、圖片、Quick Reply、Flex Message、圖文選單與 LIFF，可以做出像 App 一樣的操作介面。

## 學習路線

| 階段 | 內容 | 做出來的東西 |
| --- | --- | --- |
| 1\. 入門基礎 | [運作原理](<https://www.vcdemy.com/linebot/basics/how-it-works/index.md>)、[建立官方帳號與 Channel](<https://www.vcdemy.com/linebot/basics/channel-setup/index.md>) | 拿到 secret 與 token |
| 2a. Python 路線 | [Echo Bot](<https://www.vcdemy.com/linebot/python/echo-bot/index.md>) → [事件與訊息類型](<https://www.vcdemy.com/linebot/python/events/index.md>) → [專題：記帳小幫手](<https://www.vcdemy.com/linebot/python/expense-bot/index.md>) | 有資料庫、有 Flex 統計的記帳機器人 |
| 2b. Apps Script 路線 | [Echo Bot](<https://www.vcdemy.com/linebot/gas/echo-bot/index.md>) → [專題：試算表問答機器人](<https://www.vcdemy.com/linebot/gas/sheets-bot/index.md>) | 免架站、用試算表維護內容的客服機器人 |
| 3\. 介面設計 | [Quick Reply 與 Template](<https://www.vcdemy.com/linebot/ui/quick-reply-template/index.md>)、[Flex Message](<https://www.vcdemy.com/linebot/ui/flex-message/index.md>)、[圖文選單](<https://www.vcdemy.com/linebot/ui/rich-menu/index.md>)、[LIFF](<https://www.vcdemy.com/linebot/ui/liff/index.md>) | 點餐選單、商品卡片、報名表單 |
| 4\. 金流交易 | [金流總覽](<https://www.vcdemy.com/linebot/payment/index.md>)、[LINE Pay 實作](<https://www.vcdemy.com/linebot/payment/linepay/index.md>)、[其他金流](<https://www.vcdemy.com/linebot/payment/other-gateways/index.md>) | 在 Bot 裡下單並用 LINE Pay 付款 |
| 5\. 部署上線 | [託管平台怎麼選](<https://www.vcdemy.com/linebot/deploy/index.md>)、[部署 Python Webhook](<https://www.vcdemy.com/linebot/deploy/python-deploy/index.md>) | 24 小時運作的正式 Bot |

> [!TIP]
>
> **Python 還是 Apps Script？**
>
> 兩條路線可以只選一條。想學完整的後端開發、之後要接資料庫或金流，選 **Python**；不想架伺服器、只要一個好維護的小工具，選 **Apps Script**。兩邊的概念完全相同，只是寫法不同。

## 範例程式

本站所有範例程式都放在 GitHub repo 的 [`examples/`](<https://github.com/vcdemy/linebot/tree/main/examples>) 資料夾，網頁上看到的程式碼就是這些檔案的內容，可以直接下載執行：

| 資料夾 | 說明 |
| --- | --- |
| `examples/python/echo-bot` | 最小可運作的 Echo Bot |
| `examples/python/events-bot` | 接收圖片、貼圖、位置、加好友事件 |
| `examples/python/expense-bot` | 專題：記帳小幫手（SQLite + Flex Message） |
| `examples/python/ui-demo` | Quick Reply、Template、Flex、Postback 示範 |
| `examples/python/rich-menu` | 用 API 建立圖文選單 |
| `examples/python/linepay-shop` | LINE Pay 咖啡小舖 |
| `examples/gas/echo-bot` | Apps Script 版 Echo Bot |
| `examples/gas/sheets-bot` | 專題：試算表問答＋每日提醒 |
| `examples/liff/index.html` | LIFF 活動報名表單 |

## 推薦影音

### 關於 LINE 機器人（使用 ngrok 測試）

簡述：唯客學院的實作示範，從建立 Channel、寫 Flask webhook 到用 ngrok 在本機測試，適合搭配 [Python Echo Bot](<https://www.vcdemy.com/linebot/python/echo-bot/index.md>) 一起看。

> [!NOTE]
>
> **官方文件**
>
> LINE 的官方教學以文件為主：[Messaging API 總覽](<https://developers.line.biz/en/docs/messaging-api/overview/>)、[Get started with the Messaging API](<https://developers.line.biz/en/docs/messaging-api/getting-started/>)，以及 [line-bot-sdk-python](<https://github.com/line/line-bot-sdk-python>)。

## 常見問題

### 做 LINE Bot 要花錢嗎？

學習與小量使用可以完全免費。建立官方帳號、Messaging API 與 reply 回覆都不收費；只有主動推播（push、群發）的訊息會計入官方帳號的每月方案則數，超過免費額度才需要升級方案。託管也有免費選項，例如 Google Apps Script 與 Cloud Run 免費額度。

### 需要會寫程式嗎？

只想做關鍵字自動回覆，用 LINE 官方帳號後台就能設定，不用寫程式。要依資料庫內容回覆、串接其他服務、處理付款，就需要自己寫 webhook，本站從 Python 與 Apps Script 的基礎寫法開始教。

### LINE Bot 可以收款嗎？

可以，但付款不是在聊天室裡完成，而是 Bot 傳一個付款連結，使用者在 LINE Pay 或其他金流的付款頁面付款，完成後再由伺服器通知 Bot。詳見 [金流交易](<https://www.vcdemy.com/linebot/payment/index.md>)。

### Webhook 要放在哪裡？

開發時用 ngrok 把本機程式暫時公開；正式上線建議 Python 版放 Google Cloud Run，小型工具用 Google Apps Script。各平台比較見 [部署與託管](<https://www.vcdemy.com/linebot/deploy/index.md>)。

### 舊教學裡的 `LineBotApi`、`TextSendMessage` 為什麼不能用？

那是 line-bot-sdk 2.x 的寫法，官方已改為只維護 `linebot.v3` 模組。v3 的類別名稱不同，例如 `MessagingApi`、`TextMessage`、`ReplyMessageRequest`，本站範例全部使用 v3。
