forked from fengnz/fengdoorbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.js
58 lines (48 loc) · 1.21 KB
/
Code.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
function debugCode() {
debug();
}
function doPost(e) {
if (!e) {
e = {};
e.postData = {};
e.postData.contents = testAdd;
}
var dataFromTelegram = {
"method": "post",
"payload": e.postData.contents
};
var body = JSON.parse(e.postData.contents);
if (body.message && body.message.chat) {
body.message.chat.id = body.message.chat.id + '';
}
var payload = preparePayload(body);
var payloads;
if (Array.isArray(payload)) {
payloads = payload;
} else {
payloads = [payload];
}
for (var i = 0; i < payloads.length; i++) {
payload = payloads[i];
if (payload) {
var handleResponseCallBack = null;
var delay = 0;
if (payload.callback) {
handleResponseCallBack = payload.callback;
delete payload.callback;
}
if (payload.delay) {
delay = payload.delay;
delete payload.delay;
}
//Be careful, as GAS can only make sync all. Only use the delay for the last payloads and adjust the delay time wisely.
if (delay > 0) {
Utilities.sleep(delay * 1000);
}
var res = postTelegram(payload);
if (handleResponseCallBack) {
handleResponseCallBack(res);
}
}
}
}