งานบริการกลาง

Folder Monitor with Notification

ระบบสำรวจไฟล์และแจ้งเตือนอัตโนมัติผ่าน Google Chat

กองบริหารงานกลางได้พัฒนาระบบสำรวจไฟล์และแจ้งเตือนอัตโนมัติผ่าน Google Chat เพื่อเพิ่มประสิทธิภาพในกระบวนงาน Digital Workflow ขององค์กร ระบบนี้ถูกพัฒนาด้วยเทคโนโลยี Google AppScript ซึ่งทำหน้าที่ตรวจสอบและติดตามไฟล์ใหม่ที่ถูกวางลงในโฟลเดอร์ที่กำหนดโดยอัตโนมัติ

คุณสมบัติหลักของระบบ

เมื่อมีเอกสารหรือไฟล์ใหม่ถูกอัปโหลดเข้าสู่ระบบ ระบบจะดำเนินการตรวจจับและส่งการแจ้งเตือนแบบทันทีผ่าน Google Chat ไปยังผู้ที่เกี่ยวข้อง ทำให้เอกสารได้รับการพิจารณาและดำเนินการอย่างรวดเร็ว ลดความล่าช้าในกระบวนการทำงาน และเพิ่มความโปร่งใสในการติดตามสถานะเอกสาร

ระบบนี้เป็นส่วนสำคัญในการขับเคลื่อนองค์กรสู่การทำงานแบบดิจิทัลอย่างเต็มรูปแบบ สอดคล้องกับนโยบายการพัฒนาประสิทธิภาพการทำงานและลดการใช้กระดาษภายในหน่วยงาน

code.gs
JavaScript
function checkFolders() {
  //var sheet = SpreadsheetApp.getActiveSheet();
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Settings");
  var settingsData = sheet.getDataRange().getValues();
  settingsData.forEach(
    function(row, index) {
      if (index > 1 && row[1] !== '' && row[2] !== '') { 
        folderMonitor(row[0],row[1],row[2],row[3],row[4],row[5],index);
      }
    }
  );
}

function folderMonitor(oldCount, Description, folderID, GoogleChatWebhook, LineNotiToken,emailAddress, index) {
  //Get the folder to be monitored
  var sheet = SpreadsheetApp.getActiveSheet();
  var theFolder = DriveApp.getFolderById(folderID);
  var files = theFolder.getFiles();
  var newCount = 0;
  var file;
  var listFiles = '';
  var listFilesEmail = '';
  
  //Count the number of files in "theFolder"
  while (files.hasNext()) {
    newCount++;
    file = files.next();
    listFiles = listFiles + '>>>  ' + file.getName() + '\n';
    listFilesEmail = listFilesEmail + '<li><a href=' + file.getUrl() + '>' + file.getName() + '</a>';
    //Logger.log(file.getName());
  };
  
  //Create notification message with a link to the monitored folder 
    var folderURL = 'https://drive.google.com/drive/folders/' + folderID;
    var lastCheck = Utilities.formatDate(new Date(), "GMT+7", "dd-MMM-YYYY HH:mm:ss");
    var chatMessage = lastCheck
    + ' : ' + Description 
    + ' (เพิ่มขึ้นจำนวน ' + (newCount-oldCount) + ' ฉบับ) \n'
    + 'ขณะนี้มีไฟล์รอลงนามจำนวน ' + newCount + ' ไฟล์ คือ \n'
    + listFiles 
    + '\n ที่: ' + folderURL;

  // Create email message
    var emailFromName = 'DMS Notification (Do Not Reply)';
    var emailSubject = Description + ' (เพิ่มขึ้นจำนวน ' + (newCount-oldCount) + ' ฉบับ)';
    var emailBody = '<strong>' + lastCheck + '</strong><br>'
        + 'ขณะนี้มีไฟล์' + Description + ' (เพิ่มขึ้นจำนวน ' + (newCount-oldCount) + ' ฉบับ)<br>'
        + 'โดยมีไฟล์รอดำเนินการรวมจำนวน '+ newCount + ' ไฟล์ดังรายการต่อไปนี้ <ul>'
        + listFilesEmail + '</ul>'
        + 'ตรวจสอบข้อมูลได้ที่: ' + folderURL + '<br>';
    var emailFooter = 'อีเมล์นี้ถูกส่งโดยอัตโนมัติ';
    var mailMessage = '<p>' +  emailBody + '<br><br><small> ' + emailFooter + '</small>'; 
  
  //If the files in the folder are more than before, send a message to Google chat and/or Line Messenger
  if (newCount > oldCount) {  
    if (GoogleChatWebhook !== '') {
      sendGoogleChat(GoogleChatWebhook,chatMessage);
    }
    if (LineNotiToken !== '') {
      sendLineNotify(LineNotiToken,chatMessage);
    }
    if (emailAddress !== '') {
      MailApp.sendEmail(emailAddress, emailSubject, "", {name: emailFromName, htmlBody: mailMessage});
    }
  };
  
    //Set the count of files to the latest
  sheet.getRange("A"+(index+1)).setValue(newCount);
    //Logger.log(newCount);
};

function sendGoogleChat(webhook,msg) {
    // create google chat payload
    var message = { text: msg};
    var payload = JSON.stringify(message);
    var options = {
            method: 'POST',
            contentType: 'application/json',
            payload: payload
    };
  
    UrlFetchApp.fetch(webhook, options ).getContentText();
}

function sendLineNotify(token,message) {
    //create line noti payload
    var options = {
        "method": "post",
        "payload": "message=" + '\n' + message,
        "headers": {
            "Authorization": "Bearer " + token
        }
    };

    UrlFetchApp.fetch("https://notify-api.line.me/api/notify", options);
}

งานบริการกลาง

SERVICE TEAM

ภาสพันธ์ จิโนทา

passpun jinota

passji@kku.ac.th
50443