將這張家扶娃娃圖片放置 Google 雲端硬碟作為發送至 Line Notify 的操作圖片。
雲端硬碟。
輸入以下 Script 程式碼並執行。
function snedImagetoLineNotify() {
var image = "2018家扶娃娃.jpg";
var imagename = "2018家扶娃娃";
var LINE_ACCESS_TOKEN = "你的Token";
//指定圖片名稱
var image = DriveApp.getFilesByName(image).next();
//取得圖片ID
var id = image.getId();
//取得圖片所有資訊
var blob = DriveApp.getFileById(id).getBlob();
//間格字串
var boundary = "iInfo";
//組合傳送內容
var requestBody = Utilities.newBlob(
"--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"message\"; \r\n\r\n" + imagename + "\r\n"
+ "--" + boundary + "\r\n"
+ "Content-Disposition: form-data; name=\"imageFile\"; filename=\"" + blob.getName() + "\"\r\n"
+ "Content-Type: " + "image/png" +"\r\n\r\n").getBytes();
requestBody = requestBody.concat(blob.getBytes());
requestBody = requestBody.concat(Utilities.newBlob("\r\n--" + boundary + "--\r\n").getBytes());
//發送至Line Notify
var options = {
method: "post",
contentType: "multipart/form-data; boundary=" + boundary,
payload: requestBody,
headers : {"Authorization" : "Bearer " + LINE_ACCESS_TOKEN},
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch("https://notify-api.line.me/api/notify", options);
Logger.log(response.getContentText());
}
執行結果:
參考資料:
- Google Apps Script で multipart/form-data の HTTP リクエストを送信する
- Multipart-POST Request Using Google Apps Script
- Update Twitter status with an image with a single request in Apps Script
- How to Send SMS from a Google Spreadsheet
- GAS – Three Crackers: 1. Insert image at specific place in a table, 2. Remove extra paragraphs created by 1, 3. Proportionally size your image


