-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCode.js
More file actions
137 lines (127 loc) · 3.34 KB
/
Copy pathCode.js
File metadata and controls
137 lines (127 loc) · 3.34 KB
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
129
130
131
132
133
134
135
136
137
const version = 2107132240
/**
* @param {object} e
*/
function doPost(e) {
if (e.postData.type != 'application/json') return;
let data = JSON.parse(e.postData.contents);
let message = data.message;
if (!message || isBotCommand(message.entities)) return;
if (message.chat.type == 'private') {
let req = forwardMessage(message.chat.id, message.message_id);
setCache(this.admin + ':' + req.result.message_id, message.chat.id.toString());
return;
}
if (message.chat.id == this.admin && message.reply_to_message) {
let chat_id = 0;
if (message.reply_to_message.forward_from) {
chat_id = message.reply_to_message.forward_from.id;
} else {
chat_id = getCache(this.admin + ':' + message.reply_to_message.message_id);
}
copyMessage(parseInt(chat_id.toString()), message.message_id);
return;
}
}
/**
* @param {object} data
* @param {string} data.token
* @param {number} data.admin
* @param {CacheService.Cache} data.cache
* @param {string} data.exec
*
* contact me at Telegram.
* username: daffaalam
* id: 256902271
*
* @return {this}
*/
function init(data) {
if (!data || data == {}) throw 'data must not be null or empty';
if (!data.token) throw 'data token must not be null or empty';
if (!data.admin) throw 'data admin must not be null or empty';
if (!data.cache) throw 'data cache must not be null or empty';
if (!data.exec) throw 'data exec must not be null or empty';
this.token = data.token;
this.admin = data.admin;
this.cache = data.cache;
this.exec = data.exec;
return this;
}
/**
* @param {string} method
* @param {object} data
* @return {object}
*/
function request(method, data) {
data = JSON.stringify(data, null, 2);
let url = 'https://api.telegram.org/bot' + this.token + '/';
let params = {
'contentType': 'application/json',
'method': 'post',
'payload': data,
'muteHttpExceptions': true
};
let req = UrlFetchApp.fetch(url + method, params);
let res = JSON.parse(req.getContentText());
if (res.ok) return res;
else throw res.description + '\n\n`' + data + '`';
}
/**
* @param {object} entities
* @return {boolean}
*/
function isBotCommand(entities) {
if (!entities) return false;
let command = entities[0].type == 'bot_command';
let offset = entities[0].offset == 0;
if (command && offset) return true;
return false;
}
function forwardMessage(from_chat_id, message_id, chat_id = this.admin) {
let params = {
chat_id: chat_id,
from_chat_id: from_chat_id,
message_id: message_id
}
return request('forwardMessage', params);
}
function copyMessage(chat_id, message_id, from_chat_id = this.admin) {
let params = {
chat_id: chat_id,
from_chat_id: from_chat_id,
message_id: message_id
};
return request('copyMessage', params);
}
/**
* @return {object}
*/
function setWebhook() {
deleteWebhook();
let params = {
url: 'https://script.google.com/macros/s/' + this.exec + '/exec',
allowed_updates: ['message', 'poll']
};
return request('setWebhook', params);
}
/**
* @return {object}
*/
function deleteWebhook() {
return request('deleteWebhook');
}
/**
* @param {string} key
* @param {string} data
*/
function setCache(key, data) {
return this.cache.put(key, data, 21600);
}
/**
* @param {string} key
* @return {string}
*/
function getCache(key) {
return this.cache.get(key);
}