-
Notifications
You must be signed in to change notification settings - Fork 2
/
handlers.py
1201 lines (961 loc) · 59.3 KB
/
handlers.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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
# Archivo: handlers
# Descripción: Aquí se declararán los handlers a las distintas llamadas de la API.
import re
import const
import utils
import random
from math import ceil
from lang import get_lang
from telegram import ParseMode, InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle,\
InputTextMessageContent, ForceReply, Bot, Update, ChatAction
from telegram.ext import run_async
from telegram.error import Unauthorized, BadRequest
from group import Group
from group_manager import group_manager
from user_manager import user_manager
from transaction_manager import transaction_manager
UPPNT = const.USERS_PER_PAGE_NEW_TRANSACTION
re_amount_comment = re.compile(const.RE_AMOUNT_COMMENT_PATTERN)
def _history_transactions_buttons(transaction_list, page):
type_to_symbol = {"purchase": "🛒", "transfer": "💸", "debt": "📝"}
transaction_list = list(transaction_list)
transaction_list.reverse()
keyboard = []
t_min = page * const.TRANSACTIONS_PER_PAGE_HISTORY
t_max = (page + 1) * const.TRANSACTIONS_PER_PAGE_HISTORY
if t_max > len(transaction_list):
t_max = len(transaction_list)
for transaction in transaction_list[t_min:t_max]:
button_text = type_to_symbol[transaction.type] + "%s💰 %s\n" % (transaction.amount,
transaction.comment[:20])
keyboard.append([InlineKeyboardButton(button_text, callback_data="tr*%s*%s" % (transaction.id, page))])
if len(transaction_list) > const.TRANSACTIONS_PER_PAGE_HISTORY:
last_page = int(ceil(len(transaction_list) / const.TRANSACTIONS_PER_PAGE_HISTORY)) - 1
prev_page = 0 if page <= 0 else page - 1
next_page = last_page if page >= last_page else page + 1
keyboard.append([InlineKeyboardButton("⏪", callback_data="hi*0" if page != 0 else "none"),
InlineKeyboardButton("⬅️", callback_data=("hi*%s" % prev_page if prev_page != page else "none")),
InlineKeyboardButton("#️⃣", callback_data="none*%s" % page),
InlineKeyboardButton("➡️", callback_data=("hi*%s" % next_page) if page != next_page else "none"),
InlineKeyboardButton("⏩", callback_data=("hi*%s" % last_page) if page != last_page else "none")])
return keyboard
def _check_pm_ready(bot, update, lang):
try:
bot.send_chat_action(update.effective_user.id, ChatAction.TYPING)
return True
except (Unauthorized, BadRequest):
update.callback_query.answer(lang.get_text("first_pm_the_bot", bot_username=const.aux.bot_username),
show_alert=True)
return False
def _random_fact(group: Group, lang):
available_facts = {"fact_0": {"amount": Group.total_spent},
"fact_1": {"amount": Group.total_transferred},
"fact_2": {"_all": Group.find_most_expensive_purchase},
"fact_3": {"name_simple": Group.find_user_in_most_purchases_as_participant},
"fact_4": {"name_simple": Group.find_user_in_most_purchases_as_buyer}}
while available_facts:
chosen_fact = random.choice([k for k in available_facts])
atributes = {}
is_available = True
for k in available_facts[chosen_fact]:
if k != "_all":
atributes[k] = available_facts[chosen_fact][k](group)
if atributes[k] is False:
is_available = False
break
else:
result = available_facts[chosen_fact][k](group)
if result is False:
is_available = False
break
atributes.update(result)
if is_available:
break
else:
available_facts.pop(chosen_fact)
if available_facts:
return lang.get_text(chosen_fact, **atributes)
else:
return ""
def generic_message(bot, update, text_code):
# Responde a cualquier mensaje con un texto genérico, sin añadiduras.
message = update.effective_message
user = update.effective_user
user_lang_code = user.language_code
lang = get_lang(user_lang_code)
message.reply_text(lang.get_text(text_code), parse_mode=ParseMode.MARKDOWN, reply=False)
# The basics
def start(bot, update, args):
# Responde al comando "/start"
if args:
if args[0] == "help":
help(bot, update)
return
generic_message(bot, update, "start")
def help(bot, update):
# Responde al comando "/help"
generic_message(bot, update, "help")
def more(bot, update):
# Responde al comando "/more"
generic_message(bot, update, "more")
def donate(bot, update):
# Responde al comando "/donate"
generic_message(bot, update, "donate")
def error_handler(bot, update, telegram_error):
bot.send_message(const.VETU_ID, "ERROR:\nUpdate:\n" + str(update) + "\ntelegram_error:\n" + str(telegram_error))
# Bot Commands
def add(bot, update, args, chat_data, user_data):
# Pide más datos o crea una transacción con los datos proporcionados.
group = group_manager.get_group(update.effective_chat, chat_data)
group.add_telegram_user(update.effective_user)
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
if args:
if re_amount_comment.match(utils.join_unicode_list(args, " ")):
select_transaction_type_group(bot, update, user_data)
return
update.effective_message.reply_text(lang.get_text("add_message"),
parse_mode=ParseMode.MARKDOWN,
reply_markup=ForceReply(selective=True))
@run_async
def split(bot: Bot, update, chat_data, user_data):
"""/split command response."""
group = group_manager.get_group(update.effective_chat, chat_data)
group.add_telegram_user(update.effective_user)
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
fact = _random_fact(group, lang)
# Phase 1, calculate ledger
our_message = update.effective_message.reply_text(lang.get_text("split_phase_1_calculating", fact=fact),
parse_mode=ParseMode.MARKDOWN)
bot.send_chat_action(update.effective_chat.id, ChatAction.TYPING)
group.calculate_ledger()
ledger_text = ""
for member_id in group.ledger:
ledger_text += "" + user_manager.get_user_by_id(member_id).full_name + \
" %.2f 💰\n" % group.ledger[member_id]
# Phase 2, calculate movements
our_message.edit_text(lang.get_text("split_phase_2_calculating", ledger=ledger_text, fact=fact),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
bot.send_chat_action(update.effective_chat.id, ChatAction.TYPING)
best_movents = group.calculate_best_movements()
movements_text = ""
for movement in best_movents:
movements_text += user_manager.get_user_by_id(movement[0]).full_name + " ----> " + \
user_manager.get_user_by_id(movement[1]).full_name + " %.2f 💰\n" % abs(movement[2])
keyboard = [[InlineKeyboardButton(lang.get_text("presentarse"), callback_data="hi_group")]]
our_message.edit_text(lang.get_text("split_results",
ledger=ledger_text,
movements=movements_text,
fact=fact),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(keyboard))
def introduce(bot, update: Update, chat_data, user_data):
"""This command is for intrdoucing new people to the bot. Basically, to make sure they are added to the group,
so they can be selected when creating a transaction. User should use this command replying the message
of other user."""
group = group_manager.get_group(update.effective_chat, chat_data)
group.add_telegram_user(update.effective_user)
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
keyboard = [[InlineKeyboardButton(lang.get_text("presentarse"), callback_data="hi_group")]]
if update.effective_message.reply_to_message is None:
update.effective_message.reply_text(lang.get_text("introduce_error_need_reply"),
quote=False,
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(keyboard))
return
target_user = group.add_telegram_user(update.effective_message.reply_to_message.from_user)
update.effective_message.reply_text(lang.get_text("introduce", target_user_name=target_user.full_name),
quote=False,
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(keyboard))
def history_group(bot, update: Update, chat_data, user_data):
"""/history command. resumes the last transactions."""
group = group_manager.get_group(update.effective_chat, chat_data)
group.add_telegram_user(update.effective_user)
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
# Page can only be known if this is a inline_callback, else this is the first message.
if update.callback_query is not None:
page = int(update.callback_query.data.split("*")[1])
method = update.effective_message.edit_text
else:
page = 0
method = update.effective_message.reply_text
keyboard = _history_transactions_buttons(group.transaction_list, page)
keyboard.append([InlineKeyboardButton(lang.get_text("presentarse"), callback_data="hi_group")])
if len(group.transaction_list) > 0:
method(text=lang.get_text("history_group"),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(keyboard))
return
else:
keyboard = [[InlineKeyboardButton(lang.get_text("presentarse"), callback_data="hi_group")]]
method(text=lang.get_text("history_empty"),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(keyboard))
def force_save(bot, update):
if int(update.effective_user.id) != const.VETU_ID:
return
user_manager.save()
transaction_manager.save()
group_manager.save()
update.effective_message.reply_text("Guardado.")
def auto_save(*args, **kwargs):
"""Called with the interval defined on bot.py, or when the bot is stopping"""
user_manager.save()
transaction_manager.save()
group_manager.save()
# Other Messages
def new_members(bot, update, chat_data):
# Cuando se añaden nuevos usuarios al grupo los añade a la instancia del grupo correspondiente. Si se ha añadido
# al bot enviará un mensaje presentandose.
t_users = update.effective_message.new_chat_members
bot_id = const.aux.bot_id
new_group = False
group = group_manager.get_group(update.effective_chat, chat_data)
group.add_telegram_user(update.effective_user)
lang = get_lang(group.lang)
for user in t_users:
if user.id == bot_id:
new_group = True
else:
group.add_telegram_user(user)
if new_group:
keyboard = [[InlineKeyboardButton(lang.get_text("presentarse"), callback_data="hi_group")]]
update.effective_message.reply_text(lang.get_text("new_group"),
reply_markup=InlineKeyboardMarkup(keyboard),
reply=False,
parse_mode=ParseMode.MARKDOWN)
def left_chat_members(bot, update, chat_data):
bot_id = const.aux.bot_id
group = group_manager.get_group(update.effective_chat, chat_data)
group.add_telegram_user(update.effective_user)
if update.effective_message.left_chat_member.id != bot_id:
group.remove_telegram_user(update.effective_message.left_chat_member)
def select_transaction_type_group(bot, update, user_data):
# Este método se ejecutará cuando el bot reciba un mensaje con el formato "<cantidad> <comentario>", que
# deberia ser una contestación al mensaje de new_members, cuando reciba el comando /add.
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
txt = update.effective_message.text
if "/add" in txt:
txt = utils.join_unicode_list(txt.split()[1:], space=" ")
amount = txt.split()[0]
comment = txt.replace(amount + " ", "")
amount = float(amount.replace(",", "."))
keyboard = [[InlineKeyboardButton(lang.get_text("purchase"), callback_data=("n_pur*%s*%s" % (amount,
comment))[:64]),
InlineKeyboardButton(lang.get_text("transfer"), callback_data=("n_tra*%s*%s" % (amount,
comment))[:64]),
InlineKeyboardButton(lang.get_text("debt"), callback_data=("n_dbt*%s*%s" % (amount, comment))[:64])],
[InlineKeyboardButton(lang.get_text("presentarse"), callback_data="hi_group")]]
update.effective_message.reply_text(lang.get_text("select_transaction_type_message",
amount=amount,
comment=comment),
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(keyboard))
def select_transaction_type_pm(bot, update, user_data):
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
# We need to know what group is refering the user, if we don't know this may be an error due to a server restart.
if "editing" not in user_data:
return
txt = update.effective_message.text
if "/add" in txt:
txt = txt.replace("/add ", "")
amount = txt.split()[0]
comment = txt.replace(str(amount) + " ", "")
amount = float(amount.replace(",", "."))
keyboard = [[InlineKeyboardButton(lang.get_text("purchase"), callback_data=("n_pur*%s*%s" % (amount,
comment))[:64]),
InlineKeyboardButton(lang.get_text("transfer"), callback_data=("n_tra*%s*%s" % (amount,
comment))[:64]),
InlineKeyboardButton(lang.get_text("debt"), callback_data=("n_dbt*%s*%s" % (amount, comment))[:64])]]
update.effective_message.reply_text(lang.get_text("select_transaction_type_message",
amount=amount,
comment=comment),
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(keyboard))
def message(bot, update, chat_data):
# Se ejecuta cuando recibe un mensaje cualquiera que no haya pasado los filtros, sólo para debugging.
pass
# print(update.effective_message.text)
# Inline buttons
def none(bot, update):
# Para los botones que no debería hacer nada o simplemente mostrar un texto en pantalla.
text = update.callback_query.data.split("*")[1] if len(update.callback_query.data.split("*")) > 1 else ""
update.callback_query.answer(text)
def hi_button(bot, update, chat_data, user_data):
# Para el botón en el que los usuarios se presentan al bot para ser añadidos al grupo.
group = group_manager.get_group(update.effective_chat, chat_data)
user = user_manager.get_user(update.effective_user, user_data)
group.add_telegram_user(update.effective_user)
lang = get_lang(user.language_code)
update.callback_query.answer(lang.get_text("hi",
user_full_name_simple=user.full_name_simple,
group_name=update.effective_chat.title))
def transaction_info(bot, update: Update, chat_data, user_data):
group = group_manager.get_group(update.effective_chat, chat_data)
group.add_telegram_user(update.effective_user)
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
transaction_id, page = update.callback_query.data.split("*")[1:]
transaction = transaction_manager.get_transaction_by_id(transaction_id)
page = int(page)
if transaction.type == "purchase":
participants_text = lang.enum([user_manager.get_user_by_id(x).full_name for x in transaction.participants])
text = lang.get_text("purchase_info",
comment=transaction.comment,
amount=transaction.amount,
buyer=user_manager.get_user_by_id(transaction.buyer).full_name,
participants=participants_text)
elif transaction.type == "transfer":
text = lang.get_text("transfer_info",
comment=transaction.comment,
amount=transaction.amount,
payer=user_manager.get_user_by_id(transaction.payer).full_name,
receiver=user_manager.get_user_by_id(transaction.receiver).full_name)
else:
text = lang.get_text("debt_info",
comment=transaction.comment,
amount=transaction.amount,
lender=user_manager.get_user_by_id(transaction.lender).full_name,
debtor=user_manager.get_user_by_id(transaction.debtor).full_name)
keyboard = [[InlineKeyboardButton(lang.get_text("go_back"), callback_data="hi*%s" % page),
InlineKeyboardButton(lang.get_text("delete"), callback_data="del_tr*%s" % transaction_id)]]
update.effective_message.edit_text(text,
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(keyboard))
def delete_transaction(bot, update, user_data, chat_data):
group = group_manager.get_group(update.effective_chat, chat_data)
group.add_telegram_user(update.effective_user)
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
transaction_id = update.callback_query.data.split("*")[1]
transaction = transaction_manager.get_transaction_by_id(transaction_id)
group_manager.get_group_by_id(transaction.group_id).remove_transaction(transaction_id)
if transaction.type == "purchase":
user_manager.get_user_by_id(transaction.buyer).remove_transaction(transaction_id)
for user_id in transaction.participants:
user_manager.get_user_by_id(user_id).remove_transaction(transaction_id)
elif transaction.type == "transfer":
user_manager.get_user_by_id(transaction.payer).remove_transaction(transaction_id)
user_manager.get_user_by_id(transaction.receiver).remove_transaction(transaction_id)
else:
user_manager.get_user_by_id(transaction.lender).remove_transaction(transaction_id)
user_manager.get_user_by_id(transaction.debtor).remove_transaction(transaction_id)
transaction_manager.remove_transaction(transaction_id)
keyboard = [[InlineKeyboardButton(lang.get_text("go_back"), callback_data="hi*0")]]
update.effective_message.edit_text(lang.get_text("transaction_deleted"),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(keyboard))
def new_purchase(bot: Bot, update, chat_data, user_data):
# Para el botón de añadir una compra, mostrado en el mensaje de saludo (new_members) y en otros mensajes.
if update.effective_chat.type == "private":
# If "editing" it's not in user_data, we're here by an error we can't solve.
if "editing" not in user_data:
update.effective_message.edit_reply_markup(InlineKeyboardMarkup([[]]))
return
group = group_manager.get_group_by_id(user_data["editing"])
update.effective_message.delete()
else:
group = group_manager.get_group(update.effective_chat, chat_data)
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
amount, comment = data.split("*")[1:]
if not _check_pm_ready(bot, update, lang):
return
purchase = transaction_manager.add_transaction(transaction_type="purchase",
amount=float(amount),
comment=comment,
buyer=user.id,
participants=[x.id for x in group.user_list],
group_id=group.id)
group.add_telegram_user(update.effective_user)
# Mensaje al grupo
if update.effective_chat.type != "private":
keyboard = [[InlineKeyboardButton(lang.get_text("goto_pm"), url="t.me/%s" % const.aux.bot_username)],
[InlineKeyboardButton(lang.get_text("presentarse"), callback_data="hi_group")]]
update.effective_message.edit_text(lang.get_text("goto_pm_message", bot_username=const.aux.bot_username),
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(keyboard),
disable_web_page_preview=True)
# Mensaje PM
keyboard = []
for participant in group.user_list[:5]:
text = "⚪️ " if participant.id != purchase.buyer else "🔘 ️"
keyboard.append([InlineKeyboardButton(text + participant.full_name_simple,
callback_data="n_pur_bu_sel*%d*0*%s" % (participant.id,
purchase.id))])
if len(group.user_list) > UPPNT:
keyboard.append([InlineKeyboardButton("⏪", callback_data="n_pur_bu_p*0*%s" % purchase.id),
InlineKeyboardButton("⬅️", callback_data="n_pur_bu_p*0*%s" % purchase.id),
InlineKeyboardButton("0️⃣", callback_data="none*%s" % lang.get_text("page", page=0)),
InlineKeyboardButton("➡️", callback_data="n_pur_bu_p*1*%s" % purchase.id),
InlineKeyboardButton("⏩", callback_data="n_pur_bu_p*%d*%s" %
(int(ceil(len(group.user_list) / 5.0)), purchase.id))])
keyboard.append([InlineKeyboardButton(lang.get_text("confirm"),
callback_data="n_pur_pa_p*0*%s" % purchase.id),
InlineKeyboardButton(lang.get_text("cancel"),
callback_data="n_trc_c*%s" % purchase.id)])
bot.send_message(chat_id=user.id,
text=lang.get_text("select_buyer", buyer=user_manager.get_user_by_id(purchase.buyer).full_name),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
def new_purchase_buyer(bot, update, user_data):
# Para seleccionar el comprador de la transacción que se señala en callba_query.data
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
cmd = data.split("*")[0]
if cmd == "n_pur_bu_p":
page, transaction_id = data.split("*")[1:]
purchase = transaction_manager.get_transaction_by_id(transaction_id)
elif cmd == "n_pur_bu_sel":
buyer_id, page, transaction_id = data.split("*")[1:]
purchase = transaction_manager.get_transaction_by_id(transaction_id)
if purchase.buyer == int(buyer_id):
update.callback_query.answer()
return
purchase.set_buyer(int(buyer_id))
else:
print("wtf es %s" % cmd)
return
page = int(page)
group = group_manager.get_group_by_id(purchase.group_id)
keyboard = []
for member in group.user_list[UPPNT * page:UPPNT + UPPNT * page]:
text = "⚪️ " if member.id != purchase.buyer else "🔘 ️"
keyboard.append([InlineKeyboardButton(text + member.full_name_simple,
callback_data="n_pur_bu_sel*%d*%d*%s" % (member.id,
page,
purchase.id))])
if len(group.user_list) > UPPNT:
last_page = int(ceil(len(group.user_list) / UPPNT)) - 1
next_page = last_page if page >= last_page else page + 1
keyboard.append([InlineKeyboardButton("⏪", callback_data="n_pur_bu_p*0*%s" % purchase.id),
InlineKeyboardButton("⬅️", callback_data="n_pur_bu_p*%d*%s" % (0 if page <= 0 else page - 1,
purchase.id)),
InlineKeyboardButton("0️⃣", callback_data="none*%s" % lang.get_text("page", page=page)),
InlineKeyboardButton("➡️", callback_data="n_pur_bu_p*%d*%s" % (next_page, purchase.id)),
InlineKeyboardButton("⏩", callback_data="n_pur_bu_p*%d*%s" % (last_page, purchase.id))])
keyboard.append([InlineKeyboardButton(lang.get_text("confirm"),
callback_data="n_pur_pa_p*0*%s" % purchase.id),
InlineKeyboardButton(lang.get_text("cancel"),
callback_data="n_trc_c*%s" % purchase.id)])
update.effective_message.edit_text(text=lang.get_text("select_buyer",
buyer=user_manager.get_user_by_id(purchase.buyer).full_name),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
def new_purchase_participants(bot, update, user_data):
# Para seleccionar los participantes de la compra señalada en callback_query.data
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
cmd = data.split("*")[0]
if cmd == "n_pur_pa_p":
page, transaction_id = data.split("*")[1:]
purchase = transaction_manager.get_transaction_by_id(transaction_id)
elif cmd == "n_pur_pa_sel":
participant_id, page, transaction_id = data.split("*")[1:]
purchase = transaction_manager.get_transaction_by_id(transaction_id)
purchase.add_remove_participant(int(participant_id))
else:
print("wtf es %s" % cmd)
return
page = int(page)
group = group_manager.get_group_by_id(purchase.group_id)
keyboard = []
for member in group.user_list[UPPNT * page:UPPNT + UPPNT * page]:
text = "❎ " if member.id not in purchase.participants else "☑️ "
keyboard.append([InlineKeyboardButton(text + member.full_name_simple,
callback_data="n_pur_pa_sel*%d*%d*%s" % (member.id,
page,
purchase.id))])
if len(group.user_list) > UPPNT:
last_page = int(ceil(len(group.user_list) / UPPNT)) - 1
next_page = last_page if page >= last_page else page + 1
keyboard.append([InlineKeyboardButton("⏪", callback_data="n_pur_pa_p*0*%s" % purchase.id),
InlineKeyboardButton("⬅️", callback_data="n_pur_pa_p*%d*%s" % (0 if page <= 0 else page - 1,
purchase.id)),
InlineKeyboardButton("0️⃣", callback_data="none*%s" % lang.get_text("page", page=page)),
InlineKeyboardButton("➡️", callback_data="n_pur_pa_p*%d*%s" % (next_page, purchase.id)),
InlineKeyboardButton("⏩", callback_data="n_pur_pa_p*%d*%s" % (last_page, purchase.id))])
keyboard.append([InlineKeyboardButton(lang.get_text("confirm"),
callback_data="n_pur_res*%s" % purchase.id),
InlineKeyboardButton(lang.get_text("cancel"),
callback_data="n_trc_c*%s" % purchase.id)])
participant_name_list = [user_manager.get_user_by_id(x).full_name for x in purchase.participants]
update.effective_message.edit_text(text=lang.get_text("select_participants",
buyer=user_manager.get_user_by_id(purchase.buyer).full_name,
participants=lang.enum(participant_name_list)),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
# Resume with the details of the just added purchase.
def new_purchase_resume(bot, update, user_data):
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
purchase_id = data.split("*")[1]
purchase = transaction_manager.get_transaction_by_id(purchase_id)
buyer = user_manager.get_user_by_id(purchase.buyer)
participants = [user_manager.get_user_by_id(x) for x in purchase.participants]
participants_text = lang.enum([x.full_name for x in participants])
if not participants:
update.callback_query.answer(lang.get_text("new_purchase_resume_error_not_0"))
return
group_manager.get_group_by_id(purchase.group_id).add_transaction(purchase)
buyer.add_transaction(purchase)
for user_id in purchase.participants:
user_manager.get_user_by_id(user_id).add_transaction(purchase)
keyboard = [[]]
group_title = group_manager.get_group_by_id(purchase.group_id).title
update.effective_message.edit_text(text=lang.get_text("new_purchase_resume",
amount=purchase.amount,
comment=purchase.comment,
buyer=buyer.full_name,
participants=participants_text,
title=group_title),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
user_data["editing"] = purchase.group_id
def new_transfer(bot, update, chat_data, user_data):
"""Transaction type selected as transfer by the user. Now it haves to select payer."""
if update.effective_chat.type == "private":
# If "editing" it's not in user_data, we're here by an error we can't solve.
if "editing" not in user_data:
update.effective_message.edit_reply_markup(InlineKeyboardMarkup([[]]))
return
group = group_manager.get_group_by_id(user_data["editing"])
update.effective_message.delete()
else:
group = group_manager.get_group(update.effective_chat, chat_data)
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
amount, comment = data.split("*")[1:]
if not _check_pm_ready(bot, update, lang):
return
transfer = transaction_manager.add_transaction(transaction_type="transfer",
amount=float(amount),
comment=comment,
payer=user.id,
reciver=None,
group_id=group.id)
group.add_telegram_user(update.effective_user)
# Group message
if update.effective_chat.type != "private":
keyboard = [[InlineKeyboardButton(lang.get_text("goto_pm"), url="t.me/%s" % const.aux.bot_username)],
[InlineKeyboardButton(lang.get_text("presentarse"), callback_data="hi_group")]]
update.effective_message.edit_text(lang.get_text("goto_pm_message", bot_username=const.aux.bot_username),
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(keyboard),
disable_web_page_preview=True)
# Private message
keyboard = []
for participant in group.user_list[:UPPNT]:
text = "⚪️ " if participant.id != transfer.payer else "🔘 ️"
keyboard.append([InlineKeyboardButton(text + participant.full_name_simple,
callback_data="n_tra_pa_sel*%d*0*%s" % (participant.id,
transfer.id))])
if len(group.user_list) > UPPNT:
keyboard.append([InlineKeyboardButton("⏪", callback_data="n_tra_pa_p*0*%s" % transfer.id),
InlineKeyboardButton("⬅️", callback_data="n_tra_pa_p*0*%s" % transfer.id),
InlineKeyboardButton("0️⃣", callback_data="none*%s" % lang.get_text("page", page=0)),
InlineKeyboardButton("➡️", callback_data="n_tra_pa_p*1*%s" % transfer.id),
InlineKeyboardButton("⏩", callback_data="n_tra_pa_p*%d*%s" %
(int(ceil(len(group.user_list) / 5.0)), transfer.id))])
keyboard.append([InlineKeyboardButton(lang.get_text("confirm"),
callback_data="n_tra_re_p*0*%s" % transfer.id),
InlineKeyboardButton(lang.get_text("cancel"),
callback_data="n_trc_c*%s" % transfer.id)])
bot.send_message(chat_id=user.id,
text=lang.get_text("select_payer", payer=user_manager.get_user_by_id(transfer.payer).full_name),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
def new_transfer_payer(bot, update, user_data):
"""Payer or page selected on the new_transfer_payer menu"""
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
cmd = data.split("*")[0]
if cmd == "n_tra_pa_p":
page, transaction_id = data.split("*")[1:]
transfer = transaction_manager.get_transaction_by_id(transaction_id)
elif cmd == "n_tra_pa_sel":
payer_id, page, transaction_id = data.split("*")[1:]
transfer = transaction_manager.get_transaction_by_id(transaction_id)
if transfer.payer == int(payer_id):
update.callback_query.answer()
return
transfer.set_payer(int(payer_id))
else:
print("wtf es %s" % cmd)
return
page = int(page)
group = group_manager.get_group_by_id(transfer.group_id)
keyboard = []
for member in group.user_list[UPPNT * page:UPPNT + UPPNT * page]:
last_page = int(ceil(len(group.user_list) / UPPNT)) - 1
next_page = last_page if page >= last_page else page + 1
text = "⚪️ " if member.id != transfer.payer else "🔘 ️"
keyboard.append([InlineKeyboardButton(text + member.full_name_simple,
callback_data="n_tra_pa_sel*%d*%d*%s" % (member.id,
page,
transfer.id))])
if len(group.user_list) > UPPNT:
keyboard.append([InlineKeyboardButton("⏪", callback_data="n_tra_pa_p*0*%s" % transfer.id),
InlineKeyboardButton("⬅️", callback_data="n_tra_pa_p*%d*%s" % (0 if page <= 0 else page - 1,
transfer.id)),
InlineKeyboardButton("0️⃣", callback_data="none*%s" % lang.get_text("page", page=page)),
InlineKeyboardButton("➡️", callback_data="n_tra_pa_p*%d*%s" % (next_page, transfer.id)),
InlineKeyboardButton("⏩", callback_data="n_tra_pa_p*%d*%s" % (last_page, transfer.id))])
keyboard.append([InlineKeyboardButton(lang.get_text("confirm"),
callback_data="n_tra_re_p*0*%s" % transfer.id),
InlineKeyboardButton(lang.get_text("cancel"),
callback_data="n_trc_c*%s" % transfer.id)])
update.effective_message.edit_text(text=lang.get_text("select_payer",
payer=user_manager.get_user_by_id(transfer.payer).full_name),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
def new_transfer_receiver(bot, update, user_data):
"""After selecting a payer fore the transfer, the user must select a receiver. This is where that shit happens."""
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
cmd = data.split("*")[0]
if cmd == "n_tra_re_p":
page, transaction_id = data.split("*")[1:]
transfer = transaction_manager.get_transaction_by_id(transaction_id)
elif cmd == "n_tra_re_sel":
receiver_id, page, transaction_id = data.split("*")[1:]
transfer = transaction_manager.get_transaction_by_id(transaction_id)
transfer.set_receiver(int(receiver_id))
else:
print("wtf es %s" % cmd)
return
page = int(page)
group = group_manager.get_group_by_id(transfer.group_id)
keyboard = []
for member in group.user_list[5 * page:5 + 5 * page]:
text = "⚪️" if member.id != transfer.receiver else "🔘 ️"
keyboard.append([InlineKeyboardButton(text + member.full_name_simple,
callback_data="n_tra_re_sel*%d*%d*%s" % (member.id,
page,
transfer.id))])
if len(group.user_list) > UPPNT:
last_page = int(ceil(len(group.user_list) / 5.0)) - 1
next_page = last_page if page >= last_page else page + 1
keyboard.append([InlineKeyboardButton("⏪", callback_data="n_tra_re_p*0*%s" % transfer.id),
InlineKeyboardButton("⬅️", callback_data="n_tra_re_p*%d*%s" % (0 if page <= 0 else page - 1,
transfer.id)),
InlineKeyboardButton("0️⃣", callback_data="none*%s" % lang.get_text("page", page=page)),
InlineKeyboardButton("➡️", callback_data="n_tra_re_p*%d*%s" % (next_page, transfer.id)),
InlineKeyboardButton("⏩", callback_data="n_tra_re_p*%d*%s" % (last_page, transfer.id))])
keyboard.append([InlineKeyboardButton(lang.get_text("confirm"),
callback_data="n_tra_res*%s" % transfer.id),
InlineKeyboardButton(lang.get_text("cancel"),
callback_data="n_trc_c*%s" % transfer.id)])
receiver = user_manager.get_user_by_id(transfer.receiver).full_name if transfer.receiver is not None else ""
update.effective_message.edit_text(text=lang.get_text("select_receiver",
amount=transfer.amount,
payer=user_manager.get_user_by_id(transfer.payer).full_name,
receiver=receiver),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
def new_transfer_resume(bot, update, user_data):
"""Final message of the new_transfer series. A message resuming the new created transfer."""
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
transfer_id = data.split("*")[1]
transfer = transaction_manager.get_transaction_by_id(transfer_id)
payer = user_manager.get_user_by_id(transfer.payer)
receiver = user_manager.get_user_by_id(transfer.receiver)
if not receiver:
update.callback_query.answer(lang.get_text("new_transfer_resume_error_need_receiver"))
return
group_manager.get_group_by_id(transfer.group_id).add_transaction(transfer)
payer.add_transaction(transfer)
receiver.add_transaction(transfer)
keyboard = [[]]
update.effective_message.edit_text(text=lang.get_text("new_transfer_resume",
amount=transfer.amount,
comment=transfer.comment,
payer=payer.full_name,
receiver=receiver.full_name,
title=group_manager.get_group_by_id(transfer.group_id).title),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
user_data["editing"] = transfer.group_id
def new_debt(bot, update, chat_data, user_data):
"""Transaction type selected as debt by the user. Now it haves to select payer."""
if update.effective_chat.type == "private":
# If "editing" it's not in user_data, we're here by an error we can't solve.
if "editing" not in user_data:
update.effective_message.edit_reply_markup(reply_markup=InlineKeyboardMarkup([[]]))
return
group = group_manager.get_group_by_id(user_data["editing"])
update.effective_message.delete()
else:
group = group_manager.get_group(update.effective_chat, chat_data)
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
amount, comment = data.split("*")[1:]
if not _check_pm_ready(bot, update, lang):
return
debt = transaction_manager.add_transaction(transaction_type="debt",
amount=float(amount),
comment=comment,
lender=user.id,
debtor=None,
group_id=group.id)
group.add_telegram_user(update.effective_user)
# Group message
if update.effective_chat.type != "private":
keyboard = [[InlineKeyboardButton(lang.get_text("goto_pm"), url="t.me/%s" % const.aux.bot_username)],
[InlineKeyboardButton(lang.get_text("presentarse"), callback_data="hi_group")]]
update.effective_message.edit_text(lang.get_text("goto_pm_message", bot_username=const.aux.bot_username),
parse_mode=ParseMode.MARKDOWN,
reply_markup=InlineKeyboardMarkup(keyboard),
disable_web_page_preview=True)
# Private message
keyboard = []
for member in group.user_list[:UPPNT]:
text = "⚪️ " if member.id != debt.lender else "🔘 ️"
keyboard.append([InlineKeyboardButton(text + member.full_name_simple,
callback_data="n_dbt_le_sel*%d*0*%s" % (member.id,
debt.id))])
if len(group.user_list) > UPPNT:
keyboard.append([InlineKeyboardButton("⏪", callback_data="n_dbt_le_p*0*%s" % debt.id),
InlineKeyboardButton("⬅️", callback_data="n_dbt_le_p*0*%s" % debt.id),
InlineKeyboardButton("0️⃣", callback_data="none*%s" % lang.get_text("page", page=0)),
InlineKeyboardButton("➡️", callback_data="n_dbt_le_p*1*%s" % debt.id),
InlineKeyboardButton("⏩", callback_data="n_dbt_le_p*%d*%s" %
(int(ceil(len(group.user_list) / 5.0)), debt.id))])
keyboard.append([InlineKeyboardButton(lang.get_text("confirm"),
callback_data="n_dbt_de_p*0*%s" % debt.id),
InlineKeyboardButton(lang.get_text("cancel"),
callback_data="n_trc_c*%s" % debt.id)])
bot.send_message(chat_id=user.id,
text=lang.get_text("select_lender",
amount=debt.amount,
lender=user_manager.get_user_by_id(debt.lender).full_name),
reply_markup=InlineKeyboardMarkup(keyboard),
parse_mode=ParseMode.MARKDOWN,
disable_web_page_preview=True)
def new_debt_lender(bot, update, user_data):
"""First phase of the debt creation. In this message the user must select a lender."""
user = user_manager.get_user(update.effective_user, user_data)
lang = get_lang(user.language_code)
data = update.callback_query.data
cmd = data.split("*")[0]
if cmd == "n_dbt_le_p":
page, transaction_id = data.split("*")[1:]
debt = transaction_manager.get_transaction_by_id(transaction_id)
elif cmd == "n_dbt_le_sel":
lender_id, page, transaction_id = data.split("*")[1:]
debt = transaction_manager.get_transaction_by_id(transaction_id)
if debt.lender == int(lender_id):
update.callback_query.answer()
return
debt.set_lender(int(lender_id))
else:
print("wtf es %s" % cmd)
return
page = int(page)
group = group_manager.get_group_by_id(debt.group_id)
last_page = int(ceil(len(group.user_list) / UPPNT)) - 1
next_page = last_page if page >= last_page else page + 1
keyboard = []
for member in group.user_list[5 * page:5 + 5 * page]:
text = "⚪️ " if member.id != debt.lender else "🔘 ️"
keyboard.append([InlineKeyboardButton(text + member.full_name_simple,
callback_data="n_dbt_le_sel*%d*%d*%s" % (member.id,
page,
debt.id))])