-
Notifications
You must be signed in to change notification settings - Fork 1
/
ccal.py
143 lines (109 loc) · 4.18 KB
/
ccal.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
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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function
__author__ = 'ufian'
import config
import telepot
import time
import mongoengine as me
import datetime
from telepot.loop import MessageLoop
from telepot.delegate import per_chat_id, per_callback_query_chat_id, create_open, pave_event_space
from telepot.namedtuple import InlineQueryResultArticle, InputTextMessageContent, \
InlineKeyboardMarkup, InlineKeyboardButton, \
ReplyKeyboardMarkup, KeyboardButton
import utils as u
import model
import stages as st
import calculator as calc
class CCalException(Exception):
pass
def get_connect():
return me.connect(
config.DB['db'],
host=config.DB['host'],
port=config.DB['port'],
serverSelectionTimeoutMS=2500
)
class EatSession(telepot.helper.ChatHandler):
STAGES = {
st.DEFAULT: st.DefaultStage,
st.PRODUCT: st.ProductStage,
st.EAT: st.EatStage,
st.STAT: st.StatStage,
st.TODAY: st.TodayStage,
}
@property
def context(self):
return self.user_config.context
@property
def user_data(self):
return self.user_config.data
@property
def db(self):
return getattr(get_connect(), config.DB['db'])
def stage(self, new_stage=None):
stage = new_stage or self.context.get('stage', st.DEFAULT)
if stage not in self.STAGES:
stage = st.DEFAULT
self.context['stage'] = stage
return self.STAGES[stage](self)
def save_message(self, msg, skip_reply=False):
user_id = u.get_user_id(msg)
msg_copy = msg.copy()
msg_copy['date'] = datetime.datetime.utcnow()
self.db.messages.insert_one(msg_copy)
if not skip_reply:
self.sendMessage(user_id, 'Дамп из телегарма сохранен')
def open(self, initial_msg, seed):
self.user_id = seed
self.user_config = model.UserConfig.objects.filter(user_id=self.user_id).first()
print("Load ", self.user_config, 'by', seed)
if self.user_config is None:
self.user_config = model.UserConfig(user_id=self.user_id)
self.user_config.save()
if self.user_config.context is None:
self.user_config.context = dict()
if self.user_config.data is None:
self.user_config.data = dict()
return False
def on_chat_message(self, msg):
self.save_message(msg, skip_reply=True)
self.stage().on_chat_message(msg, u.get_text(msg))
def on_callback_query(self, msg):
self.save_message(msg, skip_reply=True)
cb_data = u.get_callback_data(msg)
stage, sep, cb_data = cb_data.partition('|')
if sep != '|':
cb_data = stage
stage = None
self.context['cb_message'] = u.get_edit_id(msg['message'])
self.stage(stage).on_callback_query(msg, cb_data)
def on_close(self, ex):
# st.DEFAULT -> fix message
stage = self.stage(st.DEFAULT)
if 'last_message' in self.context:
stage.base_message('Finish', update_msg=self.context['last_message'])
if 'cb_message' in self.context and self.context['cb_message'] != self.context.get('last_message'):
stage.base_message(
'Finish',
update_msg=self.context['cb_message'],
with_keyboard= 'last_message' not in self.context
)
if 'last_message' in self.context:
self.user_config.data['last_message'] = self.context['last_message']
self.user_config.context = dict()
self.user_config.save()
if __name__ == '__main__':
conn = get_connect()
params = list(model.GlobalConfig.objects.timeout(True))
bot = telepot.DelegatorBot(config.BOT_TOKEN, [
pave_event_space()(
[per_chat_id(('private',)), per_callback_query_chat_id(('private',))],
create_open,
EatSession,
include_callback_query=True, timeout=300),
])
MessageLoop(bot).run_as_thread()
print('Listening ...')
while 1:
time.sleep(10)