-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.gs
105 lines (85 loc) · 2.6 KB
/
main.gs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var SLACK_INCOMING_WEBHOOK_URL = '';
var SPREAD_SHEET_ID = '';
function doPost(e) {
var inputString = e.parameter.text;
runMainProcess(inputString);
}
function runMainProcess(args) {
if (!checkProcessVaridation(args)) {
return;
}
runCommand(args);
}
function runCommand(inputString) {
var inputArray = inputString.split(" ");
var command = inputArray[1];
var message = "";
if (command == "echo") {
if (inputArray.length > 2) {
message = inputString.slice(parseInt(command.length, 10) + 1 + 9);
}
} else if (command == "lunch") {
var spreadsheet = SpreadsheetApp.openById(SPREAD_SHEET_ID);
var sheet = spreadsheet.getSheetByName('lunch_list');
var lastRow = sheet.getLastRow();
var loopCount = 1;
if (inputArray.length > 2) {
var inputCount = parseInt(inputArray[2]);
if (isNaN_(inputCount)) {
if (inputCount > 10) {
loopCount = 10;
} else {
loopCount = inputCount;
}
}
}
for (i=1;i<=loopCount;i++) {
var targetStoreId = Math.floor(Math.random()*lastRow);
if (targetStoreId < lastRow - 1) { targetStoreId = targetStoreId + 2; }
var storeName = sheet.getRange(targetStoreId, 1).getValue();
var storeUrl = sheet.getRange(targetStoreId, 2).getValue();
message += "*"+storeName+"* : "+storeUrl+"\n";
}
} else if (command == "help") {
message
= "*[echo {args}]* : 入力された文字列をそのまま返す\n"
+ "*[lunch]* : リストからランダムに1店舗返す\n"
+ "*[lunch {int value}]* : リストからランダムに指定された数の店舗を返す(最大10件まで)";
} else {
message = command + " command not found.";
}
Logger.log(message);
// notifyToSlack(message);
}
function isNaN_(value) {
var stringValue = String(value);
return typeof value === 'number' && stringValue != "NaN";
}
function checkProcessVaridation(message) {
var inputArray = message.split(" ");
if (inputArray.length < 2) {
// ["@ykkcbot", "{command}"] が最低限担保されていること
return false;
}
if (inputArray[0] != "@ykkcbot") {
// 入力の第一文字列がbotでなければ中断する
return false;
}
return true;
}
function notifyToSlack(message) {
var jsonData =
{
"text" : message
};
var options =
{
"method" : "post",
"contentType" : "application/json",
"payload" : JSON.stringify(jsonData)
};
UrlFetchApp.fetch(SLACK_INCOMING_WEBHOOK_URL, options);
}
function test() {
runMainProcess("@ykkcbot lunch");
}