วิธีใช้ไลบรารีนี้ในโปรเจกต์ของคุณ เพิ่มด้วย Script ID ต่อไปนี้:
1oCK1JMrD5geYxFKx5bZ57HddGlDH8nBnebX1kAdakhe7_jNN_bZNz-Zz
วิธีใช้ไลบรารีนี้ในโปรเจกต์ Google Apps Script ของคุณ:
ตัวอย่างการใช้งาน
const lineNotifyPayload = {
"message": "สวัสดี LINE Messaging API",
"imageFullsize": "https://api.wealth-analyst.com/images/wealthanalystwww/1742974045430_Songkran1.png",
"imageThumbnail": "https://api.wealth-analyst.com/images/wealthanalystwww/1742974045430_Songkran1.png",
"stickerPackageId": "446",
"stickerId": "1988"
};
// lineNotifyPayload คือ รูปแบบข้อความตัวอย่าง ตามที่ต้องใช้งานบน LINE Notify
const messagingAPIPayload = convertLINENotiyMessageToLINEMessagingAPIPushMessage.convertMessaging(lineNotifyPayload);
// messagingAPIPayload คือ รูปแบบข้อความที่ใช้บน LINE Messaging API
เมื่อ คุณส่งข้อมูลที่เป็นข้อความที่ใช้งานบน LINE Notify เข้าไปยัง ไลบรารี convertLINENotiyMessageToLINEMessagingAPIPushMessage ที่ฟังก์ชั่น convertMessaging
ฟังก์ชั่นจะทำการส่งค่ากลับให้คุณด้วยรูปแบบที่สามารถใช้งานได้บน LINE Messaging API
อีก 1 ฟังก์ชั่นที่เป็นของแถมบนไลบารี คือ ฟังก์ชั่นสำหรับการส่ง LINE Push Message ผ่าน LINE OA ของคุณเอง
ฟังก์ชั่น :
sendLinePushMessage(string channelAccessToken, string userId, Object|Array messageObject)
รับข้อมูล 3 ค่าเพื่อใช้งาน :
channelAccessToken
userId
messageObject
ฟังก์ชั่นจะไม่ทำการเก็บข้อมูลอะไรใดๆเอาไว้ ขอให้ใช้ได้อย่างสบายใจ
หรือหากต้องการโค้ดเอาไว้เป็นฟังก์ชั่นของตัวเอง สามารถใช้ได้ตามโค้ดด้านล่างนี้
// Add LINE Messaging API push message function
/**
* Sends a push message to a specific user using LINE Messaging API
*
* @param {string} channelAccessToken - LINE channel access token
* @param {string} userId - LINE user ID to send the message to
* @param {Object|Array} messageObject - Message object or array of message objects to send
* @return {Object} Response from the LINE API
*/
function sendLinePushMessage(channelAccessToken, userId, messageObject) {
// Prepare the messages array
let messages = Array.isArray(messageObject) ? messageObject : [messageObject];
// Prepare the payload
const payload = {
to: userId,
messages: messages
};
// Prepare the options for the HTTP request
const options = {
'method': 'post',
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + channelAccessToken
},
'payload': JSON.stringify(payload)
};
try {
// Send the request to LINE Messaging API
const response = UrlFetchApp.fetch('https://api.line.me/v2/bot/message/push', options);
return {
success: true,
code: response.getResponseCode(),
content: JSON.parse(response.getContentText())
};
} catch (error) {
// Handle errors
return {
success: false,
error: error.toString()
};
}
}
โปรดติดตามเราบน YouTube เพื่อเขาถึงเนื้อหาการที่เป็นประโยชน์ 🎉