-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
680 lines (585 loc) · 27.3 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import json
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, RegexHandler, ConversationHandler
from telegram import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove
from functools import wraps
from collections import OrderedDict
import locale
import time
import _thread as thread
import requests
import re
import os
import pickle
import datetime
from report import Doch1_Report
DATE_SELECT, PERSON_SELECT, STATUS_SELECT, CANCEL_SELECT = range(1,5)
"""
#Hova
01::נמצא ביחידה
01:נוכח
02::מחוץ ליחידה
05:בתפקיד מחוץ ליחידה
25:כוננות
09:אחרי תורנות \ משמרת
03:עובד משמרות
13:הפניה רפואית
08:יום סידורים-חייל בודד
16:משמרת ערב
19:נגרע ממתין לקליטה
02:אבט"ש
14:לימודים על סמך אישור
20:סבב קו
23:יום פרט
03::מסופח ליחידה אחרת
01:מסופח ליחידה אחרת
04::חופשה שנתית
01:חופשה שנתית
06:חג עדתי
11:אזכרה - קרבה ראשונה
05::חופשת מחלה
01:חופשת מחלה (גימלים)
08:יום ד
16:שמירת הריון
06::בקורס \ בהכשרה
01:בקורס \ בהכשרה
07::נעדר משרות שלא ברשות
01:נפקד
02:חשש לנפקדות
03:מחלה בנפקדות
08::כלוא
04:עבודות שרות צבאיות
01:בבסיס כליאה מעצר
02:עצור בכלא אזרחי
03:מעצר בית
09::מאושפז
01:מאושפז
12::חופשה מיוחדת
04:חופשה מפקד מיוחדת בהכשרה ראשונית
07:מיוחדת ע"ח שנתית - חובה
08:חופשת ממתינים
02:מיוחדת ע"ח המערכת - חובה
03:אבל - חיילי חובה
13::חו"ל
02:חו"ל בתפקיד
04:חו"ל במיוחדת
22:חו"ל דח"ש/חל"ת
27:חו"ל - מחלה
28:חו"ל בחופשה (חובה)
17::בידוד
17:מחלה שנתית
18:בידוד ביחידה
### Keva
01::נמצא ביחידה
01:נוכח
02::מחוץ ליחידה
05:בתפקיד מחוץ ליחידה
25:כוננות
09:אחרי תורנות \ משמרת
03:עובד משמרות
13:הפניה רפואית
16:משמרת ערב
19:נגרע ממתין לקליטה
02:אבט"ש
14:לימודים על סמך אישור
18:הצ"ח
20:סבב קו
15:השעייה בתפקיד
23:יום פרט
03::מסופח ליחידה אחרת
01:מסופח ליחידה אחרת
04::חופשה שנתית
01:חופשה שנתית
04:חופשת אבל - קבע
06:חג עדתי
10:חופשה ללא תשלום קצרה
11:אזכרה - קרבה ראשונה
13:חופשה צבורה
05::חופשת מחלה
01:חופשת מחלה (גימלים)
02:מחלה עפ"י הצהרה
07:טיפול רפואי
03:מחלת ילד
04:מחלת הורה
16:שמירת הריון
05:מחלת בן\בת זוג
09:מחלת ילד ממארת
10:מחלת בן זוג ממארת
11:הריון או לידת בת זוג
12:תרומת מח עצם / איברים
14:הורה לבעל מוגבלויות
15:מחלה בפציעה בתפקיד
06::בקורס \ בהכשרה
01:בקורס \ בהכשרה
07::נעדר משרות שלא ברשות
01:נפקד
02:חשש לנפקדות
03:מחלה בנפקדות
08::כלוא
04:עבודות שרות צבאיות
01:בבסיס כליאה מעצר
02:עצור בכלא אזרחי
03:מעצר בית
09::מאושפז
01:מאושפז
12::חופשה מיוחדת
05:חופשה מיוחדת - קבע
06:חופשה מיוחדת - התמחות
13::חו"ל
02:חו"ל בתפקיד
04:חו"ל במיוחדת
22:חו"ל דח"ש/חל"ת
27:חו"ל - מחלה
01:חו"ל בחופשה אנשי קבע
17::בידוד
17:מחלה שנתית
18:בידוד ביחידה
19:ע"ח חופשה שנתית
20:עבודה מהבית
20::חופשת לידה
01:חופשת לידה
05:חופשת לידה - חו"ל
"""
# Code: ['Description', 'Main code', 'Secondary code']
possible_statuses = {
'01':['נמצא ביחידה נוכח', '01', '01'],
'02':['הגנש/אבטש', '02', '02'],
'03':['הפנייה רפואית', '02', '13'],
'04':['חופשה שנתית', '04', '01'],
'05':['חופשה צבורה - קבע', '04', '13'],
'06':['אזכרה (דרגה ראשונה)', '04', '11'],
'07':['גימלים (רופא)', '05', '01'],
'08':['יום ד\' (החלטת מפקד) - חובה', '05', '08'],
'09':['מחלה (הצהרה) - קבע', '05', '02'],
'10':['בקורס/הכשרה', '06', '01'],
'11':['חו"ל בתפקיד', '13', '02'],
'12':['חו"ל במיוחדת', '13', '04'],
'13':['חו"ל בחופשה - קבע', '13', '01'],
'14':['חו"ל בחופשה - חובה', '13', '28'],
'15':['יום סידורים חייל בודד', '02', '08'],
'16':['מסופח ליחידה אחרת', '03', '01'],
'17':['מיוחדת ע"ח המערכת - חובה', '12', '02'],
'18':['מיוחדת ע"ח חופשה שנתית - חובה', '12', '07'],
'19':['אבל - חובה', '12', '03'],
'20':['בידוד - מחלה שנתית', '17', '17'],
'21':['בידוד - ביחידה', '17', '18'],
'22':['בידוד - ע"ח חופשה שנתית - קבע', '17', '19'],
}
START_TIME = datetime.time(8, 0, 0)
END_TIME = datetime.time(10, 30, 0)
custom_keyboard = [[KeyboardButton('/send_today')], [KeyboardButton('/show_future_config')], [KeyboardButton('/toggle_auto_send'), KeyboardButton('/change_future_config')], [KeyboardButton('/cancel_future_config')],]
reply_markup = ReplyKeyboardMarkup(custom_keyboard)
remove_markup = ReplyKeyboardRemove()
user_config = {}
future_dates = {}
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def restricted(func):
@wraps(func)
def wrapped(updater, context, *args, **kwargs):
chat_id = updater.message.chat.id
if str(chat_id) != user_config['telegram_chat_id']:
print('Unauthorized access denied for chat {}.'.format(chat_id))
updater.bot.send_message(chat_id=user_config['telegram_chat_id'], text='Unauthorized access denied for chat {}.'.format(user_id))
return
return func(updater, context, *args, **kwargs)
return wrapped
def time_in_range(time, start, end):
"""Return true if x is in the range [start, end]"""
return start.hour <= time.hour <= end.hour
def can_send_now():
now = datetime.datetime.now()
return time_in_range(now, START_TIME, END_TIME) and now.isoweekday() not in [5,6]
def setup_one_identity_routine(*args):
updater = args[0]
while True:
delete_conf_cache_old_dates()
now = datetime.datetime.now()
# check if time to send doch 1, if yes do, if no, sleep
if can_send_now() and (now.date() in conf_cache['send_dates'] or conf_cache['always_send']):
updater.bot.send_message(chat_id=user_config['telegram_chat_id'], text='Auto sending today\'s report: {date}'.format(date=now.date()))
print('שולח בצורה אוטומטית את הדוח של היום: {date}'.format(date=now.date()))
report = Doch1_Report(user_config)
updater.bot.send_message(chat_id=user_config['telegram_chat_id'], text='משיג רשימת חיילים')
res = report.login_and_get_soldiers()
if not res[0]:
updater.bot.send_message(chat_id=user_config['telegram_chat_id'], text=res[1])
res = send_report(report, res[1])
updater.bot.send_message(chat_id=user_config['telegram_chat_id'], text='Sent report:\n{report}'.format(report=res))
else:
print('Waiting for next time to report')
# calc time until 8:00 am and sleep
time_to_sleep = (datetime.timedelta(hours=24) - (now-now.replace(hour=START_TIME.hour, minute=START_TIME.minute, second=START_TIME.second))).total_seconds() % (24 * 3600)
print('Sleeping for {} hours'.format(time_to_sleep/60/60))
time.sleep(time_to_sleep)
@restricted
def unknown_command(updater, context):
updater.message.reply_text(text='לא הבנתי...', reply_markup=reply_markup)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def initialize_user_config(path='config.json'):
global user_config
with open(path, 'rb') as f:
data = f.read()
user_config = json.loads(data)
"""
Todo: implement cookies cache file
global cookies_cache
if os.path.exists(cookie_cache_path):
with open(cookie_cache_path, 'rb') as f:
cookies_cache = pickle.load(f)
"""
def initialize_conf_cache(conf_cache_path='conf.cache'):
global conf_cache
if os.path.exists(conf_cache_path):
with open(conf_cache_path, 'rb') as f:
conf_cache = pickle.load(f)
else:
conf_cache={}
conf_cache['send_dates']=[]
conf_cache['send_confs']={}
conf_cache['always_send'] = False
conf_cache['default_configs'] = {}
write_to_conf_cache(conf_cache_path)
def write_to_conf_cache(conf_cache_path='conf.cache'):
with open(conf_cache_path, 'wb') as f:
pickle.dump(conf_cache, f, protocol=pickle.HIGHEST_PROTOCOL)
def delete_conf_cache_old_dates():
to_delete = []
for date in conf_cache['send_confs']:
if date < datetime.datetime.today().date():
to_delete.append(date)
for date in to_delete:
del conf_cache['send_confs'][date]
to_delete = []
for date in conf_cache['send_dates']:
if date < datetime.datetime.today().date():
to_delete.append(date)
for date in to_delete:
conf_cache['send_dates'].remove(date)
write_to_conf_cache()
def update_soldiers_list(updater, context):
updater.message.reply_text(text='משיג רשימת חיילים')
report = Doch1_Report(user_config)
res = report.login_and_get_soldiers()
if not res[0]:
updater.message.reply_text(text=res[1], reply_markup=reply_markup)
return
context.user_data['soldiers_list'] = res[1]
@restricted
def show_future_config_callback(updater, context):
"""When the command /show_future_config is issued."""
if len(conf_cache['send_confs']) != 0 or len(conf_cache['default_configs']) != 0:
if not 'soldiers_list' in context.user_data:
update_soldiers_list(updater, context)
soldiers = {}
for soldier in context.user_data['soldiers_list']:
soldiers[soldier['mi']] = soldier['firstName'] + ' ' + soldier['lastName']
conf_cache['send_dates'].sort()
conf_cache['send_confs'] = OrderedDict(sorted(conf_cache['send_confs'].items()))
text='Future auto send dates:\n'
if conf_cache['always_send']:
text += "Always !\n"
else:
for date in conf_cache['send_dates']:
text += ' {date}\n'.format(date=date.strftime('%d.%m.%Y'))
text += '\nFuture configs:\n'
for date in conf_cache['send_confs'].keys():
text += ' {date}:\n'.format(date=date.strftime('%d.%m.%Y'))
for key, val in conf_cache['send_confs'][date].items():
text += ' {key}: {val}\n'.format(key=soldiers[key], val=possible_statuses[val][0])
text += '\nDefault configs:\n'
for soldier_mi, status_code in conf_cache['default_configs'].items():
text += ' {key}: {val}\n'.format(key=soldiers[soldier_mi], val=possible_statuses[status_code][0])
updater.message.reply_text(text=text)
@restricted
def send_today_report_callback(updater, context):
"""When the command /send_today_report is issued."""
report = Doch1_Report(user_config)
if can_send_now():
updater.message.reply_text(text='משיג רשימת חיילים')
res = report.login_and_get_soldiers()
if not res[0]:
updater.message.reply_text(text=res[1], reply_markup=reply_markup)
context.user_data['soldiers_list'] = res[1]
updater.message.reply_text(text='שולח')
res = send_report(report, res[1])
updater.message.reply_text(text='Sent report:\n{report}'.format(report=res), reply_markup=reply_markup)
else:
updater.message.reply_text(text='לא יכול לשלוח עכשיו, רק בין ראשון-חמישי בין השעות {start}-{end}'.format(start=START_TIME.strftime("%H:%M"), end=END_TIME.strftime("%H:%M")))
delete_conf_cache_old_dates()
updater.message.reply_text(text='איך תרצה להמשיך?', reply_markup=reply_markup)
def auto_send_options(updater):
keyboard_temp = [[KeyboardButton('next morning')], [KeyboardButton('always on'), KeyboardButton('disable always on')], [KeyboardButton('X')]]
temp_markup = ReplyKeyboardMarkup(keyboard_temp)
updater.message.reply_text(text='באיזה תאריך? (פורמט dd.mm)', reply_markup=temp_markup)
@restricted
def toggle_auto_send_callback(updater, context):
"""When the command /toggle_auto_send is issued."""
auto_send_options(updater)
return DATE_SELECT
@restricted
def toggle_auto_send_by_text_callback(updater, context):
if updater.message.text == "always on":
conf_cache['always_send'] = True
write_to_conf_cache()
updater.message.reply_text(text='Auto send is now always on !', reply_markup=reply_markup)
elif updater.message.text == "disable always on":
conf_cache['always_send'] = False
write_to_conf_cache()
updater.message.reply_text(text='always on canceled !', reply_markup=reply_markup)
elif updater.message.text == "next morning":
updater.message.reply_text(text='Next Morning !', reply_markup=reply_markup)
now = datetime.datetime.now()
date = datetime.datetime.today() + datetime.timedelta(days=1)
if now.hour < START_TIME.hour:
date = datetime.datetime.today()
toggle_auto_send(updater, date.date())
else:
updater.message.reply_text(text='da fuck do u want ?', reply_markup=reply_markup)
auto_send_options(updater)
return DATE_SELECT
return ConversationHandler.END
def toggle_auto_send(updater, date):
# Add or cancel automatic sending in specific day
if date in conf_cache['send_dates']:
updater.message.reply_text(text='מבטל את השליחה האוטומטית בתאריך {}'.format(date.strftime('%d.%m.%Y')))
conf_cache['send_dates'].remove(date)
else:
updater.message.reply_text(text='מוסיף שליחה אוטומטית בתאריך {}'.format(date.strftime('%d.%m.%Y')))
conf_cache['send_dates'].append(date)
write_to_conf_cache()
updater.message.reply_text(text='איך תרצה להמשיך?', reply_markup=reply_markup)
def parse_date(updater):
try:
# Parse date to nearest date based on day and month (round year upwards)
date = datetime.datetime.strptime(updater.message.text, '%d.%m').replace(year=datetime.datetime.today().year)
if date.date() < datetime.datetime.today().date(): # Change next year
date = date.replace(year=datetime.datetime.today().year+1)
date = date.date()
return date
except Exception as e:
updater.message.reply_text(text='Can\'t parse date {}.'.format(str(e)), reply_markup=reply_markup)
return None
@restricted
def toggle_auto_send_by_date_callback(updater, context):
date = parse_date(updater)
if not date:
auto_send_options(updater)
return DATE_SELECT
toggle_auto_send(updater, date)
@restricted
def change_future_config_callback(updater, context):
"""When the command /change_future_config is issued."""
keyboard_temp = [[KeyboardButton('Next morning')], [KeyboardButton('Change default')], [KeyboardButton('X')]]
temp_markup = ReplyKeyboardMarkup(keyboard_temp)
updater.message.reply_text(text='באיזה תאריך? (פורמט dd.mm)', reply_markup=temp_markup)
return DATE_SELECT
@restricted
def change_next_morning_config_callback(updater, context):
now = datetime.datetime.now()
date = datetime.datetime.today() + datetime.timedelta(days=1)
if now.hour < START_TIME.hour:
date = datetime.datetime.today()
context.user_data['change_future_config_date'] = date.date()
display_people_list(updater, context)
return PERSON_SELECT
@restricted
def change_default_config_callback(updater, context):
context.user_data['change_future_config_date'] = "ALWAYS"
display_people_list(updater, context)
return PERSON_SELECT
@restricted
def select_future_config_date_callback(updater, context):
date = parse_date(updater)
if not date:
updater.message.reply_text(text='חרא של תאריך הבאת לי', reply_markup=reply_markup)
return ConversationHandler.END
context.user_data['change_future_config_date'] = date
display_people_list(updater, context)
return PERSON_SELECT
def display_people_list(updater, context):
# Change soldier's status in specific date
if not 'soldiers_list' in context.user_data:
update_soldiers_list(updater, context)
soldiers_chunks = divide_list_to_chunks(context.user_data['soldiers_list'], round(len(context.user_data['soldiers_list'])/2))
keyboard_temp = [[KeyboardButton(soldier['firstName']+' '+soldier['lastName']) for soldier in soldier_group] for soldier_group in soldiers_chunks]
keyboard_temp += [[KeyboardButton('X')]]
temp_markup = ReplyKeyboardMarkup(keyboard_temp)
updater.message.reply_text(text='איזה חייל תרצה לשנות?', reply_markup=temp_markup)
return PERSON_SELECT
@restricted
def soldier_name_callback(updater, context):
"""When sending soldiers name"""
soldier_to_change = None
# Finding soldier by name
for soldier in context.user_data['soldiers_list']:
if updater.message.text == soldier['firstName']+' '+soldier['lastName']:
soldier_to_change = soldier['mi']
if not soldier_to_change:
updater.message.reply_text(text='לא מצאתי את החייל..', reply_markup=reply_markup)
return ConversationHandler.END
context.user_data['change_future_config_soldier_to_change'] = soldier_to_change
context.user_data['change_future_config_soldier_to_change_name'] = updater.message.text
statuses_description = [[KeyboardButton("{status_id} - {desc}".format(status_id=status_id, desc=status_info[0]))] for status_id, status_info in possible_statuses.items()]
statuses_description += [[KeyboardButton('X')]]
temp_markup = ReplyKeyboardMarkup(statuses_description)
updater.message.reply_text(text='בחר את האפשרות:\n', reply_markup=temp_markup)
return STATUS_SELECT
@restricted
def soldier_change_status_callback(updater, context):
"""When sending status for a soldier in specific date"""
status_code = updater.message.text.split("-")[0].strip().zfill(2)
soldier_to_change = context.user_data['change_future_config_soldier_to_change']
soldier_to_change_name = context.user_data['change_future_config_soldier_to_change_name']
date_to_change = context.user_data['change_future_config_date']
context.user_data['change_future_config_soldier_to_change'] = None
context.user_data['change_future_config_soldier_to_change_name'] = None
context.user_data['change_future_config_date'] = None
# No such status
if not status_code in possible_statuses.keys():
updater.message.reply_text(text='אין סטטוס כזה, נסה שנית', reply_markup=reply_markup)
return ConversationHandler.END
if date_to_change == "ALWAYS":
if status_code == '01':
if soldier_to_change in conf_cache['default_configs']:
del conf_cache['default_configs'][soldier_to_change]
else:
conf_cache['default_configs'][soldier_to_change] = status_code
updater.message.reply_text(text='changed default status for {} to {}'.format(soldier_to_change_name, possible_statuses[status_code][0]), reply_markup=reply_markup)
return ConversationHandler.END
# Date does not exist yet in conf_cache['send_confs']
if not date_to_change in conf_cache['send_confs'].keys():
conf_cache['send_confs'][date_to_change] = {}
conf_cache['send_confs'][date_to_change][soldier_to_change] = status_code
write_to_conf_cache()
updater.message.reply_text(text='שיניתי בתאריך {date} את הסטטוס של {soldier} ל{status}'.format(date=date_to_change, soldier=soldier_to_change_name, status=possible_statuses[status_code][0]), reply_markup=reply_markup)
return ConversationHandler.END
@restricted
def cancel_callback(updater, context):
updater.message.reply_text(text='מה תרצה לעשות?', reply_markup=reply_markup)
return ConversationHandler.END
@restricted
def cancel_future_config_callback(updater, context):
"""When the command /cancel_future_config is issued."""
if len(conf_cache['send_confs']) == 0:
updater.message.reply_text(text="You idiot, you don't even have any future config set !", reply_markup=reply_markup)
return ConversationHandler.END
if not 'soldiers_list' in context.user_data:
update_soldiers_list(updater, context)
soldiers = {}
for soldier in context.user_data['soldiers_list']:
soldiers[soldier['mi']] = soldier['firstName'] + ' ' + soldier['lastName']
conf_cache['send_confs'] = OrderedDict(sorted(conf_cache['send_confs'].items()))
options = {}
keyboard_temp = []
for date in conf_cache['send_confs'].keys():
for key, val in conf_cache['send_confs'][date].items():
option_text = '{date} - {key} - {val}'.format(date=date.strftime('%d.%m.%Y'), key=soldiers[key], val=possible_statuses[val][0])
options[option_text] = (date, key)
keyboard_temp.append([KeyboardButton(option_text)])
keyboard_temp.append([KeyboardButton("never mind")])
context.user_data["cancel_options"] = options
temp_markup = ReplyKeyboardMarkup(keyboard_temp)
updater.message.reply_text(text="choose config to cancel", reply_markup=temp_markup)
return CANCEL_SELECT
@restricted
def select_config_to_cancel_callback(updater, context):
if updater.message.text not in context.user_data["cancel_options"].keys():
updater.message.reply_text(text="Bad option madafaka", reply_markup=reply_markup)
return ConversationHandler.END
date, soldier_mi = context.user_data["cancel_options"][updater.message.text]
del conf_cache["send_confs"][date][soldier_mi]
# if conf_cache['send_confs'][date_to_change] is empty, delete it from the dates list
if len(conf_cache['send_confs'][date]) == 0:
del conf_cache['send_confs'][date]
write_to_conf_cache()
updater.message.reply_text(text="Removed {}".format(updater.message.text), reply_markup=reply_markup)
return ConversationHandler.END
def divide_list_to_chunks(original_list, size):
for i in range(0, len(original_list), size):
yield original_list[i:i + size]
def send_report(report, soldiers_list):
pre_placements = {}
default_placements = conf_cache['default_configs']
if default_placements:
for soldier_id, status_code in default_placements.items():
pre_placements[soldier_id] = {}
pre_placements[soldier_id]['mainStatusCode'] = possible_statuses[status_code][1]
pre_placements[soldier_id]['secondaryStatusCode'] = possible_statuses[status_code][2]
todays_placement = conf_cache['send_confs'][datetime.datetime.today().date()] if datetime.datetime.today().date() in conf_cache['send_confs'] else None
if todays_placement:
for soldier_id, status_code in todays_placement.items():
pre_placements[soldier_id] = {}
pre_placements[soldier_id]['mainStatusCode'] = possible_statuses[status_code][1]
pre_placements[soldier_id]['secondaryStatusCode'] = possible_statuses[status_code][2]
#TODO: add option to send note on 'Outside base' status
#pre_placements[soldier_id]['note'] =
if pre_placements == {}:
pre_placements = None
return report.do_report_and_get_statuses(soldiers_list, pre_placements)
def main():
# load user configuration file
initialize_user_config()
initialize_conf_cache()
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
updater = Updater(user_config['telegram_api_key'], use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
dp.add_handler(CommandHandler('send_today', send_today_report_callback))
dp.add_handler(CommandHandler('show_future_config', show_future_config_callback))
dp.add_handler(ConversationHandler(
entry_points=[CommandHandler('toggle_auto_send', callback=toggle_auto_send_callback)],
states={
DATE_SELECT: [MessageHandler(Filters.regex(r'^[0-9]{1,2}\.[0-9]{1,2}$'), toggle_auto_send_by_date_callback),
MessageHandler(Filters.regex('X'), callback=cancel_callback),
MessageHandler(None, callback=toggle_auto_send_by_text_callback),
],
},
fallbacks=[]
))
dp.add_handler(ConversationHandler(
entry_points=[CommandHandler('change_future_config', callback=change_future_config_callback)],
states={
DATE_SELECT: [MessageHandler(Filters.regex(r'^[0-9]{1,2}\.[0-9]{1,2}$'), select_future_config_date_callback),
MessageHandler(Filters.regex(r'Change default'), change_default_config_callback),
MessageHandler(Filters.regex(r'Next morning'), change_next_morning_config_callback),
],
PERSON_SELECT: [MessageHandler(Filters.regex(r'^[\u0590-\u05fe\s\']+$'), soldier_name_callback)],
STATUS_SELECT: [MessageHandler(Filters.regex(r'^[0-9]{1,2}.+$'), soldier_change_status_callback)],
},
fallbacks=[MessageHandler(None, cancel_callback)]
))
dp.add_handler(ConversationHandler(
entry_points=[CommandHandler('cancel_future_config', callback=cancel_future_config_callback)],
states={
CANCEL_SELECT: [MessageHandler(Filters.regex("never mind"), cancel_callback), MessageHandler(None, select_config_to_cancel_callback),],
},
fallbacks=[],
))
# log all errors
dp.add_error_handler(error)
updater.bot.send_message(chat_id=user_config['telegram_chat_id'], text='מה תרצה לעשות?', reply_markup=reply_markup)
# Start the Bot
updater.start_polling()
# start new thread for daily notifications
args = (updater,)
#thread.start_new_thread(setup_daily_reminder, args)
thread.start_new_thread(setup_one_identity_routine, args)
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
# Make locale understand commas is number parsing!
# See https://stackoverflow.com/questions/2953746/python-parse-comma-separated-number-into-int.
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
main()