-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.gs
128 lines (122 loc) · 4.3 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var token = "1676131447:AAG6wp9M41tZtCSuy1pR_BU2Odn-FyH0mgA";
var telegramUrl = "https://api.telegram.org/bot" + token;
var webAppUrl = "https://script.google.com/macros/s/AKfycbwoWJBItnwHwz-OwfO80L84edZgqugLql085ModARRnnDVlkjI/exec";
function setWebhook(){
var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
var response = UrlFetchApp.fetch(url);
}
function sendMessage(id, text, keyBoard){
var data = {
method: "post",
payload: {
method: "sendMessage",
chat_id: String(id),
text: text,
parse_mode: "HTML",
reply_markup: JSON.stringify(keyBoard)
}
};
UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}
function doPost(e){
var contents = JSON.parse(e.postData.contents);
var ssId = "1tzrl93Nn-Ce8iyI9f13PUMYd1PtJ9g064yxk3Isbtdc";
var sheet = SpreadsheetApp.openById(ssId).getSheetByName("Laundry Machines");
var number_keyBoard = {
"inline_keyboard": [
[{
"text": "Machine 1",
"callback_data": 1
}],
[{
"text": "Machine 2",
"callback_data": 2
}],
[{
"text": "Machine 3",
"callback_data": 3
}],
[{
"text": "Machine 4",
"callback_data": 4
}],
[{
"text": "Machine 5",
"callback_data": 5
}],
[{
"text": "Machine 6",
"callback_data": 6
}],
]
}
var sample_keyBoard = {
"keyboard": [
[
"Check machine status :D"
]/*,
[
"Notify :)"
]*/
],
resize_keyboard:true,
one_time_keyboard:false
}
if (contents.callback_query){
var id = contents.callback_query.from.id;
var username = contents.callback_query.from.username;
var data = contents.callback_query.data;
for (i = 1; i < 7; i++){
if (i == data){
var using = sheet.getDataRange().getCell(9, i + 1).getValue();
var status = sheet.getDataRange().getCell(3, i + 1).getValue();
var statusarr = status.split(',');
var counter = 0;
for (j = 0; j < statusarr.length; j++){
if (statusarr[j] == "ON"){
counter = counter + 1;
}
}
if (counter > 0 && using != ""){
var return_text = "Sorry... Machine " + i + " is currently in use... Please try again later D:";
} else {
sheet.getRange(9, i + 1).setValue("@" + username);
sheet.getRange(10, i + 1).setValue(id);
var return_text = "Machine " + i + " has been blocked under your name... You will be notified when your laundry is done!";
}
sendMessage(id, return_text);
}
}
} else if (contents.message){
var id = contents.message.from.id;
var text = contents.message.text;
if (text == "Check machine status :D"){
var checked = sheet.getDataRange().getCell(4, 2).getValue();
let [h, m, s] = checked.toLocaleTimeString("en-GB").split(/:| /);
var return_text = "";
for (i = 1; i < 7; i++){
var statusline = sheet.getDataRange().getCell(3, i + 1).getValue();
var statusarr = statusline.split(',');
var counter = 0;
for (j = 0; j < statusarr.length; j++){
if (statusarr[j] == "ON"){
counter = counter + 1;
}
}
if (counter == 0){
return_text = return_text + "\nMachine " + i + ": Available :D\n";
} else {
var runtime = sheet.getDataRange().getCell(7, i + 1).getValue();
let [hour, minute, second] = runtime.toLocaleTimeString("en-US").split(/:| /);
return_text = return_text + "\nMachine " + i + ": In use. Running for " + hour + " hour(s) and " + minute + " min(s) O.o\n";
}
}
return_text = return_text + "\n~ Last checked at " + h + ":" + m + " hrs.\n";
sendMessage(id, return_text);/*
} else if (text == "Notify :)"){
return sendMessage(id, "Select the machine to reserve ._.", number_keyBoard);*/
} else {
return sendMessage(id, "Hi there! Welcome to the Eusoff Laundry Bot... What would you like to do today? :P", sample_keyBoard);
}
}
}