Skip to content

Instantly share code, notes, and snippets.

@nwatab
Last active July 21, 2021 14:11
Show Gist options
  • Save nwatab/7ba08590a7ab461a6e5d0552f8801af5 to your computer and use it in GitHub Desktop.
Save nwatab/7ba08590a7ab461a6e5d0552f8801af5 to your computer and use it in GitHub Desktop.
CRUD-operation-in-google-app-script-with-LINE-bot
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);
}
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;
}
};
@nwatab
Copy link
Author

nwatab commented Jul 21, 2018

image

@nwatab
Copy link
Author

nwatab commented Jul 21, 2018

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment