-
Notifications
You must be signed in to change notification settings - Fork 7
/
bot.py
330 lines (263 loc) Β· 12.1 KB
/
bot.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineQueryResultArticle, InputTextMessageContent, InlineKeyboardMarkup, ParseMode
import logging
import json
import os
import numpy as np
import operator
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
actions = {
'NEXT': '1',
'PREV': '2',
'SEARCH': '3',
'SHOW': '4',
'CHOOSE': '5',
'CHANGE': '6',
'LAWER': '7',
'ASS': '8'
}
iterator = 0
reg = 0
is_searching = False
to_iterate = None
# ------------------ #
# Here goes bot's #
# Implementation #
# ------------------ #
def check_auth(func):
def wrapper(uid):
if reg:
func()
else:
pass
def get_token():
path = 'token.json'
with open(path, 'r') as jsn:
data = json.load(jsn)
return data['token']
keyboard1 = [[InlineKeyboardButton("Choose transaction", callback_data=actions['CHOOSE'])],
[InlineKeyboardButton("Change assignment", callback_data=actions['CHANGE'])],
[InlineKeyboardButton("Previous", callback_data=actions['PREV']),
InlineKeyboardButton("Next", callback_data=actions['NEXT'])]]
keyboard0 = [[InlineKeyboardButton("Lawer stats", callback_data=actions['LAWER']),
InlineKeyboardButton("Assignment stats", callback_data=actions['ASS'])],
[InlineKeyboardButton("Show me last transaction", callback_data=actions['SHOW'])],
[InlineKeyboardButton("Choose transaction", callback_data=actions['CHOOSE'])],
[InlineKeyboardButton("Change assignment", callback_data=actions['CHANGE'])]]
def start(bot, update):
if reg == 1:
update.message.reply_text('Start working...')
#update.message.reply_text('Please, enter your assignment:')
elif reg == 0:
update.message.reply_text('Your code')
def button(bot, update):
global iterator
global to_iterate
global iterator
global case
query = update.callback_query
# SHOW case
if query.data == actions['SHOW']:
send_transaction(query)
#bot.send_message(query.message.chat_id, 'The End. Now send me another keyword')
reply_markup = InlineKeyboardMarkup(keyboard1)
query.message.reply_text('Choose something', reply_markup=reply_markup)
# CHOOSE case
elif query.data == actions['CHOOSE']:
with open('json_4.json', 'r') as f:
d = json.load(f)
global lp
lp = []
for name, count in d[case]['Transactions'].items():
for k in range(to_iterate):
if count['Transaction Number'] == k:
lp.append((name, count, k))
keyboard2 = [[InlineKeyboardButton("{}".format(j[0]), callback_data=j[0])] for j in lp]
reply_markup = InlineKeyboardMarkup(keyboard2)
query.message.reply_text('Choose an action', reply_markup=reply_markup)
elif query.data == actions['LAWER']:
from legal_tech_utils import get_lawer_assignment_info
with open('json_4.json', 'r') as f:
d = json.load(f)
responsible_name = d[case]['Info']['Responsible']
lawer_assignment_info = get_lawer_assignment_info(responsible_name)
with open('plot11.png', 'rb') as f:
query.message.reply_photo(photo=f)
reply_markup = InlineKeyboardMarkup(keyboard0)
query.message.reply_text('Choose something', reply_markup=reply_markup)
elif query.data == actions['ASS']:
from legal_tech_utils import plot_spents
with open('json_4.json', 'r') as f:
d = json.load(f)
#responsible_name = d[case]['Info']['Responsible']
lawer_assignment_info = plot_spents(case, '2018-01-01')
with open('piechart_paid.png', 'rb') as f:
query.message.reply_photo(photo=f)
reply_markup = InlineKeyboardMarkup(keyboard0)
query.message.reply_text('Choose something', reply_markup=reply_markup)
# change case
elif query.data == actions['CHANGE']:
query.message.reply_text('Start working...')
query.message.reply_text('Please, choose of your possible assignments:')
global ass
with open('dict_code.json', 'r') as f:
d = json.load(f)
#update.message.reply_text('Great 2!')
ass = []
for name, k in d[code].items():
ass.append((name, k))
keyboard3 = [[InlineKeyboardButton("{}".format(j[0]), callback_data=j[0])] for j in ass]
reply_markup = InlineKeyboardMarkup(keyboard3)
query.message.reply_text('Choose an ass', reply_markup=reply_markup)
# prev case
if query.data == actions['PREV']:
#bot.edit_message_text(text="",
# chat_id=query.message.chat_id,
# message_id=query.message.message_id)
#bot.edit_message_text(text="Ok, prev",
# chat_id=query.message.chat_id,
# message_id=query.message.message_id)
#bot.edit_message_text(text="",
# chat_id=query.message.chat_id,
# message_id=query.message.message_id)
#bot.send_message(query.message.chat_id, '--------------------------')
if iterator+1 < to_iterate:
iterator += 1
# bot.send_message(query.message.chat_id, to_iterate[iterator])
send_transaction(query)
else:
bot.send_message(query.message.chat_id, 'Move Next please')
reply_markup = InlineKeyboardMarkup(keyboard1)
query.message.reply_text('Choose something', reply_markup=reply_markup)
# SEARCH case
elif query.data == actions['SEARCH']:
bot.edit_message_text(text="Ok, send me a keyword",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
is_searching = True
# next case
elif query.data == actions['NEXT']:
#bot.edit_message_text(text="",
# chat_id=query.message.chat_id,
# message_id=query.message.message_id)
#bot.edit_message_text(text="Ok, next",
# chat_id=query.message.chat_id,
# message_id=query.message.message_id)
#bot.edit_message_text(text="",
# chat_id=query.message.chat_id,
# message_id=query.message.message_id)
if iterator > 0:
iterator -= 1
# bot.send_message(query.message.chat_id, to_iterate[iterator])
send_transaction(query)
else:
bot.send_message(query.message.chat_id, 'The End. Now send me another keyword')
reply_markup = InlineKeyboardMarkup(keyboard1)
query.message.reply_text('Choose something', reply_markup=reply_markup)
for i in ass:
#query.message.reply_text('Ya tut1 {} {}'.format(query.data, ass[0]))
if query.data == i[0]:
#query.message.reply_text('Ya tut2')
case = str(i[1])
with open('json_4.json', 'r') as f:
d = json.load(f)
to_iterate = np.max([d[case]['Transactions'][j]['Transaction Number'] for j in list(d[case]['Transactions'].keys())])
query.message.reply_text('Info for your assignment: ')#{}'.format(d[case]['Info']))
sorted_x = sorted(d[case]['Info'].items(), key=operator.itemgetter(0))
#sorted_counts = sorted(d[case]['Info'].items(), key=lambda x: x[1], reverse=True)
for name, count in sorted_x:
if count == count: query.message.reply_text(text = '{}: <b>{}</b>'.format(name, count), parse_mode=ParseMode.HTML)
from legal_tech_utils import gen_status_bar
st_bar = gen_status_bar(case, '2008-08-30')
query.message.reply_text('Progress: <i>{}%</i>'.format(st_bar['percent'].split('\\')[0]), parse_mode=ParseMode.HTML)
with open(st_bar['filepath'], 'rb') as f:
query.message.reply_photo(photo=f)
reply_markup = InlineKeyboardMarkup(keyboard0)
#update.message.reply_text('Progress: <b>{}</b>'.format(st_bar['percent']))
query.message.reply_text('Choose something', reply_markup=reply_markup)
for j in lp:
if query.data == j[0]:
#update.message.reply_text('{} {}'.format(to_iterate, j[3]), parse_mode=ParseMode.HTML)
iterator = to_iterate - j[2]
send_transaction(query)
#bot.send_message(query.message.chat_id, 'The End. Now send me another keyword')
reply_markup = InlineKeyboardMarkup(keyboard1)
query.message.reply_text('Choose something', reply_markup=reply_markup)
def send_transaction(update):
chat_id = str(update.message.chat_id)
with open('json_4.json', 'r') as f:
d = json.load(f)
for name, count in d[case]['Transactions'].items():
if count['Transaction Number'] == to_iterate-iterator:
lt_name = name
lt = count
global graph
graph = lt
update.message.reply_text('<b>Transaction: {}</b>'.format(lt_name), parse_mode=ParseMode.HTML)
for name, count in lt.items():
if count == count: update.message.reply_text(text = '{}: <b>{}</b>'.format(name, count), parse_mode=ParseMode.HTML)
def rules_fun(bot, update):
global to_iterate
to_iterate = 0
global iterator
global ass
global lp
lp = []
iterator = 0
global case
global reg
global code
case = update.message.text
print('{}'.format(reg))
if reg == 0 and case in ['lol', 'kek']:
update.message.reply_text('Great!')
reg = 1
code = case
#start(bot, update)
chat_id = str(update.message.chat_id)
with open('dict_code.json', 'r') as f:
d = json.load(f)
#update.message.reply_text('Great 2!')
ass = []
for name, k in d[code].items():
ass.append((name, k))
keyboard3 = [[InlineKeyboardButton("{}".format(j[0]), callback_data=j[0])] for j in ass]
reply_markup = InlineKeyboardMarkup(keyboard3)
update.message.reply_text('Choose an ass', reply_markup=reply_markup)
elif reg == 0 and case not in ['lol', 'kek']:
update.message.reply_text('Wrong!')
start(bot, update)
else:
code = case
#start(bot, update)
chat_id = str(update.message.chat_id)
with open('dict_code.json', 'r') as f:
d = json.load(f)
#update.message.reply_text('Great 2!')
ass = []
for name, k in d[code].items():
ass.append((name, k))
keyboard3 = [[InlineKeyboardButton("{}".format(j[0]), callback_data=j[0])] for j in ass]
reply_markup = InlineKeyboardMarkup(keyboard3)
update.message.reply_text('Choose an ass', reply_markup=reply_markup)
def help_function(bot, update):
update.message.reply_text('Help!')
def error(bot, update, error_arg):
logger.warning('Update "%s" caused error "%s"' % (update, error_arg))
def main():
updater = Updater(get_token())
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help_function))
dp.add_error_handler(error)
# on non-command messages
dp.add_handler(MessageHandler(Filters.text, rules_fun))
dp.add_handler(CallbackQueryHandler(button))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
print("-> Hi!")
main()