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

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