แจ้งเตือนตามเวลา Line Messaging API

function sendLineNotification() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // ชื่อชีต
  const data = sheet.getDataRange().getDisplayValues(); // ดึงข้อมูลจากชีต
  const now = new Date();

  // รูปแบบวันที่และเวลา
  const todayStr = Utilities.formatDate(now, Session.getScriptTimeZone(), "yyyy-MM-dd");
  const timeStr = Utilities.formatDate(now, Session.getScriptTimeZone(), "HH:mm");

  const token = 'xxxx'; // Channel access token
  const userId = 'xxxx'; // User ID

  // ตรวจสอบข้อมูลในชีต
  for (let i = 1; i < data.length; i++) { // เริ่มจากแถวที่ 2 (ไม่รวม header)
    const [date, time, activity] = data[i];

    // แปลงวันที่ในชีตให้เป็น Date object
    const dateObj = new Date(date);

    // ตรวจสอบว่าตรงกับวันที่และเวลาปัจจุบัน
    if (Utilities.formatDate(dateObj, Session.getScriptTimeZone(), "yyyy-MM-dd") === todayStr && time === timeStr) {
      // แปลงวันที่เป็นภาษาไทย
      const thaiDate = formatThaiDate(dateObj);

      // สร้างข้อความแจ้งเตือน
      const message = `📢 กิจกรรมแจ้งเตือน\n📅 ${thaiDate}\n⏲️ เวลา: ${time+" น."}\n 📌 กิจกรรม: ${activity}`;
      sendToLine(token, userId, message);
    }
  }
}

function sendToLine(token, userId, message) {
  const url = "https://api.line.me/v2/bot/message/push";
  const payload = {
    to: userId,
    messages: [
      {
        type: "text",
        text: message
      }
    ]
  };

  const options = {
    method: "post",
    headers: {
      Authorization: `Bearer ${token}`
    },
    contentType: "application/json",
    payload: JSON.stringify(payload)
  };

  UrlFetchApp.fetch(url, options);
}

function formatThaiDate(date) {
  const monthNamesThai = [
    "มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", 
    "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", 
    "ตุลาคม", "พฤศจิกายน", "ธันวาคม"
  ];

  const day = date.getDate(); // วันที่
  const month = monthNamesThai[date.getMonth()]; // เดือนภาษาไทย
  const year = date.getFullYear() + 543; // ปี พ.ศ.

  return `วันที่ ${day} ${month} ${year}`;
}

  
{fullwidth}

ติดต่อสอบถามปัญหาได้เลยครับ

แสดงความคิดเห็น (0)
ใหม่กว่า เก่ากว่า