-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
54 lines (48 loc) · 1.61 KB
/
main.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
import telepot
import model
import view
import controller
import sys
class App:
"""Here it is: this bot's entry point. Create a new app by giving it their
API code as provided by Telegram and start running it. It will load the
users' database file into memory and update their status."""
def __init__(self, api):
apiCode = api
print('Loading...')
self.bot = telepot.Bot(apiCode)
# self.bot.polling(none_stop = True)
self.model = model.Model('data')
self.controller = controller.Controller(self.model)
self.ids = { }
for userId in self.model.getIds():
self.ids[userId] = self.generateMVC(userId)
self.offset = 0
def loop(self):
updates = self.bot.getUpdates(self.offset)
# Dealing with updates
for update in updates:
if 'message' in update:
userId = update['message']['chat']['id']
if userId not in self.ids:
self.ids[userId] = self.generateMVC(userId)
self.ids[userId].answer(update)
# Taking care of offset
self.offset = updates[-1]['update_id'] + 1
def generateMVC(self, userId):
v = view.View(self.bot, userId)
self.controller.addView(v)
return v
if __name__ == '__main__':
api = sys.argv[1]
app = App(api)
print('---')
while True:
try:
app.loop()
except KeyboardInterrupt:
print('...')
break
except HTTPSConnectionPool:
print('--- # Connection lost... restarting')
app = App(api)