-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.py
44 lines (36 loc) · 1.15 KB
/
daemon.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
#!/usr/bin/env python3
import persistence
import requests
import config
import methods
import atexit
methods.startupMessage()
atexit.register(methods.shutdownMessage)
storage = persistence.UpdatesDatabase()
last_update_id = storage.getLastUpdate()
while True:
methods.alarms()
#print("Make request: {0}".format(last_update_id))
r = requests.post(config.API_URL + "getUpdates", json={
"offset" : last_update_id + 1,
"timeout" : config.TIMEOUT
}, timeout=config.TIMEOUT + 5)
result = r.json()
if result["ok"]:
for update in result["result"]:
update_id = update["update_id"]
if update_id > last_update_id:
last_update_id = update_id
storage.registerLastUpdate(update_id)
message = update.get("message")
if message:
try:
methods.processMessage(update["message"])
except Exception as err:
print(err)
else:
print("Could not resolve message: {0}".format(update))
else:
# TODO error handling
print(result)
#print(result)