星期二, 8月 26, 2025

[GAS] LineBot 傳送訊息

在 Google Apps Script 內透過 LineBot 傳送訊息和圖片給特定使用者。

.gs Code

圖檔必須為 JPEG or PNG 格式且檔案大小限制
  • originalContentUrl:10MB
  • previewImageUrl:1MB
詳見官方網站說明 Image Message
function send()
{
  LineBot_SendText("Hello From Google Apps Script");
  LineBot_SendImage("https://live.staticflickr.com/123/456.jpg");
}

function LineBot_SendText(textMessage)
{
  let message = [{ type: "text", text: textMessage }];
  LineBot_Send(message);
}

function LineBot_SendImage(fileURL)
{
  let message = [{ 
      type: "image", 
      originalContentUrl: fileURL,
      previewImageUrl: fileURL
      }];

  LineBot_Send(message);
}

function LineBot_Send(message) {

  // 從 PropertiesService 取出 LineChannelAccessToken 和 UserID
  let scriptProperties = PropertiesService.getScriptProperties();
  let properties = scriptProperties.getProperties();
  let lineBotChannelAccessToken = properties["LineChannelAccessToken"];
  let userID = properties["UserID"];
  
  let payload = {
    to: userID,
    messages: message
  };

  let options = {
    headers: { Authorization: "Bearer " + lineBotChannelAccessToken },
    contentType: "application/json; charset=UTF-8",
    method: "post",
    payload: JSON.stringify(payload)
  };

  let lineUrl = "https://api.line.me/v2/bot/message/push";
  UrlFetchApp.fetch(lineUrl, options);  
}

實際執行
訊息費用

免費仔首選,輕用量方案
  • 免月費,每月 200 則訊息則數
  • 訊息則數僅計算主動發送,自動回覆或一對一聊天等互動不列入計算
  • 群組內假如有 5 位,主動發出 1 則訊息至群組,實際算是耗掉 5 則訊息

服務配額

Google 帳號與 Google Apps Sctipt 雖然免費,但其相關服務是有其使用限制,使用 LineBot 來互動時要特別注意,把相關服務限制列出來

功能 個人帳戶 (gmail.com) Google Workspace
觸發條件總執行階段
(Triggers total runtime)
90 分鐘 / 天 6 小時 / 天
網址擷取呼叫次數
(URL Fetch Calls)
20,000 次 / 天 100,000 次 / 天
指令碼執行階段
(Script runtime)
6 分鐘 / 次 6 分鐘 / 次

完整功能限制,請參考官方文章 - Quotas for Google Services

沒有留言:

張貼留言