-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
68 lines (63 loc) · 2.05 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const accessToken = 'あなたのチャネルアクセストークン'
const lineReplyUrl = 'https://api.line.me/v2/bot/message/reply'
const spreadsheetId = 'スプレッドシートのID'
function doPost(e) {
// イベントの中から必要な情報を取得
let data = JSON.parse(e.postData.contents)
let json = data.events[0]
let replyToken = json.replyToken
let userMessage = json.message.text
let spreadSheetApp = SpreadsheetApp.openById(spreadsheetId)
let sheet = spreadSheetApp.getSheets()[0]
// メッセージを解析
let command = userMessage.split("\n")[0]
let todo = userMessage.split("\n")[1]
let todoList = []
// 命令を実行する
if (command === "追加") {
sheet.appendRow([todo])
} else if (command === "一覧") {
let rows = sheet.getDataRange().getValues();
for (row of rows) {
todoList.push(row[0])
}
} else if (command === "削除") {
let rows = sheet.getDataRange().getValues();
for (row of rows) {
if (row[0] === todo) {
todoList.push([""])
} else {
todoList.push([row[0]])
}
}
sheet.getDataRange().setValues(todoList)
}
// 返信メッセージを作る
let replyMessage = ""
if (command === "追加") {
replyMessage = "追加しました: " + todo
} else if (command === "一覧") {
for (t of todoList) {
if (t !== "") {
replyMessage += t + "\n"
}
}
} else if (command === "削除") {
replyMessage = "削除しました: " + todo
}
// 返信処理を行う
UrlFetchApp.fetch(lineReplyUrl, {
'headers': {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + accessToken,
},
'method': 'post',
'payload': JSON.stringify({
'replyToken': replyToken,
'messages': [{
'type': 'text',
'text': replyMessage,
}]
})
})
}