เนื่องจากเราสามารถส่งข้อความผ่านแอพพลิเคชันไลน์ได้หลากหลายรูปแบบ อาทิ flex,template,image,confirm,location,sticker,…,etc. ดังนั้นเราจึงสามารถส่งข้อความทุกรูปแบบที่กล่าวมาข้างต้นด้วย Google Script เช่นเดียวกัน ไปลุยกันเล้ยย
main.gs
// Presented by BrilliantPy v.1.0.2 /*######################### Editable1 Start #########################*/ let sheetName = "ชีต1" let ss_id = "10uV1iv5GrV_sd4pCPYuQnLmfVZwlTAQ-4hJR0wNBRrU"; let start_row_value = 1; /*######################### Editable1 End #########################*/ // Init let ss,sheet,lastRow,lastCol,range,values; Logger = BetterLog.useSpreadsheet(ss_id); function doPost(e) { initSpreadSheet(); try{ let req_content = e.postData.contents; Logger.log("req_content:"+req_content); let event_0 = JSON.parse(req_content).events[0]; let event_0_type = event_0.type; let token = event_0.replyToken; let msgObj = {}; if(event_0_type=="message"){ let user_msg = event_0.message.text; user_msg = user_msg.trim(); Logger.log("user_msg:"+user_msg); for(let i=start_row_value;i<values.length;i++){ let key = values[i][0]; let type = values[i][1]; let value1 = values[i][2]; let value2 = values[i][3]; let value3 = values[i][4]; let value4 = values[i][5]; let value5 = values[i][6]; if(key == user_msg){ if(type=="text"){ msgObj = { "type" : type, "text" : value1 } }else if(type=="sticker"){ msgObj = { "type" : type, "packageId": value1, "stickerId": value2 } }else if(type=="image"){ msgObj = { "type" : type, "originalContentUrl": value1, "previewImageUrl": value2 } }else if(type=="location"){ msgObj = { "type" : type, "title": value1, "address": value2, "latitude": value3, "longitude": value4 } }else if(type=="confirm"){ msgObj = { "type": "template", "altText": "Confirm template", "template": { "type": "confirm", "text": value1, "actions": [ { "type": "message", "label": value2, "text": value3 }, { "type": "message", "label": value4, "text": value5 } ] } } }else if(type=="carousel"){ msgObj = JSON.parse(value1); }else if(type=="image_carousel"){ msgObj = JSON.parse(value1); }else if(type=="test"){ msgObj = JSON.parse(value1); } break; } } if(event_0.message.type === "text"){ if(!isEmpty(msgObj)){ replyMessage(token, msgObj); } } }else if(event_0_type=="postback"){ let postback = event_0.postback; let postback_data = postback.data; Logger.log("postback_data:"+postback_data); } }catch(e){ Logger.log("doPost error:"+e); } } function initSpreadSheet() { ss = SpreadsheetApp.getActive(); sheet = ss.getSheetByName(sheetName); lastRow = sheet.getLastRow(); lastCol = sheet.getLastColumn(); range = sheet.getDataRange(); values = range.getValues(); Logger.log('initSpreadSheet completed'); } async function replyMessage(token,msgObj) { Logger.log("msgObj:"+JSON.stringify(msgObj)); let url = "https://api.line.me/v2/bot/message/reply"; let lineHeader = { "Content-Type": "application/json", "Authorization": "Bearer "+CHANNEL_ACCESS_TOKEN }; let postData = { "replyToken" : token, "messages" : [msgObj] }; let options = { "method" : "POST", "headers" : lineHeader, "payload" : JSON.stringify(postData) }; Logger.log("options:"+JSON.stringify(options)); try { let response = await UrlFetchApp.fetch(url, options); Logger.log("response:"+response); } catch (error) { Logger.log(error.name + ":" + error.message); return; } if (response.getResponseCode() === 200) { Logger.log("Sending message completed."); } } function isEmpty(obj) { for (const prop in obj) { if (Object.hasOwn(obj, prop)) { return false; } } return true; }
config.gs
// Presented by BrilliantPy v.1.0.1 /*######################### Editable1 Start #########################*/ let CHANNEL_ACCESS_TOKEN = 'Bri5ll26/antP9ri5ll07/pyb9ri5ll26/antP9ri5ll07/py+3591Bri9ll26/antP9ri5ll07/pyb9ri5ll2y+3Bri9ll26/antP9ri5ll07/pyf58Bri9ll26/antP9ri5ll06/antP9ri5ll07/py/1B/1cB9ri5ll07/py=' /*######################### Editable1 End #########################*/