HomeLINE OAUpoad รูปภาพ วิดีโอ และไฟล์ เข้า Drive ผ่าน Line OA

Upoad รูปภาพ วิดีโอ และไฟล์ เข้า Drive ผ่าน Line OA

// ติดตั้งไลบรารี LINE BOT SDK: 1KsjhQa6oymhUiACWsAlFTG_XoN8Pnz4px2ekABPjO4tSMX6xRSQMBicy

const ACCESS_TOKEN = "ACCESS_TOKEN";
const USERID = "USERID";
const bot = new LineBotSdk.client(ACCESS_TOKEN);

function doPost(e) { 
  bot.call(e, callback); 
}

function callback(e) {
  if (e.message.type == "text" && e.message.text == "userid") {
    bot.replyMessage(e, [bot.textMessage(e.source.userId)]);
  }

  if (e.message.type == "text" && e.message.text == "groupid") {
    bot.replyMessage(e, [bot.textMessage(e.source.groupId)]);
  }

  if (e.message.type == "image") {
    const resultMsg = SAVEFILE(e);
    bot.replyMessage(e, [bot.textMessage(resultMsg)]);
  }
}

function SAVEFILE(e) {
  try {
    const messageId = e.message.id;
    const url = "https://api-data.line.me/v2/bot/message/" + messageId + "/content";
    const headers = {
      "Authorization": "Bearer " + ACCESS_TOKEN
    };
    
    const FOLDER_ID = "FOLDER_ID;
    
    // ดึงข้อมูลรูปภาพจาก LINE API
    const response = UrlFetchApp.fetch(url, {
      method: "get",
      headers: headers,
      muteHttpExceptions: true
    });
    
    if (response.getResponseCode() !== 200) {
      return "ดาวน์โหลดรูปไม่สำเร็จ (Status: " + response.getResponseCode() + ")";
    }

    // กำหนดชื่อไฟล์และประเภทรูปภาพ
    const blob = response.getBlob().setName("line_img_" + messageId + "_" + new Date().getTime() + ".jpg");
    
    // บันทึกลง Google Drive
    const folder = DriveApp.getFolderById(FOLDER_ID);
    const file = folder.createFile(blob);
    
    return "บันทึกรูปภาพเรียบร้อยแล้ว\nURL: " + file.getUrl();
    
  } catch (err) {
    return "เกิดข้อผิดพลาด: " + err.message;
  }
}

// ฟังก์ชันสำหรับกด Run เพื่อให้สิทธิ์ Google Drive ในโปรเจกต์
function grantPermission() {
  DriveApp.getFolderById("FOLDER_ID");
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
สั่งซื้อหนังสือ

Most Popular

Recent Comments