forked from tegohsx/laporan-keuangan-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.gs
87 lines (73 loc) · 1.9 KB
/
index.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
//CONFIG
var BOT_TOKEN = "1234567890:abcdefghijklmnopqrstuvwxyz" //BOT TOKEN ANDA
var SS_URL = "https://docs.google.com/spreadsheets/d/abcdefghijklmnopq/edit#gid=0" //URL SPREADSHEET
var SHEET_NAME = "laporan" //NAMA SHEET
var USERS = [
173739838,
183837728
] //CHAT ID, bisa lebih dari 1
//BEGIN
var SHEET = SpreadsheetApp.openByUrl(SS_URL).getSheetByName(SHEET_NAME);
function doGet(e) {
return HtmlService.createHtmlOutput('<h1>OK</h1>')
}
function doPost(e) {
if (e.postData.type == "application/json") {
let update = JSON.parse(e.postData.contents);
if (update) {
commands(update)
return true
}
}
}
function commands(update) {
let chatId = update.message.chat.id;
let first_name = update.message.chat.first_name;
let text = update.message.text || '';
let tanggal = new Date().toLocaleString();
if (USERS.includes(chatId)) {
if (text.startsWith("/start")) {
sendMessage({
chat_id: chatId,
text: "Mulai laporan dengan cara \n/new [harga] [#kategori] [item1, item2 dst]"
})
} else if (text.startsWith("/new")) {
let item,
harga,
kategori,
stext = text.split(' ')
harga = eval(stext[1]);
kategori = stext[2].startsWith('#') ? stext[2].replace('#', '') : '';
stext.splice(0, 3);
item = stext.join(' ')
if (harga && kategori && item) {
insert_value([
tanggal,
kategori,
item,
harga,
chatId,
first_name
], SHEET)
sendMessage({
chat_id: chatId,
text: 'Laporan sukses.'
})
} else {
sendMessage({
chat_id: chatId,
text: 'Gagal. Pastikan sesuai format. \n/new [harga] [#kategori] [item1, item2 dst]'
})
}
}
}
}
function sendMessage(postdata) {
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(postdata),
'muteHttpExceptions': true
};
UrlFetchApp.fetch('https://api.telegram.org/bot' + BOT_TOKEN + '/sendMessage', options);
}