-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_handler.py
More file actions
executable file
·439 lines (393 loc) · 18.3 KB
/
Copy pathtelegram_handler.py
File metadata and controls
executable file
·439 lines (393 loc) · 18.3 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import ujson
import aiohttp
import time
import config
from random import randint
import asyncio
import logging
import os
# this class gets updates from telegram api and sorts the data. most of the methods are self-explanatory. most of them
# needs an object of get_all() (mostly called 'data') in order to return the value
class Bot(object):
def __init__(self, token):
self.token = token
self.link = f'https://api.telegram.org/bot{self.token}'
self.session = aiohttp.ClientSession()
self.watchlist = {}
async def get_all(self):
async with self.session.get(f'{self.link}/getUpdates') as response:
return await response.json()
# defining the function that would look up for updates and put it in a queue. every message that bot receives gets
# logged. when this function receives a message - it checks for spamming. if spam returns true - it will start up a
# process that would handle the message.
async def loop_void(self, queue, data_resolver):
while True:
try:
data = await self.get_all()
offset = self.get_id(data) + 1
await self.log_saver(str(self.get_name(data)), str(self.get_from_id(data)), str(self.get_message(data)),
self.get_chat_type(data), self.get_chat_id(data), self.get_username_or_first_name(data))
# if spam.checker(bot.get_chat_id(data)):
await queue.put(data)
await self.session.get(self.link + '/getUpdates?offset=' + str(offset))
await data_resolver(queue, admin=True)
# elif not spam.checker(bot.get_chat_id(data)):
# requests.get(bot.link + '/getUpdates?offset=' + str(offset))
except (IndexError, KeyError, TypeError):
pass
except aiohttp.client_exceptions.ClientOSError as e:
await asyncio.sleep(3 + randint(0, 9))
# Logging method
@staticmethod
async def log_saver(name, uid, message, ctype, cid, uname):
if os.path.exists('./logs/general.log'):
pass
else:
os.mkdir('./logs')
logging.basicConfig(filename=f'{os.path.dirname(os.path.abspath(__file__))}/../logs/general.log', level=logging.INFO,
format='%(asctime)s:%(message)s')
logging.info('TYPE {}-{} BY @{}-{}, ID {} : {}'.format(ctype, cid, uname, name, uid, message))
# Spam protection method
def checker(self, uid):
if uid in self.watchlist:
if int(time.time())-int(self.watchlist[uid]) < 3:
self.watchlist.update({uid: int(time.time())})
return False
elif int(time.time())-int(self.watchlist[uid]) >= 3:
self.watchlist.update({uid: int(time.time())})
return True
elif uid not in self.watchlist:
self.watchlist.update({uid: int(time.time())})
return True
@staticmethod
def get_id(data):
return int(data['result'][0]['update_id'])
async def offset(self, data):
offset = self.get_id(data) + 1
self.session.get(self.link + '/getUpdates?offset=' + str(offset))
@staticmethod
def get_chat_id(data):
if 'edited_message' in data['result'][0]:
return data['result'][0]['edited_message']['chat']['id']
elif 'callback_query' in data['result'][0]:
return data['result'][0]['callback_query']['message']['chat']['id']
else:
return data['result'][0]['message']['chat']['id']
@staticmethod
def get_from_id(data):
if 'edited_message' in data['result'][0]:
return data['result'][0]['edited_message']['from']['id']
elif 'callback_query' in data['result'][0]:
return data['result'][0]['callback_query']['from']['id']
else:
return data['result'][0]['message']['from']['id']
@staticmethod
def get_sender_id(data):
if 'edited_message' in data['result'][0]:
if 'type' in data['result'][0]['edited_message']['chat'] == 'group':
return data['result'][0]['edited_message']['from']['id']
elif 'type' in data['result'][0]['edited_message']['chat'] == 'private':
return data['result'][0]['edited_message']['from']['id']
else:
if 'type' in data['result'][0]['message']['chat'] == 'group':
return data['result'][0]['message']['from']['id']
elif 'type' in data['result'][0]['message']['chat'] == 'private':
return data['result'][0]['message']['from']['id']
@staticmethod
def get_chat_type(data):
if 'edited_message' in data['result'][0]:
return data['result'][0]['edited_message']['chat']['type']
elif 'callback_query' in data['result'][0]:
return data['result'][0]['callback_query']['message']['chat']['type']
else:
return data['result'][0]['message']['chat']['type']
def get_message(self, data):
if self.is_callback(data):
return self.get_callback_data(data)
if 'message' in data['result'][0]:
if 'chat' in data['result'][0]['message']:
if data['result'][0]['message']['chat']['type'] == 'private':
if 'text' in data['result'][0]['message']:
return data['result'][0]['message']['text']
elif data['result'][0]['message']['chat']['type'] == 'group':
if 'text' in data['result'][0]['message']:
return data['result'][0]['message']['text']
if 'new_chat_participant' in data['result'][0]['message']:
return False
elif 'left_chat_participant' in data['result'][0]['message']:
return False
elif 'pinned_message' in data['result'][0]['message']:
return False
elif 'message' in data['result'][0]['edited_message']:
if 'chat' in data['result'][0]['edited_message']:
if data['result'][0]['edited_message']['chat']['type'] == 'private':
if 'text' in data['result'][0]['edited_message']:
return data['result'][0]['edited_message']['text']
elif data['result'][0]['edited_message']['chat']['type'] == 'group':
if 'text' in data['result'][0]['edited_message']:
return data['result'][0]['edited_message']['text']
if 'new_chat_participant' in data['result'][0]['edited_message']:
return False
elif 'left_chat_participant' in data['result'][0]['edited_message']:
return False
elif 'pinned_message' in data['result'][0]['edited_message']:
return False
else:
return False
@staticmethod
def get_message_id(data):
if 'edited_message' in data['result'][0]:
return data['result'][0]['edited_message']['message_id']
else:
return data['result'][0]['message']['message_id']
# this method creates a set of buttons for the message
@staticmethod
def make_keyboard(my_list=None):
if my_list is None:
my_list = []
arr = []
for i in my_list:
res = ('{"text":"%s"}' % i)
arr.append(res)
keys = ('{"keyboard":[%s]}' % arr)
return str(keys.replace('\'', ''))
@staticmethod
def make_inline(buttons, callback=None):
button_set = {'inline_keyboard': []}
if not buttons:
prep = [{'text': 'Sorry, nothing here.', 'url': 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'}]
button_set['inline_keyboard'].append(prep)
return button_set
elif buttons:
if callback is not None:
if type(callback) is list:
for item, call in zip(buttons, callback):
prep = [{'text': item, 'callback_data': call}]
button_set['inline_keyboard'].append(prep)
elif type(callback) is str:
for item in buttons:
prep = [{'text': item, 'callback_data': callback}]
button_set['inline_keyboard'].append(prep)
elif callback is None:
for item in buttons:
prep = [{'text': item, 'url': f'https://telegra.ph/{item}'}]
button_set['inline_keyboard'].append(prep)
elif callback == 'empty':
num = 0
for item in buttons:
prep = [{'text': item, 'callback_data': f'callback_data{num}'}]
button_set['inline_keyboard'].append(prep)
num += 1
return button_set
# this method creates a dictionary that can be used for post requests. this one is used for regular text messages
def make_payload(self, ide, text, markup=None, inline=None, callback=None):
if inline is not None:
if callback is None:
payload = {
'chat_id': ide,
'text': text,
'reply_markup': ujson.dumps(self.make_inline(inline))
}
return payload
elif callback is not None:
payload = {
'chat_id': ide,
'text': text,
'reply_markup': ujson.dumps(self.make_inline(inline, callback=callback))
}
return payload
elif markup is not None:
payload = {
'chat_id': ide,
'text': text,
'reply_keyboard': self.make_keyboard(list(markup[0]))
}
return payload
else:
payload = {
'chat_id': ide,
'text': text,
}
return payload
@staticmethod
def make_charge(callback_id, text):
charge = {
'callback_query_id': callback_id,
'text': text,
'show_alert': False
}
return charge
@staticmethod
def prepare_message(text):
if len(text) > 4096:
return [text[i:i + 4096] for i in range(0, len(text), 4096)]
else:
return [text]
# this method creates sendMessage requests that may contain inline keyboard, use get_chat_id or use clean data
async def send_message(self, data, message, pure=None, inline=None, callback=None, chat_id=None):
address = f'{self.link}/sendMessage'
if inline is not None:
await self.session.post(address,
data=self.make_payload(ide=self.get_chat_id(data), text=message,
inline=inline))
elif inline and callback is not None:
await self.session.post(address,
data=self.make_payload(ide=self.get_chat_id(data), text=message,
inline=inline,
callback=callback))
elif pure is not None:
await self.session.post(address, data=self.make_payload(data, message))
elif chat_id is not None:
await self.session.post(address, data=self.make_payload(chat_id, message))
else:
msg = self.prepare_message(message)
for i in msg:
await self.session.post(address, data=self.make_payload(self.get_chat_id(data), i))
#
# await self.session.post(address,
# data=self.make_payload(ide=self.get_chat_id(data), text=message,
# inline=inline))
# await self.session.post(address,
# data=self.make_payload(ide=self.get_chat_id(data), text=message,
# inline=inline,
# callback=callback))
# await self.session.post(address, data=self.make_payload(self.get_from_id(data), message))
# msg = self.prepare_message(message)
# for i in msg:
# await self.session.post(address, data=self.make_payload(self.get_chat_id(data), i))
# # pure
# await self.session.post(address, data=self.make_payload(data, message))
# # chat_id
# await self.session.post(address, data=self.make_payload(chat_id, message))
@staticmethod
def get_name(data):
if 'edited_message' in data['result'][0]:
return data['result'][0]['edited_message']['from']['first_name']
elif 'callback_query' in data['result'][0]:
return data['result'][0]['callback_query']['from']['first_name']
elif 'message' in data['result'][0]:
return data['result'][0]['message']['from']['first_name']
else:
return False
@staticmethod
def is_username(data):
if 'message' in data['result'][0]:
if 'username' in data['result'][0]['message']['from']:
return True
elif 'edited_message' in data['result'][0]:
if 'username' in data['result'][0]['edited_message']['from']:
return True
elif 'callback_query' in data['result'][0]:
if 'username' in data['result'][0]['callback_query']['from']:
return True
else:
return False
@staticmethod
def get_username(data):
if 'message' in data['result'][0]:
return data['result'][0]['message']['from']['username']
elif 'edited_message' in data['result'][0]:
return data['result'][0]['edited_message']['from']['username']
elif 'callback_query' in data['result'][0]:
return data['result'][0]['callback_query']['from']['username']
else:
return False
@staticmethod
def get_username_or_first_name(data):
if 'message' in data['result'][0]:
if 'username' in data['result'][0]['message']['from']:
return data['result'][0]['message']['from']['username']
else:
return data['result'][0]['message']['from']['first_name']
elif 'edited_message' in data['result'][0]:
if 'username' in data['result'][0]['edited_message']['from']:
return data['result'][0]['edited_message']['from']['username']
else:
return data['result'][0]['edited_message']['from']['first_name']
elif 'callback_query' in data['result'][0]:
if 'username' in data['result'][0]['callback_query']['from']:
return data['result'][0]['callback_query']['from']['username']
else:
return data['result'][0]['callback_query']['from']['firstname']
else:
return False
# same as make_payload(), creates a dictionary for delete_message() method that deletes messages. should rewrite it
@staticmethod
def make_remove(chat, message):
payload = {
'chat_id': chat,
'message_id': message
}
return payload
# this method deletes a message
async def delete_message(self, data):
url = f'{self.link}/deleteMessage'
await self.session.post(url, data=self.make_remove(self.get_chat_id(data), self.get_message_id(data)))
def print_debug(self):
print(self.link)
@staticmethod
def is_callback(data):
if 'callback_query' in data['result'][0]:
return True
else:
return False
@staticmethod
def get_callback_id(data):
if 'callback_query' in data['result'][0]:
return data['result'][0]['callback_query']['id']
else:
pass
async def callback_response(self, data, text):
callback = f'{self.link}/answerCallbackQuery'
await self.session.post(callback, data=self.make_charge(self.get_callback_id(data), text))
@staticmethod
def get_callback_data(data):
data1 = [data]
if 'callback_query' in data1[0]['result'][0]:
return data1[0]['result'][0]['callback_query']['data']
async def direct_message(self, chat_id, message):
address = f'{self.link}/sendMessage'
await self.session.post(address,
data=self.make_payload(ide=chat_id, text=message))
async def get_uptime(self, start, data):
if self.get_chat_id(data) == 237892260:
past = start
vremechko = time.asctime(time.gmtime(time.time() - past))
await self.direct_message(chat_id='237892260', message=f'{vremechko[7:-5]}')
else:
await self.send_message(data, 'Only Gargoyle can do that!')
@staticmethod
def get_reply_to(data):
if 'message' in data['result'][0]:
if 'reply_to_message' in data['result'][0]['message']:
if 'username' in data['result'][0]['message']['reply_to_message']['from']:
return data['result'][0]['message']['reply_to_message']['from']['username']
else:
return data['result'][0]['message']['reply_to_message']['from']['first_name']
@staticmethod
def is_reply(data):
if 'message' in data['result'][0]:
if 'reply_to_message' in data['result'][0]['message']:
return True
else:
return False
@staticmethod
def make_photo(chat_id, photo, caption=None,):
if caption is None:
payload = {
'chat_id': chat_id,
'photo': photo
}
return payload
elif caption is not None:
payload = {
'chat_id': chat_id,
'photo': photo,
'caption': caption
}
return payload
async def send_photo(self, data, link):
address = f'{self.link}/sendPhoto'
await self.session.post(address, data=self.make_photo(self.get_chat_id(data), link))
def strict(self, data):
return self.get_from_id(data) == config.ADMIN