-
Notifications
You must be signed in to change notification settings - Fork 0
/
utelegram.py
92 lines (78 loc) · 2.92 KB
/
utelegram.py
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
import time
import gc
import ujson
import urequests
class ubot:
def __init__(self, token, offset=0):
self.url = 'https://api.telegram.org/bot' + token
self.commands = {}
self.default_handler = None
self.message_offset = offset
self.sleep_btw_updates = 3
messages = self.read_messages()
if messages:
if self.message_offset==0:
self.message_offset = messages[-1]['update_id']
else:
for message in messages:
if message['update_id'] >= self.message_offset:
self.message_offset = message['update_id']
break
def send(self, chat_id, text):
data = {'chat_id': chat_id, 'text': text}
try:
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = urequests.post(self.url + '/sendMessage', json=data, headers=headers)
response.close()
return True
except:
return False
def read_messages(self):
result = []
self.query_updates = {
'offset': self.message_offset + 1,
'limit': 1,
'timeout': 30,
'allowed_updates': ['message']}
try:
update_messages = urequests.post(self.url + '/getUpdates', json=self.query_updates).json()
if 'result' in update_messages:
for item in update_messages['result']:
result.append(item)
return result
except (ValueError):
return None
except (OSError):
print("OSError: request timed out")
return None
def listen(self):
while True:
self.read_once()
time.sleep(self.sleep_btw_updates)
gc.collect()
def read_once(self):
messages = self.read_messages()
if messages:
if self.message_offset==0:
self.message_offset = messages[-1]['update_id']
self.message_handler(messages[-1])
else:
for message in messages:
if message['update_id'] >= self.message_offset:
self.message_offset = message['update_id']
self.message_handler(message)
break
def register(self, command, handler):
self.commands[command] = handler
def set_default_handler(self, handler):
self.default_handler = handler
def set_sleep_btw_updates(self, sleep_time):
self.sleep_btw_updates = sleep_time
def message_handler(self, message):
if 'text' in message['message']:
parts = message['message']['text'].split(' ')
if parts[0] in self.commands:
self.commands[parts[0]](message)
else:
if self.default_handler:
self.default_handler(message)