-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCode.js
356 lines (336 loc) · 8.96 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
const version = 2107060215
/**
* @param {object} e
* @return {HtmlService.HtmlOutput}
*/
function doGet(e) { return HtmlService.createHtmlOutput(this.alert) }
/**
* @param {object} e
*/
function doPost(e) {
if (!e.postData) return;
if (e.postData.type != 'application/json') return;
let data = JSON.parse(e.postData.contents);
try {
let message = data.message;
if (!message) return;
if (message.chat.type != 'private') return;
let sent = false;
if (message.media_group_id) Utilities.sleep(1000);
let isCommand = isBotCommand(message.entities);
let validTrigger = isValidTrigger(message.text) || isValidTrigger(message.caption);
if (isCommand) sendBotCommand(message.text);
else if (!validTrigger) sendMessage(this.invalid, message.from.id);
else sent = copyMessage(message.chat.id, message.message_id);
if (sent) sendMessage(this.thanks, message.from.id);
} catch (error) {
error = 'ERROR @' + this.channel + '\n\n' + error.toString();
error += '\n\n`' + JSON.stringify(data, null, 2) + '`';
sendMessage(error, this.admin);
}
return true;
}
/**
* @param {object} data
* @param {string} data.token
* @param {number} data.admin
* @param {string} data.trigger
* @param {string} data.channel
* @param {string} data.exec
* @param {string} data.thanks
* @param {string} data.report
* @param {string} data.invalid
* 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.exec) throw 'data exec must not be null or empty';
if (!data.channel) throw 'data channel must not be null or empty';
let thanks = 'Thanks for using this bot, your message will be sent immediately to ';
this.token = data.token;
this.botUrl = 'https://api.telegram.org/bot' + this.token;
this.admin = data.admin || -1001385703290;
this.trigger = data.trigger || '';
this.channel = data.channel;
this.username = '@' + this.channel;
this.alert = '<script>window.open("https://t.me/s/' + this.channel + '","_top")</script>';
this.idExec = data.exec;
this.thanks = data.thanks || thanks + this.username;
this.report = data.report || thanks + ' Admin.';
this.invalid = data.invalid || 'Your message does not contain trigger words.';
return this;
}
/**
* @param {string} endPoint
* @param {object} data
* @return {object}
*/
function request(endPoint, data = {}) {
data = JSON.stringify(data, null, 2);
let params = {
'contentType': 'application/json',
'method': 'post',
'payload': data,
'muteHttpExceptions': true
};
let request = UrlFetchApp.fetch(this.botUrl + endPoint, params);
let response = JSON.parse(request.getContentText());
if (response.ok) return response;
else throw response.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;
}
/**
* @param {string} text
* @return {boolean}
*/
function isValidTrigger(text) {
if (!this.trigger) return true;
if (!text) return false;
if (text.toLowerCase().includes(this.trigger)) return true;
return false;
}
/**
* @param {string} text
* @return {object}
*/
function parseMessage(text) {
let format = ['http://', 'https://', ''];
let separate = ['.rep', '-reply!', '-reply', ' '];
let result = {
id: 0,
text: text
};
if (!text) return result;
for (let i in format) {
let name = format[i] + 't.me/' + this.channel + '/';
if (text.indexOf(name) != 0) continue;
let start = text.indexOf(name) + name.length;
if (start == name.length - 1) continue;
for (let n in separate) {
let end = text.indexOf(separate[n]);
if (end == -1 || start == end) continue;
result.id = parseInt(text.substring(start, end));
result.text = text.substring(end + separate[n].length);
return result;
}
}
return result;
}
/**
* @param {string} command
* @return {boolean}
*/
function sendBotCommand(command) {
if (!command.toLowerCase().includes(' ')) return false;
sendMessage(command, this.admin);
this.thanks = this.report;
return true;
}
/**
* @param {string} message
* @param {string | number} id
* @param {boolean} preview
* @param {string} parse
* @return {object}
*/
function sendMessage(message, id = this.username, preview = false, parse = 'Markdown') {
let msg = parseMessage(message);
let params = {
chat_id: id,
text: msg.text,
parse_mode: parse,
disable_web_page_preview: preview,
reply_to_message_id: msg.id
};
let response = request('/sendMessage', params);
return response.result;
}
/**
* @param {string} image
* @param {string | number} id
* @return {object}
*/
function sendSticker(image, id = this.username) {
let params = {
chat_id: id,
sticker: image
};
let response = request('/sendSticker', params);
return response.result;
}
/**
* @param {string} image
* @param {string} message
* @param {string | number} id
* @param {string} parse
* @return {object}
*/
function sendPhoto(image, message, id = this.username, parse = 'Markdown') {
let msg = parseMessage(message);
let params = {
chat_id: id,
photo: image,
caption: msg.text,
parse_mode: parse,
reply_to_message_id: msg.id
};
let response = request('/sendPhoto', params);
return response.result;
}
/**
* @param {string} image
* @param {string} message
* @param {string | number} id
* @param {string} parse
* @return {object}
*/
function sendAnimation(image, message, id = this.username, parse = 'Markdown') {
let msg = parseMessage(message);
let params = {
chat_id: id,
animation: image,
caption: msg.text,
parse_mode: parse,
reply_to_message_id: msg.id
};
let response = request('/sendAnimation', params);
return response.result;
}
/**
* @param {string} vid
* @param {string} message
* @param {string | number} id
* @param {string} parse
* @return {object}
*/
function sendVideo(vid, message, id = this.username, parse = 'Markdown') {
let msg = parseMessage(message);
let params = {
chat_id: id,
video: vid,
caption: msg.text,
parse_mode: parse,
reply_to_message_id: msg.id
};
let response = request('/sendVideo', params);
return response.result;
}
/**
* @param {string} music
* @param {string} message
* @param {string | number} id
* @param {string} parse
* @return {object}
*/
function sendAudio(music, message, id = this.username, parse = 'Markdown') {
let msg = parseMessage(message);
let params = {
chat_id: id,
audio: music,
caption: msg.text,
parse_mode: parse,
reply_to_message_id: msg.id
};
let response = request('/sendAudio', params);
return response.result;
}
/**
* @param {string} doc
* @param {string} message
* @param {string | number} id
* @param {string} parse
* @return {object}
*/
function sendDocument(doc, message, id = this.username, parse = 'Markdown') {
let msg = parseMessage(message);
let params = {
chat_id: id,
document: doc,
caption: msg.text,
parse_mode: parse,
reply_to_message_id: msg.id
};
let response = request('/sendDocument', params);
return response.result;
}
/**
* @param {object} poll
* @param {string | number} id
* @param {string} parse
* @return {object}
*/
function sendPoll(poll, id = this.username, parse = 'Markdown') {
let answers = [];
for (let i in poll.options) answers.push(poll.options[i].text);
let params = {
chat_id: id,
question: poll.question,
options: answers,
type: poll.type,
allows_multiple_answers: poll.allows_multiple_answers,
correct_option_id: poll.correct_option_id,
explanation: poll.explanation,
explanation_parse_mode: parse
};
let response = request('/sendPoll', params);
return response.result;
}
/**
* @param {string} dice
* @param {string | number} id
* @return {object}
*/
function sendDice(dice, id = this.username) {
let params = {
chat_id: id,
emoji: dice
};
let response = request('/sendDice', params);
return response.result;
}
/**
* @param {string | number} from_chat
* @param {number} message
* @param {string | number} chat
* @return {object}
*/
function copyMessage(from_chat, message, chat = this.username) {
let params = {
chat_id: chat,
from_chat_id: from_chat,
message_id: message
};
let response = request('/copyMessage', params);
return response.result;
}
/**
* @return {object}
*/
function setWebhook() {
request('/deleteWebhook');
let params = {
url: 'https://script.google.com/macros/s/' + this.idExec + '/exec',
allowed_updates: ['message', 'poll']
};
return request('/setWebhook', params);
}
/**
* @return {object}
*/
function deleteWebhook() {
return request('/deleteWebhook');
}