使用 LINE Bot 聊天機器人替代 LINE Notify 服務
LINE Notify 服務在 2025 年 3 月底停止服務,由於 LINE 是台灣地區每個人都在使用的服務,在考量不需要額外安裝 App 的情況下,能夠沿用 LINE 設計類似的訊息通知服務是最好的解法。我們將帶大家改用 LINE 聊天機器人作為替代方案。
基本概念
要建立 LINE 聊天機器人,必須建立 LINE 官方帳號,也就是代表該機器人的帳號。有了官方帳號後,就可以設計後端程式透過 LINE Messaging API 被動回應使用者訊息或是主動發送推播訊息:
- 被動回覆訊息:這是由使用者發送訊息給官方帳號,再由後端程式接收後以官方帳號身分回覆給使用者。我們必須建立能讓官方帳號與後端程式互傳資料的通道(channel),才能讓訊息在兩者之間往返。
- 主動發送推播訊息:由我們的程式以 LINE 官方帳號身分直接傳送訊息給使用者或群組,這需要透過使用者的 LINE User Id 或是群組的 LINE group Id(這跟加聯絡人時看到的 LINE Id 是不一樣的東西)指定傳傳送對象。
雖然我們只需要主動發送推播訊息就可以達成 LINE Notify 的單向通知功能,不過因為需要先取得 LINE user/group Id,這項資訊只能透過接收使用者送來的訊息得知,所以我們幫大家設計好一個後端程式,不論收到什麼訊息,都只會回覆使用者的 LINE user/group Id。
流程
實際上的流程就如以下的循序圖所示:
以下就依照上述流程完成。
建立通道與 LINE 官方帳號
首先就來建立讓 LINE 官方帳號與後端程式互傳資料的通道,建立的過程中就會連帶建立官方帳號,也就是代表聊天機器人的帳號。請依照以下步驟進行:
-
請先連至 LINE 開發者網站 - https://developers.line.biz/zh-hant/。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
按網頁右上角的 Log in,會出現如下的畫面:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
你可以使用原本的 LINE 帳號登入,或者是按底下的建立帳號建立獨立的商用帳號,請自行選擇後登入。
-
首次登入 LINE 開發者網頁會要求填入基本資料,這裡的資料和你的 LINE 帳號是分開的,可以自由填寫:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
請填入後勾選同意授權條款,再按 Create my accout 建立帳號。
-
登入 LINE 開發者網站後第一步就是要建立 provider,你可以把商用帳號視為一個組織,組織下可能分為不同部門或開發者,而 provider 就代表單一部門或是某位開發者。請往下捲動:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
按一下 Create a new provider 建立:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
請鍵入自訂的名稱後按 Create 完成。
-
建立好 provider 後會要求你建立 Messaging API channel,這就是建立可用於 Messaging API 讓官方帳號與後端程式互傳資料的通道,請在剛剛建立好的 provider 總覽頁面中按 Create a Messaging API channel:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
建立 Messaging API Channel 的第一步就是建立 LINE 官方帳號:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
這個步驟要在官方帳號專屬的頁面進行,請按 Create a LINE Official Account** 開啟頁面:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
按登入後繼續:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
首次建立 LINE 官方帳號會需要手機簡訊認證,請按進行簡訊認證:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
請輸入電話號碼後按傳送簡訊,這裡的電話號碼不需要是你的 LINE 帳號綁定的電話號碼,只要是可以接收簡訊即可:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
收到簡訊後輸入認證碼按認證完成:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
再按返回服務即可。
接著就要設定 LINE 官方帳號的資訊,其中公司名稱可以不填,業種請自行選取,或是像我一樣都先選其它:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
確認無誤後按確定:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
再按完成即可。
-
最後要啟用剛剛所建立官方帳號的 Messaging API 功能,這樣才能讓這個官方帳號與稍後要建立的後端程式互動:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
由於我們只是要利用這個官方帳號傳送訊息的功能,並沒有要用來推廣業務,可以先略過認證帳號的步驟,請直接按稍後進行認證:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
再按登入:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
因為不同意也無法繼續,請按同意。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
如果有看到上面提醒強制雙重認證的畫面,請按關閉。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
請按下一步略過加入將剛剛建立的官方帳號加入你的 LINE 好友的步驟,稍後會手動完成:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
按前往主頁進行設定:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
如果想幫你的官方帳號建立頭像,可以按一下左上方的頭像:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
你可以把設計好的頭像圖檔拖放進來,沒有設定頭像也沒有關係,並不影響功能:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
按儲存關閉此交談窗。切換到主頁就會看到剛剛上傳的頭像:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
請按官方帳號的名稱進入設定頁面:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
按左側邊欄的 Messaging API 後再按一下右邊的啟用 Messaging API,這樣才能讓官方帳號與後端程式連通:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
選取之前建立的 provider 後按同意:

隱私權欄位與服務條款都可以之後再補,按確定:

再按確定完成。
-
完成後回到 LINE 開發者頁面進一步設定剛剛建立好的 Messaging API channel:

按左側之前建立的 provider,就會在右側看到剛剛建立的官方帳號,請按該官方帳號進入設定頁面:

切換到 Messaging API 頁籤,可以看到這個官方帳號的 LINE ID,請利用這個 ID 將它加入你的 LINE 好友,或者也可以用手機掃底下的 QR code:

加入後會看到如下的歡迎畫面:

回到 LINE 開發者網頁往下捲找到 LINE Official Account features 區段:

按一下 Auto-reply messages 右側的 Edit,會開啟新的設定頁面:

請關閉自動回應功能,否則每次收到使用者的訊息時 LINE 都會自動幫我們回應不必要的罐頭訊息。完成後請關閉此設定頁面,回到 LINE 開發者頁面:

再按一下 Allow bot to join group chats 右側的 Edit,同樣會開啟新的設定頁面:

開啟加入群組的設定,否則官方帳號無法加入群組。設定後關閉頁面回到 LINE 開發者頁面,重新整理頁面:

確認剛剛的設定都生效後往下捲找到 Channel access token 區段:

按Issue產生存取令牌:

這個存取令牌是稍後要建立的後端程式使用 Messaging API 發送訊息的通行證,請先把頁面留在這裡不要關閉。
這樣就建立好可以作為 LINE 聊天機器人的官方帳號了,下一步就是要建立可以和 LINE 聊天機器人連動的後端程式。
使用 Google Apps Script 建立 LINE Bot 後端程式
後端程式基本上就是一個網站,每次官方帳號收到使用者送來的訊息時,就會連往後端程式,把訊息轉送過去。當後端程式需要回覆訊息時,就必須以剛剛看到的存取令牌作為通行許可證明,將訊息送到 LINE 的伺服器,由 LINE 轉送訊息給使用者。
由於後端程式必須是一個公開在網路上的網站,為了省去自行架設網站的麻煩,我們採用 Goolge 的 Apps Script 服務快速架設網站,請跟著以下步驟使用我們設計好的範本建立:
-
開啟我們預先準備好的 Google Apps Script 專案 - https://flagtech.pse.is/6sveez 範本:

請登入你的 Google 帳號:

按左側的總覽:

按右側的複製將此專案複製到你自己的 Google 帳號下:

按專案名稱處可以更改名稱。
-
將存取令牌儲存到專案中,讓後端程式可以在執行時取得存取令牌:

請按左側的專案設定:

往下捲找到指令碼屬性區段後按新增指令碼屬性:

在屬性欄位填入名稱 "line_channel_access_token",值欄位填入剛剛在 LINE 開發者網頁生成的存取令牌後按儲存指令碼屬性完成。
後端程式會在需要的時候從這裡讀取存取令牌,如此可以避免將存取令牌揭露在程式碼中。指令碼屬性是跟著帳戶,所以即使享專案給別人時,也不會把指令碼屬性一併分享,非常安全。
-
將設定好的後端程式部署到網路上:

請到網頁上方按右側的部署後選取新增部署作業:

確認無誤後按部署:

由於剛剛的部署設定會讓任何人都可以以你的身分執行這個專案,所以會要求你同意授權,請按授與存取權:

選取剛剛專案的同一個帳戶:

由於這是未經 Google 驗證的專案,所以會要求你確認同意授權,請按 Advanced:

再按 Go to …(unsafe) 完成授權:

最後按 Allow 許可授權即可:

你會看到部署到網路上的公開網址,請複製此網址。
設定官方帳號連動後端程式
到這裡我們已經建立好官方帳號與後端程式,最後就是要把兩者連動:
-
回到 LINE 開發者頁面,從剛剛的 Channel access token 區段往上捲,找到 Webhook settings 區段:

按一下 Edit

填入剛剛複製的後端程式網址後按 Update:

最後記得啟用 Use webhook。
測試
現在我們已經建立好 LINE 官方帳號,也設定好後端程式,接著就來測試看看囉。
查詢 LINE user id
要能讓 LINE 聊天機器人替代 LINE Notify 功能,首先得查詢你的 LINE user id。請到剛剛加入官方帳號成好友後的聊天室,隨便輸入任何訊息:

就可以看到後端程式回覆的 LINE user id 了。如果你把這個帳號加入群組,那麼群組中任何人發送訊息,後端程式都會回覆該群組的 LINE group id。
請特別留意,同一個人或是群組的 id 並不是固定的,如果聊天機器人所屬的 provider 不同,即使是同一個人或是群組,id 就會不一樣。因此,透過 provider A 下的聊天機器人查到的 id 在另一個 provider B 的聊天機器人中就會變成不存在的使用者,必須重新以 provider B 下的聊天機器人查詢才行。
使用瀏覽器測試發送通知訊息
剛剛設計的後端程式也可以接受特定規格的 HTTP GET 請求,幫我們發送推播訊息,對於無法直接使用 LINE Messaging API 的環境,一樣可以發送通知訊息。
請先在瀏覽器開啟新的頁面,然後如下格式輸入網址:
其中 {你在LINE中看到的id}
請換成你要發送對象或是群組的 LINEuser/group id,{訊息內容}
請換成你要傳送的實際訊息內容,最後按下 Enter 即可。以下是訊息內容為 "通知來了" 的測試,我輸入的網址如下:
你要換成你自己的後端程式網址,測試結果如下:

看到頁面上顯示 "OK Push message sent." 就表示成功了,這時你會在 LINE 上看到訊息:

發送通知給群組
如果把這個官方帳號加入某個群組,例如:

就可以查到該群組的 id,這和剛剛看到的 LINE user id 不同:

只要把剛剛程式碼中的 id 換成群組的 id,就可以發送通知到群組讓所有人知道了:

群組中的任何人發訊息,都可以看到聊天機器人回覆的群組 id:

發送貼圖
除了傳送簡單的文字訊息外,也可以發送貼圖,可用的貼圖可以在這裡查看,以下就以這組貼圖為例:

只要在網址中加上 pid 與 sid 個別指定貼圖的套件編號(package ID)和貼圖編號即可,格式如下:
以下是實際測試的網址:
就可以看到送出貼圖了:

送出圖片
你也可以在訊息中送出圖片,只要是公開網址的圖片都可以利用 img 指定,格式如下:
以下是實際測試的網址:
就可以看到訊息中包含文字、貼圖以及圖片:

限制
要注意的是,Messaging API 有其限制,詳細可參考這裡,對於單純發送通知訊息來說,主要的限制就是免費帳號每個月只能發送 200 則訊息,你可以在 LINE 開發者網頁個別 Messaging API Channel 的頁面中查詢用量:

切換到 Statictis 頁次就可以看簡易的分析,如果想看細部的統計資訊,可以按 LINE Official Account MAnage 到官方帳號網頁查看。
結語
利用本文教導的方式,你就可以建立一個 LINE 聊天機器人來取代原本的 LINE Notify 功能。