-
Notifications
You must be signed in to change notification settings - Fork 3
/
TgFlow.py
219 lines (191 loc) · 5.85 KB
/
TgFlow.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import telebot
import hashlib
from . import handles
from . import render
import pickle,time
import pprint
pp = pprint.PrettyPrinter(indent=4)
action = handles.action
bot,key = None,None
def_state = None
def_data= None
States = {}
UI = {}
Data = {}
Actions = {}
Keyboards = {}
Reaction_triggers = {}
def read_sd(sf,df):
with open(sf,'rb') as f:
try:
s= pickle.load(f)
except:
s={}
with open(df,'rb') as f:
try:
d = pickle.load(f)
except:
d={}
return s,d
def save_sd(states,data):
try:
with open('states.p','wb+') as f:
pickle.dump(states,f)
with open('data.p','wb+') as f:
pickle.dump(data,f)
except Exception as e:
print('Non-picklable',str(e))
try:
States,Data = read_sd('states.p','data.p')
except FileNotFoundError:
print("tgflow: creating data.p and states.p files")
def configure(token=None, state=None, data={}):
global def_state,def_data
global bot,key
if not token:
raise Exception("tgflow needs your bot token")
if not state:
raise Exception("tgflow needs a default state for new users")
key =token
def_state=state
def_data =data
# create bot and assign handlers
bot = telebot.TeleBot(key)
bot.set_update_listener(message_handler)
set_callback_handler()
def start(ui):
global bot,UI
UI = ui
print("tgflow: listening")
while True:
try:
bot.polling(none_stop=True)
except Exception as e:
print("tgflow:polling error",e)
time.sleep(10)
def get_file_link(file_id):
finfo = bot.get_file(file_id)
l='https://api.telegram.org/file/bot%s/%s'%(
key,finfo.file_path)
return l
def message_handler(messages):
global States,UI
for msg in messages:
s = States.get(msg.chat.id,def_state)
print('tgflow: got message. State:'+str(s))
# for security reasons need to hash. user can call every action in this state
# key format: kb_+ButtonName
a = Actions.get('kb_'+str(msg.text))
if not a:
for r,a_ in Reaction_triggers[msg.chat.id]:
if msg.__dict__.get(r):
a = a_
d = Data.get(msg.chat.id,def_data)
messages = flow(a,s,d,msg,msg.chat.id)
send(messages,msg.chat.id)
def set_callback_handler():
@bot.callback_query_handler(func=lambda call: True)
def callback_handler(call):
s = States.get(call.message.chat.id,def_state)
a = Actions.get(call.data)
d = Data.get(call.message.chat.id,def_data)
messages = flow(a,s,d,call,call.message.chat.id)
if a:
if not a.update:
send(messages,call.message.chat.id)
else:
update(messages, call.message)
else:
print("tgflow: Warning: no action found but should")
send(messages,call.message.chat.id)
def gen_state_msg(i,ns,nd,_id,state_upd=True):
pre_a = UI.get(ns).get('prepare')
if pre_a:
# call user-defined data perparations.
nd = pre_a(i,ns,**nd)
args = {'s':ns,'d':nd}
ui = render.prep(UI.get(ns),args)
# saving data and state
Data[_id] = nd
if state_upd: States[_id] = ns
save_sd(States,Data)
# registering callback triggers on buttons
save_iactions(ui.get('b'))
save_kactions(ns,ui.get('kb'),ns)
print("tgflow: actions registration ended",Actions)
# registering reaction triggers
rc = ui.get('react')
if rc:
print("tgflow: reaction tgigger for %i registrated %s"%(i,str(rc)))
Reaction_triggers[_id].append((rc.react_to,rc))
# clearing reaction triggers if needed
rc = ui.get('clear_trig')
if rc:
print("tgflow: reaction trigger clear",rc)
for r,a_ in Reaction_triggers[_id]:
#TODO: handle arrays of triggers
if rc == r:
Reaction_triggers[_id].remove((r,a_))
# rendering message and buttons
messages = render.render(ui)
return messages
def send_state(ns,tg_id):
d = Data.get(tg_id,def_data)
msg = gen_state_msg(None,ns,d,tg_id)
send(msg,tg_id)
def flow(a,s,d,i,_id):
if a:
ns,nd = a.call(i,s,**d)
print('tgflow: called action:'+str(a))
else:
print('tgflow: no action found for message. State unchanged')
ns,nd = s,d
print ('tgflow: new state',ns)
return gen_state_msg(i,ns,nd,_id)
def get_state(id,s):
pass
def save_iactions(ui):
if isinstance(ui,action):
Actions[str(ui)]=ui
if isinstance(ui,dict):
for k,v in ui.items():
save_iactions(v)
elif isinstance(ui,list):
d = [save_iactions(x) for x in ui ]
# TODO: remove s argument
def save_kactions(k,ui,s):
if isinstance(ui,action):
# key format: State+ButtonName
if ui.react_to:
print('react to'+ui.react_to)
Reaction_triggers.append((ui.react_to,ui))
else:
Actions['kb_'+str(k)]=ui
if isinstance(ui,dict):
for k,v in ui.items():
save_kactions(k,v,s)
elif isinstance(ui,list):
ui = [save_kactions(k,x,s) for x in ui ]
def send(message,id):
print("tgflow: sending message")
for text,markup in message:
bot.send_message(
text=text,
chat_id=id,
parse_mode='Markdown',
reply_markup =markup)
def update(messages,msg):
print("tgflow: updating message")
for text,markup in messages:
if text:
bot.edit_message_text(
text=text,
chat_id=msg.chat.id,
parse_mode='Markdown',
message_id=msg.message_id
)
if markup:
bot.edit_message_reply_markup(
chat_id=msg.chat.id,
message_id=msg.message_id,
reply_markup=markup)