Skip to content

Instantly share code, notes, and snippets.

@mhakrook
Created May 5, 2020 10:28
Show Gist options
  • Save mhakrook/07b1201e8d383358ac1d37f728ec37be to your computer and use it in GitHub Desktop.
Save mhakrook/07b1201e8d383358ac1d37f728ec37be to your computer and use it in GitHub Desktop.
function doPost(e) {
var data = JSON.parse(e.postData.contents)
var intentName = data.queryResult.intent.displayName
var result
if (intentName == "studentID") {
result = gradeBOT(data)
} else {
result = timeTableBOT(data)
}
var replyJSON = ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON);
return replyJSON;
}
function gradeBOT(data) {
var ss = SpreadsheetApp.openById("1AXzA4H1k_kaCQL3_y9kYBNLLqM1KtduH_d1ATOOVn3I")
var stuId = data.queryResult.queryText
//var stuId = "ngn06165"
var sheet = ss.getSheetByName("ngnlist")
var mainSheetData = sheet.getDataRange().getDisplayValues()
var rowData = mainSheetData.find(function a(row) { //หาแถวที่รหัสตรงกับที่รหัส User ป้อนเข้ามา
return row[0] == stuId
})
sheet = ss.getSheetByName(rowData[1]) //เปิดชีทข้อมูลเกรดนักเรียน
var sheetData = sheet.getDataRange().getDisplayValues()
var studentRow = sheetData.find(function a(row) {
return row[3] == stuId
})
var mainsub = []
var extraSub = []
var activity = []
var stuClass = rowData[1][1] + "/" + rowData[1][3] + " เลขที่ " + studentRow[0]
studentRow.forEach(function a(value, index) {
if (value != "" && index >= 8) {
var subject = getSubject(sheetData[1][index])
var weight = getWeight(sheetData[1][index])
var grade = value == "ผ"? "ผ่าน": value
var sub = subJectFlex(subject, weight, grade)
if (index >= 8 && index < 18 && value != "") {
mainsub.push(sub)
} else if (index >= 18 && index < 26 && value != "") {
extraSub.push(sub)
} else if (index >= 26 && value != "") {
activity.push(sub)
}
}
})
var result = {
"fulfillmentMessages": [{
"platform": "line",
"type": 4,
"payload": {
"line": {
"type": "flex",
"altText": "เกรด",
"contents": {
"type": "carousel",
"contents": [{
"type": "bubble",
"size": "mega",
"body": {
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": "สรุปผลการเรียน",
"weight": "bold",
"size": "xxl",
"color": "#ff0000"
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": studentRow[4],
"size": "lg",
"color": "#0000ff",
"weight": "bold"
},
{
"type": "text",
"text": "ชั้น " + stuClass,
"color": "#0000ff",
"size": "lg",
"weight": "bold"
},
{
"type": "spacer"
}
]
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "separator"
},
{
"type": "text",
"text": "รายวิชาพื้นฐาน",
"weight": "bold",
"color": "#00ff00",
"size": "lg"
},
{
"type": "box",
"layout": "vertical",
"contents": mainsub,
"spacing": "sm"
}
],
"spacing": "md"
}
]
}
},
{
"type": "bubble",
"size": "mega",
"body": {
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": "สรุปผลการเรียน",
"weight": "bold",
"size": "xxl",
"color": "#ff0000"
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": studentRow[4],
"size": "lg",
"color": "#0000ff",
"weight": "bold"
},
{
"type": "text",
"text": "ชั้น " +stuClass,
"color": "#0000ff",
"size": "lg",
"weight": "bold"
},
{
"type": "spacer"
}
]
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "separator"
},
{
"type": "text",
"text": "รายวิชาเพิ่มเติม",
"weight": "bold",
"color": "#00ff00",
"size": "lg"
},
{
"type": "box",
"layout": "vertical",
"contents": extraSub,
"spacing": "sm"
}
],
"spacing": "md"
}
]
}
},
{
"type": "bubble",
"size": "mega",
"body": {
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": "สรุปผลการเรียน",
"weight": "bold",
"size": "xxl",
"color": "#ff0000"
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": studentRow[4],
"size": "lg",
"color": "#0000ff",
"weight": "bold"
},
{
"type": "text",
"text": "ชั้น " + stuClass,
"color": "#0000ff",
"size": "lg",
"weight": "bold"
},
{
"type": "spacer"
}
]
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "separator"
},
{
"type": "text",
"text": "กิจกรรมพัฒนาผู้เรียน",
"weight": "bold",
"color": "#00ff00",
"size": "lg"
},
{
"type": "box",
"layout": "vertical",
"contents": activity,
"spacing": "sm"
},
{
"type": "separator"
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "box",
"layout": "horizontal",
"contents": [{
"type": "text",
"text": "รวมหน่วยกิต",
"color": "#ff0000",
"weight": "bold",
"size": "xl"
},
{
"type": "text",
"text": studentRow[5],
"color": "#ff0000",
"weight": "bold",
"size": "xl",
"align": "end"
}
]
},
{
"type": "box",
"layout": "horizontal",
"contents": [{
"type": "text",
"text": "เกรดเฉลี่ย",
"color": "#ff0000",
"weight": "bold",
"size": "xl"
},
{
"type": "text",
"text": studentRow[6],
"color": "#ff0000",
"weight": "bold",
"size": "xl",
"align": "end"
}
]
}
],
"paddingTop": "20px"
},
{
"type": "spacer"
}
],
"spacing": "md"
}
]
}
}
]
}
}
}
}]
}
Logger.log(result)
return result
}
function timeTableBOT(data) {
var code = data.originalDetectIntentRequest.payload.data.message.text
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName(code.slice(0, 2))
var sheetData = sheet.getDataRange().getDisplayValues()
var targetRowIndex = sheetData.findIndex(function a(row) {
return row[3] == code
})
var date = sheetData[targetRowIndex][2]
var level = sheetData[targetRowIndex][4]
var contentsFlex = [];
contentsFlex.push({
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": "ตารางเรียน" + date,
"weight": "bold",
"size": "xl"
},
{
"type": "text",
"text": level,
"size": "lg",
"weight": "bold"
},
{
"type": "separator",
"margin": "md",
"color": "#ff0000"
}
]
})
for (var i = 8; i < sheetData[targetRowIndex].length; i++) {
var flex = setFlex(sheetData[1][i], sheetData[targetRowIndex][i])
contentsFlex.push(flex)
}
var result = {
"fulfillmentMessages": [{
"platform": "line",
"type": 4,
"payload": {
"line": {
"type": "flex",
"altText": code,
"contents": {
"type": "bubble",
"size": "giga",
"body": {
"type": "box",
"layout": "vertical",
"contents": contentsFlex
}
}
}
}
}]
}
return result
}
function setFlex(time, data) {
var flex;
if (data == "เบรค" || data == "พักกลางวัน") {
flex = {
"type": "box",
"layout": "vertical",
"backgroundColor": "#ff0000",
"cornerRadius": "5px",
"margin": "lg",
"spacing": "sm",
"contents": [{
"type": "box",
"layout": "horizontal",
"spacing": "sm",
"contents": [{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": time,
"wrap": true,
"color": "#ffffff",
"weight": "bold"
},
]
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": data,
"color": "#ff0000",
"weight": "bold"
},
]
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": data,
"color": "#ffffff",
"weight": "bold"
},
]
}
]
},
{
"type": "separator"
}
]
}
} else {
var splitData = data.split("/")
if (splitData.length > 1) {
flex = {
"type": "box",
"layout": "vertical",
"margin": "lg",
"spacing": "sm",
"contents": [{
"type": "box",
"layout": "horizontal",
"spacing": "sm",
"contents": [{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": time,
"wrap": true,
"color": "#666666",
"weight": "bold"
}]
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": "ครู" + splitData[1],
"color": "#00ff00",
"weight": "bold"
}]
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": "วิชา :" + splitData[0],
"color": "#0000ff",
"weight": "bold"
},
{
"type": "text",
"text": "ห้อง :" + splitData[2],
"color": "#ff00ff",
"weight": "bold"
}
]
}
]
},
{
"type": "separator"
}
]
}
} else {
flex = {
"type": "box",
"layout": "vertical",
"margin": "lg",
"spacing": "sm",
"contents": [{
"type": "box",
"layout": "horizontal",
"spacing": "sm",
"contents": [{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": time,
"wrap": true,
"color": "#666666",
"weight": "bold"
}]
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": "ครู",
"color": "#ffffff",
"weight": "bold"
}]
},
{
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": "วิชา :" + splitData[0],
"color": "#0000ff",
"weight": "bold"
}]
}
]
},
{
"type": "separator"
}
]
}
}
}
return flex
}
function getSubject(text) {
var a = text.split(" ")
var result
if (a[1].indexOf("(") > -1) {
var b = a[1].split("(")
result = a[0] + "(" + b[1]
} else {
result = a[0]
}
return result
}
function getWeight(text) {
var a = text.split(" ")
var b = a[1].split("(")
return b[0]
}
function subJectFlex(subject, weight, grade) {
var flex = {
"type": "box",
"layout": "vertical",
"contents": [{
"type": "text",
"text": "วิชา " + subject,
"wrap": true,
"size": "md",
"weight": "bold"
},
{
"type": "box",
"layout": "horizontal",
"contents": [{
"type": "text",
"text": weight + " หน่วยกิต",
"weight": "bold",
"color": "#ff55ff"
},
{
"type": "text",
"text": "เกรด " + grade,
"color": "#00ff22",
"weight": "bold"
}
]
}
],
"borderColor": "#d3d3d3",
"borderWidth": "1px",
"cornerRadius": "10px",
"paddingStart": "5px"
}
return flex
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment