-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtelegram.py
62 lines (55 loc) · 2.06 KB
/
telegram.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
import os
import json
import requests
class telegram:
def __init__ (self, botname, token, useragent):
self.botname = botname
self.token = token
self.savefile = "telegram.json"
self.useragent = useragent
def send_to_bot(self, access_point, data=None):
headers = {'user-agent': self.useragent}
try:
r = requests.get('https://api.telegram.org/bot{0}/{1}'.format(self.token, access_point), data=data, timeout=40, headers=headers)
except requests.exceptions.ConnectionError:
return None
except requests.exceptions.Timeout:
return None
return r
def get_update(self):
if self.data["last_update"] != 0:
self.data["last_update"] += 1
r = self.send_to_bot('getUpdates?timeout=30&offset={0}'.format(self.data["last_update"]))
if not r:
return None
r_json = r.json()
if not r_json["ok"]:
return None
if not "result" in r_json:
return None
if len(r_json["result"]) > 0:
self.data["last_update"] = r_json["result"][-1]["update_id"]
return r
def open_session(self):
if not self.get_telegram_data():
self.data = {
"last_update": 0
}
def close_session(self):
self.set_telegram_data()
def get_telegram_data(self):
if not os.path.exists(self.savefile):
return False
else:
#with open(self.savefile, encoding='utf-8') as data_file:
with open(self.savefile) as data_file:
try:
self.data = json.load(data_file)
except:
raise
return False
return True
def set_telegram_data(self):
#with open(self.savefile, 'w', encoding='utf-8') as data_file:
with open(self.savefile, 'w') as data_file:
json.dump(self.data, data_file, sort_keys=True, indent=4, separators=(',', ': '))