Saturday, January 18, 2025
API KEY
HomeGoogle Apps ScriptUpload รูปภาพ วิดีโอ ผ่าน Line Oa ขึ้น Google Drive

Upload รูปภาพ วิดีโอ ผ่าน Line Oa ขึ้น Google Drive

โค้ด Upload รูปภาพ วิดีโอ ผ่าน Line Oa ขึ้น Google Drive ด้วย Google App Script

const LINE_CHANNEL_ACCESS_TOKEN = 'YOUR_LINE_CHANNEL_ACCESS_TOKEN';
const FOLDER_ID = 'YOUR_GOOGLE_DRIVE_FOLDER_ID';

/**
 * DoPost function to handle webhook events
 */
function doPost(e) {
  const json = JSON.parse(e.postData.contents);
  const events = json.events;

  for (let i = 0; i < events.length; i++) {
    const event = events[i];
    const message = event.message;

    if (message.type === 'image' || message.type === 'file') {
      handleFileMessage(event);
    }
  }

  return ContentService.createTextOutput(JSON.stringify({ status: 'success' }));
}

/**
 * Handle image or file message
 */
function handleFileMessage(event) {
  const messageId = event.message.id;
  const url = `https://api-data.line.me/v2/bot/message/${messageId}/content`;
  const headers = {
    'Authorization': 'Bearer ' + LINE_CHANNEL_ACCESS_TOKEN
  };

  const response = UrlFetchApp.fetch(url, { headers: headers, muteHttpExceptions: true });
  const blob = response.getBlob();
  const folder = DriveApp.getFolderById(FOLDER_ID);
  const file = folder.createFile(blob);
  
  // Set file permissions to public
  file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
  
  // Get the URL of the uploaded file
  const fileUrl = file.getUrl();
  
  // Reply to the user with the file URL
  replyToUser(event.replyToken, `File uploaded successfully. You can access it here: ${fileUrl}`);
}

/**
 * Reply to user
 */
function replyToUser(replyToken, message) {
  const url = 'https://api.line.me/v2/bot/message/reply';
  const headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + LINE_CHANNEL_ACCESS_TOKEN
  };
  const payload = {
    replyToken: replyToken,
    messages: [
      {
        type: 'text',
        text: message
      }
    ]
  };

  const options = {
    method: 'post',
    headers: headers,
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  };

  UrlFetchApp.fetch(url, options);
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
คอร์สเรียนออนไลน์

Most Popular

Recent Comments