Skip to content

Instantly share code, notes, and snippets.

@nwatab
Last active May 27, 2021 10:35
Show Gist options
  • Save nwatab/7067eeb44e7864e9ebb78220992b8b8e to your computer and use it in GitHub Desktop.
Save nwatab/7067eeb44e7864e9ebb78220992b8b8e to your computer and use it in GitHub Desktop.
Supporterz 2 and 24, October, 2018
function doGet (request) {
return ContentService.createTextOutput(JSON.stringify({
post: "ok"
}))
.setMimeType(ContentService.MimeType.JSON);
}
var CHANNEL_ACCESS_TOKEN = 'YOUR_CHANNEL_ACCESS_TOKEN';
var line_endpoint = 'https://api.line.me/v2/bot/message/reply';
function doPost(e) {
var json = JSON.parse(e.postData.contents);
var userId = contents.events[0].source.userId;
var reply_token= json.events[0].replyToken;
var user_message = contents.events[0].message.text;
if (user_message.match(/^予約作成$/)) {
var today = new Date();
var nextWeek = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);
reservation.createReservation(userId, nextWeek);
var message = nextWeek.toString() + "を予約しました";
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': [{
'type': 'text',
'text': message,
}],
}),
});
}
return ContentService.createTextOutput(JSON.stringify({'content': 'post ok'}))
.setMimeType(ContentService.MimeType.JSON);
}
//LINE Developersで取得したアクセストークンを入れる
var CHANNEL_ACCESS_TOKEN = 'YOUR_CHANNEL_ACCESS_TOKEN';
var line_endpoint = 'https://api.line.me/v2/bot/message/reply';
//ポストで送られてくるので、送られてきたJSONをパース
function doPost(e) {
var json = JSON.parse(e.postData.contents);
//返信するためのトークン取得
var reply_token= json.events[0].replyToken;
//送られたメッセージ内容を取得
var message = json.events[0].message.text;
// メッセージを返信
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': [{
'type': 'text',
'text': message,
}],
}),
});
return ContentService.createTextOutput(JSON.stringify({'content': 'post ok'}))
.setMimeType(ContentService.MimeType.JSON);
}
var reservation = {
sheet: SpreadsheetApp.getActive().getSheetByName('reservation'),
/**
* reservation creater. userId and datetime are not unique columns
* @param {[string]} userId [Line User ID]
* @param {[Date]} datetime [date object]
*/
createReservation: function(userId, datetime) {
var data = this.sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) { // avoid duplicate row
var row = data[i];
if (row[0] == userId && parseInt(row[1]) == datetime.getTime()) {
return {status: 409}; // conflict.
}
}
return {status: 201, sheet: this.sheet.appendRow([userId, datetime.getTime()])};
},
readReservation: function(userId, from) {
userId = (typeof userId !== 'undefined') ? userId : "";
var data = this.sheet.getDataRange().getValues();
data = data.slice(1, data.length); // skip table header
data = data.sort(function(a, b) {
return parseInt(a[1]) - parseInt(b[1]); // sort ascending by unixtime
}).map(function(row) {
return [row[0], parseInt(row[1])];
});
return data;
},
deleteReservation: function(userId, from) {
from = (typeof from !== 'string') ? from : from.toString();
var data = this.sheet.getDataRange().getValues();
for (var i = data.length - 1; i >= 0 ; i--) {
if (data[i][0] == userId && data[i][1] == from) {
this.sheet = this.sheet.deleteRow(i + 1); // sheet row number starts from 1
return {status: 200, row: data[i], sheet: this.sheet};
}
}
return {status: 404};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment