Last active
July 21, 2021 14:11
-
-
Save nwatab/7ba08590a7ab461a6e5d0552f8801af5 to your computer and use it in GitHub Desktop.
CRUD-operation-in-google-app-script-with-LINE-bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CHANNEL_ACCESS_TOKEN = 'xxx='; | |
var line_endpoint = 'https://api.line.me/v2/bot/message/reply'; | |
function doGet(e) { | |
return ContentService.createTextOutput(UrlFetchApp.fetch("http://ip-api.com/json")); | |
} | |
function doPost(e) { | |
var contents = JSON.parse(e.postData.contents); | |
//่ฟไฟกใใใใใฎใใผใฏใณๅๅพ | |
var reply_token = contents.events[0].replyToken; | |
if (typeof reply_token === 'undefined') return; | |
//้ใใใLINEใกใใปใผใธใๅๅพ | |
var user_message = ""; | |
var data = {}; | |
if (contents.events[0].type == "postback") { | |
data = JSON.parse(contents.events[0].postback.data); | |
} else if (contents.events[0].type == "message") { | |
var message_type = contents.events[0].message.type; | |
if (message_type === "text") { | |
user_message = contents.events[0].message.text; | |
} else if (message_type === "video") { | |
} | |
} | |
//่ฟไฟกใใๅ ๅฎนใไฝๆ | |
var messages; | |
var userId = contents.events[0].source.userId; | |
if (user_message.match(/^ๅๅ็ป้ฒ .+$/)) { | |
var name = user_message.split(" ")[1] | |
var res = user.createName(contents.events[0].source.userId, name); | |
var text; | |
if (res) { | |
text = "ๅๅใ" + name + "ใใ็ป้ฒใใใพใใ."; | |
} else { | |
text = "ๅๅใใใงใซ็ป้ฒใใใฆใใใฎใง, ใ/ๅๅๅคๆด ๆฐใใๅๅใใงๅๅๅคๆดใใฆใใ ใใ"; | |
} | |
messages = [{ | |
type: "text", | |
text: text | |
}]; | |
} else if (user_message.match(/^ๅๅ่กจ็คบ$/)) { | |
messages = [{ | |
type: "text", | |
text: "ๅๅใฏใ" + user.readName(userId) + "ใใงใ." | |
}]; | |
} else if (user_message.match(/^ๅๅๅคๆด .+$/)) { | |
var newName = user_message.split(" ")[1] | |
var names = user.updateName(userId, newName); | |
var oldName = names.oldName; | |
messages = [{ | |
type: "text", | |
text: "ใ" + oldName + "ใใใ" + newName + "ใใซๅคๆดใใพใใ." | |
}]; | |
} else if (user_message.match(/^ๅๅๅ้ค$/)) { | |
var deletedRow = user.deleteName(userId); | |
messages = [{ | |
type: "text", | |
text: "ๅๅใ" + deletedRow[1] + "ใใๅ้คใใใพใใ." | |
}]; | |
} else if (contents.events[0].type === "join" || user_message.match(/^\/?(help|ใใซใ|ใธใใท|ไฝฟใๆน)$/)) { | |
messages = [{ | |
type: "text", | |
text: 'ๅๅ็ป้ฒ name\nๅๅ่กจ็คบ\nๅๅๅคๆด name\nๅๅๅ้ค\nใฎใใใใใๅ ฅๅใใฆใใ ใใ\n(ไพ: ๅๅ็ป้ฒ ใซใใใใใฃใจ).\nๅ็ปใๆ็จฟใใใจใซใฆใณใใใพใใ' | |
}]; | |
// messages.push({ | |
// type: "text", | |
// text: JSON.stringify(e, null, 2) | |
// }); | |
} | |
if (!messages) return; | |
UrlFetchApp.fetch(line_endpoint, { | |
'headers': { | |
'Content-Type': 'application/json; charset=UTF-8', | |
'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN, | |
}, | |
'method': 'post', | |
'payload': JSON.stringify({ | |
'replyToken': reply_token, | |
'messages': messages || [{type: "text", text: "Error Occured"}], | |
}), | |
}); | |
return ContentService.createTextOutput(JSON.stringify({ | |
'content': 'post ok' | |
})).setMimeType(ContentService.MimeType.JSON); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var user = { | |
sheet: SpreadsheetApp.getActive().getSheetByName('user'), | |
createName: function(id, name) { | |
// if success, return true, if record exists, return false | |
var data = this.sheet.getDataRange().getValues(); | |
for (var i = 0; i < data.length; i++) { | |
if (data[i][0] == id) { | |
return false; | |
} | |
} | |
this.sheet.appendRow([id, name]); | |
return true; | |
}, | |
readName: function(id) { | |
// if success, return name, if record not found, return false | |
var data = this.sheet.getDataRange().getValues(); | |
for (var i = 0; i < data.length; i++) { | |
if (data[i][0] == id) { | |
return data[i][1]; | |
} | |
} | |
return false; | |
}, | |
updateName: function(id, newName) { | |
// if success, return {oldName: name, newName: name}, if record not found, return false | |
var data = this.sheet.getDataRange().getValues(); | |
for (var i = 0; i < data.length; i++) { | |
if (data[i][0] == id) { | |
this.sheet.getRange(i + 1, 2).setValue(newName); // 2 for Column B | |
return { | |
oldName: data[i][1], | |
newName: newName | |
}; | |
} | |
} | |
return false; | |
}, | |
deleteName: function(id) { | |
// if success, return row number, if record not found, return false | |
var data = this.sheet.getDataRange().getValues(); | |
for (var i = data.length - 1; i >= 0; i--) { | |
if (data[i][0] == id) { // [0] for the first column | |
this.sheet.deleteRow(i + 1); // sheet row number starts from 1 | |
return data[i]; | |
} | |
} | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment