-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlclbitcoinsTelegramBot.py
More file actions
1148 lines (1075 loc) Β· 60.2 KB
/
lclbitcoinsTelegramBot.py
File metadata and controls
1148 lines (1075 loc) Β· 60.2 KB
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
from telegram import Update, ParseMode, ReplyKeyboardMarkup, ReplyKeyboardRemove,\
InlineKeyboardMarkup, InlineKeyboardButton
from telegram.error import NetworkError
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters,
ConversationHandler, PicklePersistence, CallbackContext, CallbackQueryHandler)
from localbits.tokens import telegramBotToken, telegramChatID, online_buy, online_sell, myUserName,\
ruslanSberCardMessage, momSberCardMessage, ayratSberCardMessage
from localbits.localbitcoins import LocalBitcoin
import re, time
from typing import Union
class TelegramBot:
MENU_CHOOSING_OPTION, CHANGING_ADS, CHANGING_WORKS = range(3)
"""
On object initialization pass telegram TOKEN and ChatID
"""
def __init__(self, TOKEN : str, chatID : str, localBitcoinObject : LocalBitcoin):
self.token = TOKEN
self.chatID = chatID
self.updater = Updater(self.token)
self.dispatcher = self.updater.dispatcher
self.localBitcoinObject = localBitcoinObject
#This info strongly connected with bot's implementation
self.worksDictionary = {
'sell' : {'status' : False, 'sellBorder' : None, 'cardOwner' : {'name' : None, 'cardMessage' : None}},
'buy' : {'status' : False, 'buyDifference' : None},
'scan' : {'status' : False}
}
self.CARDNAMES = ['Ruslan', 'Mom', 'Ayrat']
"""
Dictionary with contacts' info
"""
self.contactsDictionary = {}
"""
Dictionary with different banks' ads' list of dicts, adType and time of receiving this ads
Example {'sberbank' : {'sell' : {'ads' : [adDict1, adDict2, ..., adDictN], 'time' : 2200000}}}
Main key('sberbank') is bank's name, 'sell' is ad type,
'ads' is a list of dictionaries with every ad info, 'time' is last time.time() of receiving the ads.
"""
self.recentAdsDictionary = {}
"""
Last callbackquery data, needed to get user's text message once for feedback, message sending.
"""
self.lastCallbackQuery = ''
self.lastUpdate = None
self.dispatcher.add_handler(CommandHandler('start', self.start))
self.dispatcher.add_handler(CommandHandler('help', self.help))
self.dispatcher.add_handler(CommandHandler('adsstatus', self.adsStatus))
self.dispatcher.add_handler(CommandHandler('switchad', self.switchAd))
self.dispatcher.add_handler(CommandHandler('switchwork', self.switchWork))
self.dispatcher.add_handler(CommandHandler('changeborder', self.changeBorder))
#Handler to catch message for feedback
self.feedbackMessageHandler = MessageHandler(filters = Filters.text & ~Filters.command,
callback=self.get_reputation_message_callback)
#Handler to catch message which will be sent to user
self.sendMessageHandler = MessageHandler(filters=(Filters.text | Filters.document.category('image')) & ~Filters.command,
callback=self.get_message_send_message_callback)
#Handler to catch message of digits to change ad limits.
self.getNewLimitHandler = MessageHandler(filters=Filters.regex('^\d*[.,]?\d*$') | Filters.regex('^ΠΡΠΌΠ΅Π½Π°$'),
callback=self.get_new_limit_message_callback)
#Handler to catch message of digits to input new work argument
self.getNewWorkArgumentHandler = MessageHandler(filters=Filters.regex('^\d*[.,]?\d*$') | Filters.regex('^ΠΡΠΌΠ΅Π½Π°$'),
callback=self.get_work_numberArg_manually_message)
self.dispatcher.add_handler(CallbackQueryHandler(self.getReleaseCallback, pattern='^release_\S+$'))
self.dispatcher.add_handler(CallbackQueryHandler(self.change_reputation_callback, pattern='^reputation_'))
self.dispatcher.add_handler(CallbackQueryHandler(self.send_message_callback, pattern='^message_\S+$'))
self.menuHandler = ConversationHandler(
entry_points=[
CommandHandler('menu', self.command_menu),
CallbackQueryHandler(self.command_menu_from_contact, pattern='^callback_menu_from_contact$')
],
states={
self.MENU_CHOOSING_OPTION: [
CallbackQueryHandler(self.command_menu_ads, pattern='^callback_ads$'),
CallbackQueryHandler(self.command_menu_works, pattern='^callback_works$'),
CallbackQueryHandler(self.command_menu_balance, pattern='^callback_wallet$'),
CallbackQueryHandler(self.command_menu_later, pattern='^callback_menu$'),
],
self.CHANGING_ADS: [
CallbackQueryHandler(self.ads_switch, pattern='^callback_ad_switch_\d_\d+_\d+$'),
CallbackQueryHandler(self.change_limit_callback, pattern=f'^callback_ad_changeLimit_((max)|(min))?_\d+_\d+$'),
CallbackQueryHandler(self.command_menu_later, pattern='^callback_menu$')
],
self.CHANGING_WORKS: [
CallbackQueryHandler(self.command_menu_works, pattern='(^callback_menu_works$)|(^callback_back$)'),
CallbackQueryHandler(self.work_sell_options, pattern='^callback_works_sell$'),
CallbackQueryHandler(self.work_buy_options, pattern='^callback_works_buy$'),
CallbackQueryHandler(self.work_scan_options, pattern='^callback_works_scan$'),
CallbackQueryHandler(self.command_menu_later, pattern='^callback_menu$'),
CallbackQueryHandler(self.work_switch, pattern='^callback_works_switch_\w*$'),
CallbackQueryHandler(self.change_work_numberArg_fromButton, pattern='^callback_work_\w+_\w+_[+-]\d+$'),
CallbackQueryHandler(self.change_work_numberArg_manually, pattern='^callback_work_\w+_\w+_manual$'),
CallbackQueryHandler(self.change_work_activeCard, pattern='^callback_sell_card_\w+$'),
CallbackQueryHandler(self.command_menu_works, pattern='^callback_works$'),
CallbackQueryHandler(self.command_menu_later, pattern='^callback_menu_later$')
]
},
fallbacks=[
CommandHandler('menu', self.command_menu),
CallbackQueryHandler(self.command_menu_later, pattern='^' + 'callback_menu' + '$')
])
self.dispatcher.add_handler(self.menuHandler)
self.testDict = {
'777' : {
'sentCard' : True,
'askedFIO': True,
'payment_completed' : True,
'buyerMessages' : ['Hi', 'Paid'],
'amount' : '777',
'tobe_deleted' : False
}
}
"""
Function which compares dashboard on lclbitcoins and contacts dictionary and
makes needed changes for contacts.
"""
def checkDashboardForNewContacts(self, msg, start=False):
for contact_id in list(self.contactsDictionary):
if self.contactsDictionary[contact_id]['tobe_deleted']:
print(f"Contact {contact_id} is closed, dict updated")
del self.contactsDictionary[contact_id]
dashBoard = self.localBitcoinObject.sendRequest('/api/dashboard/seller/', '', 'get')
for contact in dashBoard['contact_list']:
contact = contact['data']
contact_id = str(contact['contact_id'])
username = contact['buyer']['username']
paymentCompleted = contact['payment_completed_at'] is not None
if not contact['disputed_at']:
if start == True:
self.contactsDictionary[contact_id] = {
'username' : username,
'sentCard' : True,
'askedFIO': True,
'payment_completed' : paymentCompleted,
'buyerMessages' : [],
'amount' : contact['amount'],
'tobe_deleted' : False
}
else:
if contact_id not in self.contactsDictionary:
self.contactsDictionary[contact_id] = {
'username' : username,
'sentCard': False,
'askedFIO' : False,
'payment_completed': paymentCompleted,
'buyerMessages': [],
'amount': contact['amount'],
'tobe_deleted' : False,
}
postMessageRequest = self.localBitcoinObject.postMessageToContact(contact_id, msg)
if postMessageRequest[0] == 200:
self.contactsDictionary[contact_id]['sentCard'] = True #Changing dictionary only if message posting was succesful(code 200)
print('New contact: ', contact_id)
else:
self.contactsDictionary[contact_id]['payment_completed'] = paymentCompleted
messageReq = self.localBitcoinObject.getContactMessages(contact_id)
newMessages = messageReq['message_list']
newMessages = [msg['msg'] for msg in newMessages if msg['sender']['username'] != myUserName]
amountOfNewMessages = len(newMessages) - len(self.contactsDictionary[contact_id]['buyerMessages'])
self.contactsDictionary[contact_id]['buyerMessages'] = newMessages
if amountOfNewMessages > 0:
self.sendNewMessageFromUserMessage(contact_ID=contact_id, amountOfNewMessages=amountOfNewMessages)
if paymentCompleted:
# Get user's messages and ask for FIO if needed
if not self.contactsDictionary[contact_id]['askedFIO'] and len(
self.contactsDictionary[contact_id]['buyerMessages']) == 0:
# There could be better way of determining if user sent his name
if self.localBitcoinObject.postMessageToContact(contact_id, message='ΠΈΠ½ΠΈΡΠΈΠ°Π»Ρ?')[0] == 200:
self.contactsDictionary[contact_id][
'askedFIO'] = True # Changing dictionary only if message posting was succesful(code 200)
completedPayments = set([key for key in list(self.contactsDictionary) if self.contactsDictionary[key]['payment_completed']])
for key in completedPayments:
self.sendCompletedPaymentMessage(key)
"""
Function to send contact with 'completed_payment' == True status.
"""
def sendCompletedPaymentMessage(self, contact_ID: str):
botText = str(f"πΈ Payment completed:\n\n<i>ID{contact_ID}</i> - <b>{self.contactsDictionary[contact_ID]['amount']}</b>"
f"RUB - " + "; ".join(self.contactsDictionary[contact_ID]['buyerMessages']) + "\n")
replyMarkup = self.get_payment_keyboard(contact_ID)
if not self.contactsDictionary[contact_ID]['tobe_deleted']:
msg_ID = self.sendMessageWithConnectionCheck(chat_id=self.chatID, text=botText,
reply_markup=replyMarkup,
parse_mode=ParseMode.HTML).message_id
if self.contactsDictionary[contact_ID]['tobe_deleted']:
self.updater.bot.delete_message(chat_id=self.chatID, message_id=msg_ID)
"""
Function to get user's new message from contact and send it through telegram bot.
"""
def sendNewMessageFromUserMessage(self, contact_ID: str, amountOfNewMessages: int):
botText = f"<i>π¬ ID {contact_ID}</i> | <b>{self.contactsDictionary[contact_ID]['amount']}</b> RUB | " \
f"<b>{self.contactsDictionary[contact_ID]['username']}</b> send message(s):\n\n"
for msg in self.contactsDictionary[contact_ID]['buyerMessages'][-amountOfNewMessages:]:
botText += ('<i>' + msg + '</i>\n')
keyboard = [[]]
if self.contactsDictionary[contact_ID]['payment_completed']:
keyboard[0].append(InlineKeyboardButton("Release πΈ", callback_data=f"release_{contact_ID}"))
keyboard[0].append(InlineKeyboardButton("Message π¬", callback_data=f"message_{contact_ID}_{self.contactsDictionary[contact_ID]['username']}"))
replyMarkup = InlineKeyboardMarkup(keyboard)
self.sendMessageWithConnectionCheck(chat_id=self.chatID,
text=botText,
reply_markup=replyMarkup,
parse_mode=ParseMode.HTML)
"""
Function to catch release callback_data when user presses 'Release' button and try to release contact.
"""
def getReleaseCallback(self, update: Update, context: CallbackContext):
query = update.callback_query
contact_ID = query.data.split('_')[1]
query.edit_message_text("Releasing contact...π", reply_markup=self.get_payment_keyboard(contact_ID))
wasReleased = self.releaseContact(contact_ID)
if wasReleased:
query.edit_message_text(
f"β
<i>ID {contact_ID}</i> | <b>{self.contactsDictionary[contact_ID]['amount']}</b> RUB with "
f"user <b>{self.contactsDictionary[contact_ID]['username']}</b> released.\n\n"
f"You can leave your feedback for this user:",
reply_markup=self.get_payment_keyboard(contact_ID, wasReleased=True),
parse_mode=ParseMode.HTML)
else:
query.edit_message_text(
f"β <i>ID {contact_ID}</i> | <b>{self.contactsDictionary[contact_ID]['amount']}</b> RUB couldn't release",
reply_markup=self.get_payment_keyboard(contact_ID), parse_mode=ParseMode.HTML)
query.answer()
"""
Function to release contact using lcl API.
Bool status of releasing is returned.
"""
def releaseContact(self, contactID) -> bool:
if self.localBitcoinObject.contactRelease(contactID)[0] == 200:
self.contactsDictionary[contactID]['tobe_deleted'] = True
return True
return False
"""
Function to catch send message callback_data when user presses 'Message' button.
After, bot is expecting user to send text or image message to send it on lcl.
"""
def send_message_callback(self, update: Update, context: CallbackContext):
query = update.callback_query
self.lastCallbackQuery = query.data
query.answer()
keyboard = [['ΠΡΠΌΠ΅Π½Π°']]
markup = ReplyKeyboardMarkup(keyboard=keyboard, resize_keyboard=True)
msg = 'π¬ Input your message:'
self.dispatcher.add_handler(self.sendMessageHandler)
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id,
text=msg,
reply_markup=markup)
"""
Callback used in MessageHandler to catch user's text or image message using Filters.
Then, depending on what user has sent, either cancel sending message or send user's message on lcl.
"""
def get_message_send_message_callback(self, update: Update, context: CallbackContext):
self.dispatcher.remove_handler(self.sendMessageHandler)
messageText = update.message.text
replyMsg = ''
if messageText == 'ΠΡΠΌΠ΅Π½Π°':
replyMsg = 'β Canceled sending message.'
else:
contact_ID = self.lastCallbackQuery.split("_")[1]
username = self.lastCallbackQuery.split("_", 2)[2]
replyMsg = 'π Sending message...'
msg_ID = self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id,
text=replyMsg).message_id
if self.localBitcoinObject.postMessageToContact(contact_id=contact_ID, message=messageText)[0] == 200:
replyMsg = f"π¬ Your message was sent to user <b>{username}</b>."
else:
replyMsg = f"β For some reason message wasn\'t sent to user <b>{username}</b>."
self.updater.bot.delete_message(chat_id=update.effective_chat.id, message_id=msg_ID)
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id,
text=replyMsg,
reply_markup=ReplyKeyboardRemove(),
parse_mode=ParseMode.HTML)
"""
Function to catch send feedback callback_data when user presses one of four reputation emojis button.
Then, build a keyboard with appropriate comments, and after that, bot is waiting user to send
only text message with the aim of sending this commentary with feedback.
"""
def change_reputation_callback(self, update: Update, context: CallbackContext):
query = update.callback_query
self.lastCallbackQuery = query.data
repStatus = query.data.split("_")[1]
username = query.data.split("_", 2)[2]
button_1, button_2 = '', ''
if repStatus == 'trust':
button_1, button_2 = 'ΠΡΡΡΡΠΎ ΠΈ Π½Π°Π΄ΡΠΆΠ½ΠΎ!', 'ΠΠ°Π΄ΡΠΆΠ½ΡΠΉ ΠΏΠΎΠΊΡΠΏΠ°ΡΠ΅Π»Ρ.'
elif repStatus == 'positive':
button_1, button_2 = 'ΠΡΡ Ρ
ΠΎΡΠΎΡΠΎ!', 'Π‘Π΄Π΅Π»ΠΊΠ° ΠΏΡΠΎΡΠ»Π° Ρ
ΠΎΡΠΎΡΠΎ.'
elif repStatus == 'neutral':
button_1, button_2 = 'ΠΠΎΡΠΌΠ°Π»ΡΠ½ΠΎ', 'ΠΠ΅ΡΠ΅Π³ΠΎ ΡΠΊΠ°Π·Π°ΡΡ...'
elif repStatus == 'block':
button_1, button_2 = 'Π£ΠΆΠ°ΡΠ½Π°Ρ ΡΠ΄Π΅Π»ΠΊΠ°!', 'ΠΡΡΡΠ΅ Π±Π»ΠΎΠΊΠΈΡΡΠΉΡΠ΅.'
button_1, button_2 = button_1 + ' | ' + myUserName, button_2 + ' | ' + myUserName
keyboard = [
[button_1, button_2],
['ΠΠ΅Π· ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΡ']
]
query.answer()
markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)
icon = ''
for buttonRow in query.message['reply_markup']['inline_keyboard']:
for button in buttonRow:
if repStatus in button['callback_data']:
icon = button['text']
self.dispatcher.add_handler(self.feedbackMessageHandler)
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id,
text=icon + 'π¬' + f" Feedback comment for user <b>{username}</b>:",
reply_markup=markup,
parse_mode=ParseMode.HTML)
"""
Function to get user's text message which will be commentary for chosen feedback status.
If feedback status is block and no commentary given, block_without_feedback is sent, because
'block' status requires mandatory commentary. Check lcl's 'postFeedbackToUser' function.
"""
def get_reputation_message_callback(self, update: Update, context: CallbackContext):
self.dispatcher.remove_handler(self.feedbackMessageHandler)
repStatus = self.lastCallbackQuery.split("_")[1]
username = self.lastCallbackQuery.split("_", 2)[2]
text = update.message.text
if text == 'ΠΠ΅Π· ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΡ':
text = None
if repStatus == 'block':
repStatus = 'block_without_feedback'
else:
text += ' | QLicman'
msgText = 'Sending feedback...π'
msg_ID = self.sendMessageWithConnectionCheck(text=msgText,
chat_id=update.effective_chat.id,
reply_markup=ReplyKeyboardRemove()).message_id
if self.localBitcoinObject.postFeedbackToUser(username=username, feedback=repStatus, msg=text)[0] == 200:
msgText = f"β
Your feedback was sent to user <b>{username}</b>."
else:
msgText = f"β Couldn't send feedback to user <b>{username}</b>"
self.updater.bot.delete_message(chat_id=update.effective_chat.id, message_id=msg_ID)
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id,
text=msgText)
"""
Function to build beautiful keyboard for contacts and paid contacts.
"""
def get_payment_keyboard(self, contact_ID: str, wasReleased: bool = False) -> InlineKeyboardMarkup:
username = self.contactsDictionary[contact_ID]['username']
if not wasReleased:
keyboard = [
[
InlineKeyboardButton("Release πΈ", callback_data=f"release_{contact_ID}"),
InlineKeyboardButton("Message π¬", callback_data=f"message_{contact_ID}_{username}")
]
]
else:
keyboard = [
[
InlineKeyboardButton("π", callback_data=f"reputation_trust_{username}"),
InlineKeyboardButton("π", callback_data=f"reputation_positive_{username}"),
InlineKeyboardButton("π", callback_data=f"reputation_neutral_{username}"),
InlineKeyboardButton("π«", callback_data=f"reputation_block_{username}"),
],
[
InlineKeyboardButton("Message π¬", callback_data=f"message_{contact_ID}_{username}"),
],
[
InlineKeyboardButton("Menu β", callback_data='callback_menu_from_contact')
]
]
return InlineKeyboardMarkup(keyboard)
"""
Callback to start conversation and send menu.
"""
def command_menu(self, update: Update, context: CallbackContext) -> int:
keyboard = [
[InlineKeyboardButton('Ads', callback_data='callback_ads'),
InlineKeyboardButton('Works', callback_data='callback_works'),
InlineKeyboardButton('Wallet', callback_data='callback_wallet')]
]
replyMarkup = InlineKeyboardMarkup(keyboard)
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id,
text='Menu β',
reply_markup=replyMarkup)
return TelegramBot.MENU_CHOOSING_OPTION
"""
Callback to return to menu later in conversation.
"""
def command_menu_later(self, update: Update, context: CallbackContext) -> int:
keyboard = [
[InlineKeyboardButton('Ads', callback_data='callback_ads'),
InlineKeyboardButton('Works', callback_data='callback_works'),
InlineKeyboardButton('Wallet', callback_data='callback_wallet')]
]
replyMarkup = InlineKeyboardMarkup(keyboard)
query = update.callback_query
query.edit_message_text('Menu β', reply_markup=replyMarkup)
return TelegramBot.MENU_CHOOSING_OPTION
"""
Callback to start menu convesation from inline button.
"""
def command_menu_from_contact(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton('Ads', callback_data='callback_ads'),
InlineKeyboardButton('Works', callback_data='callback_works'),
InlineKeyboardButton('Wallet', callback_data='callback_wallet')]
]
replyMarkup = InlineKeyboardMarkup(keyboard)
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id,
text='Menu β',
reply_markup=replyMarkup)
return TelegramBot.MENU_CHOOSING_OPTION
"""
Function to get ads info from lcl and send panel with ads' options.
"""
def command_menu_ads(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
query.answer()
query.edit_message_text('π...Getting ads info...')
myAds = self.localBitcoinObject.getSeveralAds([online_buy, online_sell])
adsKeyboard = self.get_ads_status_keyboard(myAds)
replyMarkup = InlineKeyboardMarkup(adsKeyboard)
query.edit_message_text('There are your ads.\n'
'You can switch them by clicking on status button\n'
'Or you can change your limits by clicking on corresponding button',
reply_markup=replyMarkup,
parse_mode=ParseMode.HTML)
return TelegramBot.CHANGING_ADS
"""
Function for building keyboard with ads info.
"""
def get_ads_status_keyboard(self, adsList: list) -> list:
adVisible = ''
adType = ''
bankName = ''
keyboard = [
[], #BankName and AdType
[], #Ad status
[], #Ad price equation
[InlineKeyboardButton("Ad Limits", callback_data='callback_NULL')],
[], #Ad limits
[InlineKeyboardButton('Menu β', callback_data='callback_menu')]
]
adStatusIndex = 0
limitButtonIndex = 0
for ad in adsList:
ad = ad['data']
bankName = ad['online_provider'].upper()
if ad['trade_type'] == 'ONLINE_SELL':
adType = 'Selling'
else:
adType = 'Buying'
firstRaw = InlineKeyboardButton(bankName + " " + adType,
callback_data='callback_NULL')
keyboard[0].append(firstRaw)
adVisible = f'{self.returnStatusTextAndSymbol(ad["visible"])[0]} {self.returnStatusTextAndSymbol(ad["visible"])[1]}'
secondRaw = InlineKeyboardButton(adVisible,
callback_data=f'callback_ad_switch_{abs((int(ad["visible"])) - 1)}_{ad["ad_id"]}_{adStatusIndex}')
keyboard[1].append(secondRaw)
thirdRaw = InlineKeyboardButton(f"{ad['price_equation']} RUB", callback_data='callback_NULL')
keyboard[2].append(thirdRaw)
minLimit = InlineKeyboardButton(ad['min_amount'],
callback_data=f'callback_ad_changeLimit_min_{ad["ad_id"]}_{limitButtonIndex}')
limitButtonIndex += 1
maxLimit = InlineKeyboardButton(ad['max_amount_available'],
callback_data=f'callback_ad_changeLimit_max_{ad["ad_id"]}_{limitButtonIndex}')
limitButtonIndex += 1
keyboard[4].append(minLimit)
keyboard[4].append(maxLimit)
adStatusIndex += 1
return keyboard
"""
Function to answer 'switch ad' button.
"""
def ads_switch(self, update: Update, context: CallbackContext) -> int:
print("Joined switch Ad")
query = update.callback_query
replyText = ''
ad_ID = query.data.split("_")[4]
if ad_ID == online_buy: replyText = 'Buy'
else: replyText = 'Sell'
ad_newStatus = bool(int(query.data.split("_")[3]))
index = int(query.data.split("_")[5])
prevMarkup = query.message.reply_markup
prevText = '\n'.join(query.message.text.split('\n')[-3:])
query.edit_message_text('π...Switching ad...\n',
reply_markup=prevMarkup)
if self.localBitcoinObject.switchAd(adID=ad_ID, status=ad_newStatus)[0] == 200:
replyText += f' ad is now {self.returnStatusTextAndSymbol(ad_newStatus)[0]} {self.returnStatusTextAndSymbol(ad_newStatus)[1]}'
buttonNewText = f'{self.returnStatusTextAndSymbol(ad_newStatus)[0]} {self.returnStatusTextAndSymbol(ad_newStatus)[1]}'
prevMarkup.inline_keyboard[1][index] = InlineKeyboardButton(buttonNewText,
callback_data=f'callback_ad_switch_{abs((int(ad_newStatus)) - 1)}_{ad_ID}_{index}')
elif self.localBitcoinObject.switchAd(adID=ad_ID, status=ad_newStatus)[0] == -1:
replyText += f' ad already was {self.returnStatusTextAndSymbol(ad_newStatus)[0]} {self.returnStatusTextAndSymbol(ad_newStatus)[1]}'
buttonNewText = f'{self.returnStatusTextAndSymbol(ad_newStatus)[0]} {self.returnStatusTextAndSymbol(ad_newStatus)[1]}'
prevMarkup.inline_keyboard[1][index] = InlineKeyboardButton(buttonNewText,
callback_data=f'callback_ad_switch_{abs((int(ad_newStatus)) - 1)}_{ad_ID}_{index}')
else:
replyText = 'β Couldn\'t switch ad for some reason!'
query.answer()
replyText += '\n\n' + prevText
query.edit_message_text(text=replyText, reply_markup=prevMarkup)
return TelegramBot.CHANGING_ADS
"""
Function to catch 'change ad limit' inline button.
"""
def change_limit_callback(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
query.answer()
limitBorder = query.data.split("_")[3]
self.lastCallbackQuery = query.data
reply_text = f'π Input new {limitBorder} limit or choose Cancel:'
keyboard = [
['ΠΡΠΌΠ΅Π½Π°']
]
replyMarkup = ReplyKeyboardMarkup(keyboard=keyboard, resize_keyboard=True)
self.menuHandler.states[self.CHANGING_ADS].append(self.getNewLimitHandler)
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id, text=reply_text, reply_markup=replyMarkup)
self.lastUpdate = update
return TelegramBot.CHANGING_ADS
"""
Function to get new limit from user's input and edit previous keyboard.
"""
def get_new_limit_message_callback(self, update: Update, context: CallbackContext) -> int:
self.menuHandler.states[self.CHANGING_ADS].remove(self.getNewLimitHandler)
limitBorder = self.lastCallbackQuery.split("_")[3]
ad_ID = self.lastCallbackQuery.split("_")[4]
query = self.lastUpdate.callback_query
msgText = ''
newMarkup = query.message.reply_markup
if update.message.text == 'ΠΡΠΌΠ΅Π½Π°':
msgText = f'π You have canceled changing the {limitBorder} limit!'
else:
newLimit = update.message.text
msgText = f'Changing {limitBorder} limit...π'
msg_ID = self.sendMessageWithConnectionCheck(text=msgText,
chat_id=update.effective_chat.id,
reply_markup=ReplyKeyboardRemove()).message_id
statusCode = None
if limitBorder == 'min':
statusCode = self.localBitcoinObject.changeAdField(ad_ID=ad_ID, min_amount=newLimit)[0]
elif limitBorder == 'max':
statusCode = self.localBitcoinObject.changeAdField(ad_ID=ad_ID, max_amount=newLimit)[0]
if statusCode == 200:
self.updater.bot.delete_message(chat_id=update.effective_chat.id, message_id=msg_ID)
msgText = f'π Successfully changed limit to {newLimit}!'
buttonIndex = int(self.lastCallbackQuery.split('_')[5])
newMarkup = query.message.reply_markup
newMarkup.inline_keyboard[4][buttonIndex] = InlineKeyboardButton(newLimit,
callback_data=f'callback_ad_changeLimit_{limitBorder.split("_")[0]}_{ad_ID}_{buttonIndex}')
else:
msgText = f'β Failed to change limit to {newLimit}!'
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id,
text=msgText,
reply_markup=ReplyKeyboardRemove())
query.edit_message_text(text=msgText + '\n\n' + '\n'.join(query.message.text.split('\n')[-3:]),
reply_markup=newMarkup)
return TelegramBot.CHANGING_ADS
"""
Function to catch 'Work' button in menu.
"""
def command_menu_works(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
query.answer()
keyboard = [
[InlineKeyboardButton(f'Selling {self.returnStatusTextAndSymbol(self.worksDictionary["sell"]["status"])[1]}',
callback_data='callback_works_sell'),
InlineKeyboardButton(f'Buying {self.returnStatusTextAndSymbol(self.worksDictionary["buy"]["status"])[1]}',
callback_data='callback_works_buy'),
InlineKeyboardButton(f'Scanning {self.returnStatusTextAndSymbol(self.worksDictionary["scan"]["status"])[1]}',
callback_data='callback_works_scan')],
[InlineKeyboardButton('Menu β',
callback_data='callback_menu_later')]
]
replyMarkup = InlineKeyboardMarkup(keyboard)
query.edit_message_text('Available works' + '\n'.join(query.message.text.split('\n')[-3:]),
reply_markup=replyMarkup)
return TelegramBot.CHANGING_WORKS
"""
Function to catch inline button for sell work.
"""
def work_sell_options(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
markup = self.works_build_markup('sell')
query.answer()
query.edit_message_text(text=f'Sell work control', reply_markup=markup)
return TelegramBot.CHANGING_WORKS
"""
Function to catch inline button for buy work.
"""
def work_buy_options(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
markup = self.works_build_markup('buy')
query.answer()
query.edit_message_text(text=f'Buy work control', reply_markup=markup)
return TelegramBot.CHANGING_WORKS
"""
Function to catch inline button for scan work.
"""
def work_scan_options(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
markup = self.works_build_markup('scan')
query.answer()
query.edit_message_text(text=f'Scan work control', reply_markup=markup)
return TelegramBot.CHANGING_WORKS
"""
Returns keyboard markup for particular work.
"""
def works_build_markup(self, workType: str) -> InlineKeyboardMarkup:
statusText, statusIcon = self.returnStatusTextAndSymbol(not self.worksDictionary[workType]['status'])
keyboard = [
[InlineKeyboardButton(f'Turn {statusText} {statusIcon}', callback_data=f'callback_works_switch_{workType}')],
]
if workType in ['buy', 'sell']:
keyboard += self.works_build_numberArg_Part(workType)
if workType == 'sell':
buttonsList = [[]]
cardPart = [[InlineKeyboardButton(f'Cards', callback_data='callback_NULL')]]
for name in ['Ruslan', 'Mom', 'Ayrat']:
curButton = InlineKeyboardButton(f'{name} {self.returnStatusTextAndSymbol(self.worksDictionary["sell"]["cardOwner"]["name"] == name)[1]}',
callback_data=f'callback_sell_card_{name}')
buttonsList[0].append(curButton)
cardPart += buttonsList
keyboard += cardPart
keyboard += [[InlineKeyboardButton('Back π', callback_data='callback_works')]]
return InlineKeyboardMarkup(keyboard)
"""
Returns list of buttons which change work's arguments limit.
It is contained in sell and buy works markups.
"""
def works_build_numberArg_Part(self, workType: str, minNumThousands: int = 2, maxNumThousands: int = 10) -> list:
if workType not in ['sell', 'buy']:
raise ValueError(f"Haven't found worktype type with value {workType}!\nCheck that you gave valid argument!")
dictKey = ''
buttonText = ''
if workType == 'sell':
dictKey = 'sellBorder'
buttonText = 'Sell Border'
else:
dictKey = 'buyDifference'
buttonText = 'Buy Difference'
raw = [
[InlineKeyboardButton(f'{buttonText}',
callback_data='callback_NULL')],
[
InlineKeyboardButton(f'-{minNumThousands}k',
callback_data=f'callback_work_{workType}_{dictKey}_-{minNumThousands}'),
InlineKeyboardButton(f'-{maxNumThousands}k',
callback_data=f'callback_work_{workType}_{dictKey}_-{maxNumThousands}'),
InlineKeyboardButton(f'{self.worksDictionary[workType][dictKey]}',
callback_data=f'callback_work_{workType}_{dictKey}_manual'),
InlineKeyboardButton(f'+{minNumThousands}k',
callback_data=f'callback_work_{workType}_{dictKey}_+{minNumThousands}'),
InlineKeyboardButton(f'+{maxNumThousands}k',
callback_data=f'callback_work_{workType}_{dictKey}_+{maxNumThousands}'),
]
]
return raw
"""
Function switches work ON or OFF.
Switches only if work can be switched.
"""
def work_switch(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
query.answer()
workType = query.data.split("_")[3]
replyText = ''
newMarkup = query.message.reply_markup
if self.work_can_be_switched(workType):
newStatus = not self.worksDictionary[workType]['status']
self.worksDictionary[workType]['status'] = newStatus
statusText, statusIcon = self.returnStatusTextAndSymbol(not newStatus)
newMarkup.inline_keyboard[0][0] = InlineKeyboardButton(f'Turn {statusText} {statusIcon}',
callback_data=f'callback_works_switch_{workType}')
replyText = f'β
Switched work status!'
else:
replyText = f"β Couldn't switch work ON!\n" \
f"For <i>sell</i> check that active card and sell border are set (not None). \n" \
f"For <i>buy</i> check that buy difference is set (not None)."
query.edit_message_text(text=replyText, reply_markup=newMarkup, parse_mode=ParseMode.HTML)
return TelegramBot.CHANGING_WORKS
"""
Function to switch work. If some arguments of work are not there, you can't switch work on.
For sell needed arguments are sell broder and active card.
For buy needed argument is buy difference.
"""
def work_can_be_switched(self, worktype: str) -> bool:
if worktype == 'sell':
if self.worksDictionary['sell']['sellBorder'] is None or self.worksDictionary['sell']['cardOwner'] is None:
return False
elif worktype == 'buy':
if self.worksDictionary['buy']['buyDifference'] is None:
return False
return True
def change_work_numberArg_fromButton(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
query.answer()
workType = query.data.split("_")[2]
dictKey = query.data.split("_")[3]
numArg = int(query.data.split("_")[4])
if self.worksDictionary[workType][dictKey] is not None:
self.worksDictionary[workType][dictKey] += numArg * 1000
newMarkup = query.message.reply_markup
newMarkup.inline_keyboard[2][2] = InlineKeyboardButton(f'{self.worksDictionary[workType][dictKey]}',
callback_data=f'callback_work_{workType}_{dictKey}_manual')
query.edit_message_text(text='β
Changed work argument!', reply_markup=newMarkup)
else:
query.edit_message_text(text=f'β Can\'t change work argument because it\'s not given now!',
reply_markup=query.message.reply_markup)
return TelegramBot.CHANGING_WORKS
def change_work_numberArg_manually(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
query.answer()
self.lastCallbackQuery = query.data
reply_text = f'π Input new argument or choose Cancel:'
keyboard = [
['ΠΡΠΌΠ΅Π½Π°']
]
replyMarkup = ReplyKeyboardMarkup(keyboard=keyboard, resize_keyboard=True)
self.menuHandler.states[self.CHANGING_WORKS].append(self.getNewWorkArgumentHandler)
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id, text=reply_text, reply_markup=replyMarkup)
self.lastUpdate = update
return TelegramBot.CHANGING_WORKS
def get_work_numberArg_manually_message(self, update: Update, context: CallbackContext) -> int:
self.menuHandler.states[self.CHANGING_WORKS].remove(self.getNewWorkArgumentHandler)
replyText = ''
msgText = update.message.text
if msgText == 'ΠΡΠΌΠ΅Π½Π°':
replyText = 'π Canceled changing work argument!'
else:
workType = self.lastCallbackQuery.split("_")[2]
dictKey = self.lastCallbackQuery.split("_")[3]
self.worksDictionary[workType][dictKey] = int(msgText)
replyText = f"β
Successfully changed {workType} argument to {msgText}!"
updateToChange = self.lastUpdate.callback_query
newMarkup = updateToChange.message.reply_markup
newMarkup.inline_keyboard[2][2] = InlineKeyboardButton(msgText, callback_data=self.lastCallbackQuery)
updateToChange.edit_message_text(text=replyText, reply_markup=newMarkup)
self.sendMessageWithConnectionCheck(chat_id=update.effective_chat.id,
text=replyText,
reply_markup=ReplyKeyboardRemove())
return TelegramBot.CHANGING_WORKS
def change_work_activeCard(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
query.answer()
queryName = query.data.split("_")[3]
self.setActiveCardPerson(queryName)
buttonsList = []
for name in ['Ruslan', 'Mom', 'Ayrat']:
curButton = InlineKeyboardButton(
f'{name} {self.returnStatusTextAndSymbol(self.worksDictionary["sell"]["cardOwner"]["name"] == name)[1]}',
callback_data=f'callback_sell_card_{name}')
buttonsList.append(curButton)
newMarkup = query.message.reply_markup
newMarkup.inline_keyboard[4] = buttonsList
query.edit_message_text(text='β
Changed active card!', reply_markup=newMarkup)
return TelegramBot.CHANGING_WORKS
def command_menu_balance(self, update: Update, context: CallbackContext) -> int:
query = update.callback_query
query.answer()
prevMarkup = query.message.reply_markup
query.edit_message_text(text=f"π Getting your balance...")
walletInfo = self.localBitcoinObject.getWallet()
totalBalance = walletInfo['total']['balance']
sendableBalance = walletInfo['total']['sendable']
btcAdress = walletInfo['receiving_address']
replyText = f"π¨βπ» {myUserName}\nTotal balance: <b>{totalBalance}</b>\n" \
f"Sendable amount: <b>{sendableBalance}</b>\n\n" \
f"Your BTC address: <i>{btcAdress}</i>"
query.edit_message_text(text=replyText, reply_markup=prevMarkup, parse_mode=ParseMode.HTML)
return TelegramBot.MENU_CHOOSING_OPTION
"""
Get status of main SELL and BUY ads.
"""
def adsStatus(self, update, context):
adsDict = self.localBitcoinObject.getSeveralAds([online_buy, online_sell])
bitcoinBalance = self.localBitcoinObject.getWalletBalance()['total']['balance']
text = ""
for ad in adsDict:
ad = ad['data']
if ad['visible']: text += '<i>On</i>π’'
else: text += '<i>Off</i>π΄'
argumentsInfo = ''
if ad['trade_type'] == 'ONLINE_SELL':
text += '<b>Selling</b>'
argumentsInfo = f"Sell Border: {self.worksDictionary['sell']['sellBorder']}\n" \
f"Active Card: {self.worksDictionary['sell']['cardOwner']['cardMessage']}"
else:
text += '<b>Buying</b>'
argumentsInfo = f"Buy Difference: {self.worksDictionary['buy']['buyDifference']}"
text += f'\nLimits: {int(float(ad["min_amount"]))} - {int(float(ad["max_amount_available"]))}\n{argumentsInfo}\n\n'
text += f"\nBalance: <strong>{bitcoinBalance}</strong> BTC."
self.sendMessageWithConnectionCheck(chat_id=self.chatID, text=text, parse_mode=ParseMode.HTML)
"""
Switches needed ad(sell or buy) to needed status(OFF(0) or ON(1))
"""
def switchAd(self, update, context):
userArgs = " ".join(context.args)
sellWordRegex, buyWordRegex, switchOnRegex, switchOffRegex = re.compile(r'sel'), re.compile(r'buy'), re.compile(r'on|1'), re.compile(r'off|0')
switchBoolean = None
replyMessage = ''
if not switchOnRegex.search(userArgs) and not switchOffRegex.search(userArgs):
replyMessage = f"Haven't found any status(ON/OFF)!β"
elif switchOffRegex.search(userArgs) and switchOnRegex.search(userArgs):
replyMessage = f"Found both statuses. Choose ONE!β"
else:
if switchOnRegex.search(userArgs): switchBoolean = True
else: switchBoolean = False
statusText, statusSymbol = self.returnStatusTextAndSymbol(switchBoolean)
statusCode, adText = 0, ""
if sellWordRegex.search(userArgs):
statusCode = self.localBitcoinObject.switchAd(online_sell, switchBoolean)[0]
adText = "SELL"
elif buyWordRegex.search(userArgs):
statusCode = self.localBitcoinObject.switchAd(online_buy, switchBoolean)[0]
adText = "BUY"
if statusCode == -1: # If ad already has this status, so we don't need to send request to local
replyMessage = f"{adText} AD i s already has status {statusText}!{statusSymbol}"
elif statusCode == 200:
replyMessage = f"{adText} AD successfully switched to {statusText}!{statusSymbol}"
if replyMessage == '':
replyMessage = 'Haven\'t found any AD(SELL/BUY)!β'
self.sendMessageWithConnectionCheck(chat_id=update.message.chat_id, text=replyMessage)
"""
Switches work modes on and off.
"""
def switchWork(self, update, context):
userArgs = " ".join(context.args)
workSellRegex, workBuyRegex, workScanningRegex = re.compile(r'\bsel\w{0,4}'), re.compile(r'\bbuy\w{0,3}'), re.compile(r'\bscan\w{0,4}')
switchOnRegex, switchOffRegex = re.compile(r'\bon\b'), re.compile(r'\boff\b')
numberRegex = re.compile(r'-?\d+')
cardMessageRegex = re.compile(r'(\bme\b)|(\bmom\b)|(\bayrat\b)')
reply_text = ''
workStatus : bool = None
numberArgument = None
cardMessage = None
if switchOnRegex.search(userArgs):
workStatus = True
elif switchOffRegex.search(userArgs):
workStatus = False
if workStatus is None:
reply_text = f"Haven't found any status(ON/OFF)!β"
else:
if numberRegex.search(userArgs):
numberArgument = int(numberRegex.search(userArgs).group())
if cardMessageRegex.search(userArgs):
cardMessage = cardMessageRegex.search(userArgs).group()
if workSellRegex.search(userArgs):
reply_text = self.actionOnWorkChoose('sell', workStatus, numberArgument, cardMessage)
elif workBuyRegex.search(userArgs):
reply_text = self.actionOnWorkChoose('buy', workStatus, numberArgument)
elif workScanningRegex.search(userArgs):
reply_text = self.actionOnWorkChoose('scan', workStatus)
else:
reply_text = "Haven't found any work mode!β"
self.sendMessageWithConnectionCheck(update.message.chat_id, reply_text)
"""
Controls which actions apply on work choose.
"""
def actionOnWorkChoose(self, workType : str, switchStatus : bool, workArgument : int = None, sellCard : str = None):
statusText, statusSymb = self.returnStatusTextAndSymbol(switchStatus)
reply_text = ''
if workType == 'buy' and workArgument == None:
reply_text = f"Haven't found argument for {workType} work!β"
elif switchStatus == None:
reply_text = f"Haven't found status for {workType} work!β"
elif self.worksDictionary[workType]['status'] and workType != 'sell' == switchStatus:
reply_text = f"Work {workType} is already turned {statusText}!{statusSymb}"
else:
if workType == 'sell':
requestCodeAndText = self.localBitcoinObject.switchAd(online_sell, switchStatus)
if requestCodeAndText[0] == 200:
reply_text += f"SELL AD switched {statusText}!{statusSymb}\n"
reply_text += self.switchSellWork(switchStatus, workArgument, sellCard)
elif requestCodeAndText[0] == -1:
reply_text += f"SELL AD is already was {statusText}!{statusSymb}\n"
reply_text += self.switchSellWork(switchStatus, workArgument, sellCard)
else:
reply_text += f"With SELL AD other ERROR occured:\n" + requestCodeAndText[1]
elif workType == 'buy':
requestCodeAndText = self.localBitcoinObject.switchAd(online_buy, switchStatus)
if requestCodeAndText[0] == 200:
self.worksDictionary['buy']['status'] = switchStatus
self.worksDictionary['buy']['buyDifference'] = workArgument
reply_text = f"BUY WORK was turned {statusText}!{statusSymb}"
elif requestCodeAndText[0] == -1:
reply_text = f"BUY WORK is already was {statusText}!{statusSymb}"
else:
reply_text = f"With BUY WORK other ERROR occured:\n" + requestCodeAndText[1]
elif workType == 'scan':
self.worksDictionary['scan']['status'] = switchStatus
reply_text = f"SCANNING WORK was turned {statusText}!{statusSymb}"
return reply_text
def switchSellWork(self, switchStatus : bool, sellBorder : int = None, cardMsg : str = None):
workType = 'sell'
reply_text = ''
if switchStatus is None:
reply_text = f"Haven't found any status(on/off) for SELL work!β"
else:
statusText, statusSymbol = self.returnStatusTextAndSymbol(switchStatus)
if switchStatus is True:
if self.worksDictionary[workType]['status'] is False and self.worksDictionary[workType]['sellBorder'] is None and self.worksDictionary[workType]['cardOwner']['cardMessage'] is None and (sellBorder is None or cardMsg is None):
reply_text = f"Haven't found some of arguments(cardMsg/sellborder) for first launch of SELL work!β"
elif self.worksDictionary[workType]['status'] is True:
self.worksDictionary[workType]['sellBorder'] = sellBorder
self.worksDictionary[workType]['cardOwner']['cardMessage'] = cardMsg
reply_text = f"Changed arguments of work, but SELL work already has status {statusText}!{statusSymbol}"
elif self.worksDictionary[workType]['status'] is False:
self.worksDictionary[workType]['status'] = switchStatus
self.worksDictionary[workType]['sellBorder'] = sellBorder
self.worksDictionary[workType]['cardOwner']['cardMessage'] = cardMsg
reply_text = f"Changed status of SELL work to {statusText}!{statusSymbol}"
if cardMsg == 'me':
self.setActiveCardPerson('Ruslan')
elif cardMsg == 'mom':
self.setActiveCardPerson('Mom')
elif cardMsg == 'ayrat':
self.setActiveCardPerson('Ayrat')
elif switchStatus is False:
if self.worksDictionary[workType]['status'] is False:
reply_text = f"SELL work already has status {statusText}!{statusSymbol}"
else:
self.worksDictionary[workType]['status'] = switchStatus
reply_text = f"Changed status of SELL work to {statusText}!{statusSymbol}"
return reply_text
"""
Change border / difference on sell / buy ads.
"""
def changeBorder(self, update, context):
userArgs = " ".join(context.args)
reply_message = ''
sellAdRegex = re.compile(r'sel')
buyAdRegex = re.compile(r'buy')
numberRegex = re.compile(r'-?\d+')
activeAd = ''
currentNumber = 0
if sellAdRegex.search(userArgs) and buyAdRegex.search(userArgs):
reply_message = 'Found both sell and buy ad call. Choose ONE!β'
self.sendMessageWithConnectionCheck(update.message.chat_id, reply_message)
return 0
elif sellAdRegex.search(userArgs):
activeAd = online_sell
elif buyAdRegex.search(userArgs):
activeAd = online_buy
else:
reply_message = "Haven't found any AD(sell/buy). Choose ONE!β"
self.sendMessageWithConnectionCheck(update.message.chat_id, reply_message)
return 0