-
Notifications
You must be signed in to change notification settings - Fork 747
/
telegram_bot.py
928 lines (773 loc) · 32.5 KB
/
telegram_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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Bot to reply to Telegram messages.
Usage:
Press Ctrl-C on the command line or send a signal to the process to stop the bot.
"""
import argparse
import os
import json
import subprocess
# import logging
import re
import urllib.request
from datetime import datetime
from time import sleep
# from pandas.core.frame import DataFrame
from telegram import InlineKeyboardButton, ReplyKeyboardMarkup
from telegram.bot import Bot, BotCommand
from telegram.ext import (
Updater,
CommandHandler,
CallbackQueryHandler,
Filters,
ConversationHandler,
MessageHandler,
)
from telegram.replykeyboardremove import ReplyKeyboardRemove
from apscheduler.schedulers.background import BackgroundScheduler
from models.telegram import (
TelegramControl,
TelegramHelper,
TelegramHandler,
TelegramActions,
ConfigEditor,
SettingsEditor,
)
scannerSchedule = BackgroundScheduler(timezone="UTC")
# TYPING_RESPONSE = 1
CHOOSING, TYPING_REPLY = range(2)
EXCHANGE, MARKET, ANYOVERRIDES, OVERRIDES, SAVE, START = range(6)
EXCEPT_EXCHANGE, EXCEPT_MARKET = range(2)
replykeyboard = [["Coinbase", "Coinbase Pro", "Binance", "Kucoin"]]
markup = ReplyKeyboardMarkup(replykeyboard, one_time_keyboard=True)
class TelegramBotBase:
"""
base level for telegram bot
"""
userid = ""
datafolder = os.curdir
data = {}
helper = None
handler = None
editor = None
def _check_if_allowed(self, userid, update) -> bool:
if str(userid) != self.userid:
update.message.reply_text("<b>Not authorised!</b>", parse_mode="HTML")
return False
return True
class TelegramBot(TelegramBotBase):
"""
main telegram bot class
"""
def __init__(self):
self.token = ""
self.config_file = ""
self.cl_args = ""
self.market = ""
self.exchange = ""
self.pair = ""
self.overrides = ""
parser = argparse.ArgumentParser(description="PyCryptoBot Telegram Bot")
parser.add_argument(
"--config",
type=str,
dest="config_file",
help="pycryptobot config file",
default="config.json",
)
parser.add_argument(
"--datafolder",
type=str,
help="Use the datafolder at the given location, useful for multi bots running in different folders",
default="",
)
args = parser.parse_args()
self.config_file = args.config_file
self.helper = TelegramHelper(self.config_file)
self.token = self.helper.config["telegram"]["token"]
self.userid = self.helper.config["telegram"]["user_id"]
if args.datafolder != "":
self.helper.datafolder = args.datafolder
if not os.path.exists(os.path.join(self.helper.datafolder, "telegram_data")):
os.mkdir(os.path.join(self.helper.datafolder, "telegram_data"))
if os.path.isfile(
os.path.join(self.helper.datafolder, "telegram_data", "data.json")
):
write_ok, try_count = False, 0
while not write_ok and try_count <= 5:
try_count += 1
self.helper.read_data("data.json")
write_ok = True
if "trades" not in self.helper.data:
self.helper.data.update({"trades": {}})
write_ok = self.helper.write_data()
if "markets" not in self.helper.data:
self.helper.data.update({"markets": {}})
write_ok = self.helper.write_data()
if "scannerexceptions" not in self.helper.data:
self.helper.data.update({"scannerexceptions": {}})
write_ok = self.helper.write_data()
if not write_ok:
sleep(1)
else:
ds = {"trades": {}, "markets": {}, "scannerexceptions": {}}
self.helper.data = ds
self.helper.write_data()
self.updater = Updater(
self.token,
use_context=True,
)
self.handler = TelegramHandler(self.userid, self.helper)
self.control = TelegramControl(self.helper)
self.actions = TelegramActions(self.helper)
self.editor = ConfigEditor(self.helper)
self.setting = SettingsEditor(self.helper)
if args.datafolder != "":
self.helper.datafolder = args.datafolder
def _question_which_exchange(self, update, context):
"""start new bot ask which exchange"""
self.exchange = ""
self.overrides = ""
self.helper.send_telegram_message(
update, "Select the exchange:", markup, context=context
)
def _answer_which_exchange(self, update, context) -> bool:
"""start bot validate exchange and ask which market/pair"""
if update.message.text.lower() == "cancel":
update.message.reply_text(
"Operation Cancelled", reply_markup=ReplyKeyboardRemove()
)
return ConversationHandler.END
if (
update.message.text == "Coinbase"
or update.message.text == "Coinbase Pro"
or update.message.text == "Kucoin"
or update.message.text == "Binance"
):
self.exchange = update.message.text.lower()
if update.message.text == "Coinbase":
self.exchange = "coinbase"
elif update.message.text == "Coinbase Pro":
self.exchange = "coinbasepro"
else:
if self.exchange == "":
self.helper.send_telegram_message(
update, "Invalid Exchange Entered!.", context=context
)
return False
return True
def _question_which_pair(self, update, context):
self.market = ""
self.helper.send_telegram_message(
update, "Which market/pair is this for?", ReplyKeyboardRemove(), context
)
def _answer_which_pair(self, update, context) -> bool:
if update.message.text.lower() == "cancel":
self.helper.send_telegram_message(
update, "Operation Cancelled", ReplyKeyboardRemove(), context
)
return ConversationHandler.END
if self.exchange in ("coinbase", "coinbasepro", "kucoin"):
p = re.compile(r"^[0-9A-Z]{1,20}\-[1-9A-Z]{2,5}$")
if not p.match(update.message.text):
self.helper.send_telegram_message(
update, "Invalid market format", ReplyKeyboardRemove(), context
)
return False
elif self.exchange == "binance":
p = re.compile(r"^[A-Z0-9]{4,25}$")
if not p.match(update.message.text):
self.helper.send_telegram_message(
update, "Invalid market format.", ReplyKeyboardRemove(), context
)
return False
self.pair = update.message.text
return True
# Define command handlers. These usually take the two arguments update and context.
def setcommands(self, update, context) -> None:
"""Set bot commands in telegram"""
command = [
BotCommand("controlpanel", "show command buttons"),
BotCommand("cleandata", "clean JSON data files"),
BotCommand("addexception", "add pair to scanner exception list"),
BotCommand("removeexception", "remove pair from scanner exception list"),
BotCommand(
"scanner", "start auto scan high volume markets and start bots"
),
# BotCommand("stopscanner", "stop auto scan high volume markets"),
BotCommand("addnew", "add and start a new bot"),
# BotCommand("deletebot", "delete bot from startbot list"),
BotCommand("margins", "show margins for all open trades"),
BotCommand("trades", "show closed trades"),
BotCommand("stats", "show exchange stats for market/pair"),
BotCommand("help", "show help text")
# BotCommand("showinfo", "show all running bots status"),
# BotCommand("showconfig", "show config for selected exchange"),
# BotCommand("startbots", "start all or selected bot"),
# BotCommand("stopbots", "stop all or the selected bot"),
# BotCommand("pausebots", "pause all or selected bot"),
# BotCommand("resumebots", "resume paused bots"),
# BotCommand("buy", "manual buy"),
# BotCommand("sell", "manual sell"),
]
ubot = Bot(self.token)
ubot.set_my_commands(command)
self.helper.send_telegram_message(
update,
"<i>Bot Commands Created</i>",
ReplyKeyboardRemove(),
context=context,
)
def help(self, update, context):
"""Send a message when the command /help is issued."""
helptext = "<b>Information Command List</b>\n\n"
helptext += (
"<b>/setcommands</b> - <i>add all commands to bot for easy access</i>\n"
)
helptext += "<b>/margins</b> - <i>show margins for open trade</i>\n"
helptext += "<b>/trades</b> - <i>show closed trades</i>\n"
helptext += "<b>/stats</b> - <i>display stats for market</i>\n\n"
helptext += "<b>Interactive Command List</b>\n\n"
helptext += "<b>/controlpanel</b> - <i>show interactive control buttons</i>\n"
helptext += "<b>/cleandata</b> - <i>check and remove any bad Json files</i>\n"
helptext += "<b>/addnew</b> - <i>start the requested pair</i>\n\n"
helptext += "<b>Market Scanner Commands</b>\n\n"
helptext += "<b>/scanner</b> - <i>start auto scan high volume markets and start bots</i>\n"
helptext += "<b>/addexception</b> - <i>add pair to scanner exception list</i>\n"
helptext += (
"<b>/removeexception</b> - <i>remove pair from scanner exception list</i>\n"
)
self.helper.send_telegram_message(update, helptext, context=context)
def trades(self, update, context):
"""List trades"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return
self.handler.get_trade_options(None, None)
return
def statsrequest(self, update: Updater, context):
"""Ask which exchange stats are wanted for"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
self.helper.send_telegram_message(
update, "Select the exchange", markup, context=context
)
return CHOOSING
def stats_exchange_received(self, update, context):
"""Ask which market stats are wanted for"""
if update.message.text.lower() == "done":
return None
if update.message.text.lower() == "cancel":
self.helper.send_telegram_message(
update, "Operation Cancelled", ReplyKeyboardRemove(), context=context
)
return ConversationHandler.END
if update.message.text in ("Coinbase", "Coinbase Pro", "Kucoin", "Binance"):
self.exchange = update.message.text.lower()
if update.message.text == "Coinbase":
self.exchange = "coinbase"
elif update.message.text == "Coinbase Pro":
self.exchange = "coinbasepro"
else:
if self.exchange == "":
self.helper.send_telegram_message(
update, "Invalid Exchange Entered!", context=context
)
self.statsrequest(update, context)
return None
self.helper.send_telegram_message(
update,
"Which market/pair do you want stats for?",
ReplyKeyboardRemove(),
context=context,
)
return TYPING_REPLY
def stats_pair_received(self, update, context):
"""Show stats for selected exchange and market"""
if update.message.text.lower() == "done":
return None
if update.message.text.lower() == "cancel":
self.helper.send_telegram_message(
update, "Operation Cancelled", ReplyKeyboardRemove(), context=context
)
return ConversationHandler.END
if self.exchange in ("coinbase", "coinbasepro", "kucoin"):
p = re.compile(r"^[0-9A-Z]{1,20}\-[1-9A-Z]{2,5}$")
if not p.match(update.message.text):
self.helper.send_telegram_message(
update,
"Invalid market format",
ReplyKeyboardRemove(),
context=context,
)
self.stats_exchange_received(update, context)
return None
elif self.exchange == "binance":
p = re.compile(r"^[A-Z0-9]{4,25}$")
if not p.match(update.message.text):
self.helper.send_telegram_message(
update,
"Invalid market format",
ReplyKeyboardRemove(),
context=context,
)
self.stats_exchange_received(update, context)
return None
self.pair = update.message.text
self.helper.send_telegram_message(
update, "<i>Gathering Stats, please wait...</i>", context=context
)
output = self.helper.start_process(
self.pair, self.exchange, "--stats --live 1", "telegram", True
)
self.helper.send_telegram_message(update, output, context=context)
return ConversationHandler.END
def newbot_request(self, update: Updater, context):
"""start new bot ask which exchange"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
self._question_which_exchange(update, context)
return EXCHANGE
def newbot_exchange(self, update, context):
"""start bot validate exchange and ask which market/pair"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
if not self._answer_which_exchange(update, context):
self.newbot_request(update, context)
self._question_which_pair(update, context)
return ANYOVERRIDES
def newbot_any_overrides(self, update, context) -> None:
"""start bot validate market and ask if overrides required"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
if not self._answer_which_pair(update, context):
self.newbot_exchange(update, context)
return None
r_keyboard = [["Yes", "No"]]
mark_up = ReplyKeyboardMarkup(r_keyboard, one_time_keyboard=True)
self.helper.send_telegram_message(
update, "Do you want to use any commandline overrides?", mark_up, context
)
return MARKET
def newbot_market(self, update, context):
"""start bot - ask for overrides if none required ask to save bot"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
if update.message.text == "No":
reply_keyboard = [["Yes", "No"]]
mark_up = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
self.helper.send_telegram_message(
update, "Do you want to save this?", mark_up, context
)
return SAVE
self.helper.send_telegram_message(
update,
"Tell me any other commandline overrides to use?",
ReplyKeyboardRemove(),
context,
)
return OVERRIDES
def newbot_overrides(self, update, context):
"""start bot - ask to save bot"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
# Telegram desktop client can auto replace -- with a single long dash
# this converts it back to --
self.overrides = update.message.text.replace(
b"\xe2\x80\x94".decode("utf-8"), "--"
)
reply_keyboard = [["Yes", "No"]]
mark_up = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
self.helper.send_telegram_message(
update, "Do you want to save this?", mark_up, context
)
return SAVE
def newbot_save(self, update, context):
"""start bot - save if required ask if want to start"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
self.helper.logger.info("called newbot_save")
if update.message.text == "Yes":
write_ok, try_count = False, 0
while not write_ok and try_count <= 5:
try_count += 1
try:
self.helper.read_data()
if "markets" in self.helper.data:
if self.pair not in self.helper.data["markets"]:
self.helper.data["markets"].update(
{
self.pair: {
"overrides": f"--exchange {self.exchange} --market {self.pair} {self.overrides}"
}
}
)
write_ok = self.helper.write_data()
if write_ok:
self.helper.send_telegram_message(
update, f"{self.pair} saved \u2705", context=context
)
else:
sleep(1)
else:
self.helper.send_telegram_message(
update,
f"{self.pair} already setup, no changes made.",
context=context,
)
write_ok = True
else:
self.helper.data.update({"markets": {}})
self.helper.data["markets"].update(
{
self.pair: {
"overrides": f"--exchange {self.exchange} --market {self.pair} {self.overrides}"
}
}
)
write_ok = self.helper.write_data()
if write_ok:
self.helper.send_telegram_message(
update, f"{self.pair} saved \u2705", context=context
)
else:
sleep(1)
except Exception as err: # pylint: disable=broad-except
print(err)
reply_keyboard = [["Yes", "No"]]
mark_up = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
self.helper.send_telegram_message(
update, "Do you want to start this bot?", mark_up, context
)
return START
def newbot_start(self, update, context, startmethod: str = "telegram") -> None:
"""start bot - start bot if want"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
if update.message.text == "No":
self.helper.send_telegram_message(
update,
"Command Complete, have a nice day.",
ReplyKeyboardRemove(),
context,
)
return ConversationHandler.END
if (
self.helper.start_process(
self.pair, self.exchange, self.overrides, startmethod
)
is False
):
self.helper.send_telegram_message(
update,
f"{self.pair} is already running, no action taken.",
ReplyKeyboardRemove(),
context,
)
else:
if startmethod != "scanner":
self.helper.send_telegram_message(
update,
f"{self.pair} crypto bot Starting",
ReplyKeyboardRemove(),
context,
)
self.helper.send_telegram_message(
update, "Command Complete, have a nice day.", ReplyKeyboardRemove(), context
)
return ConversationHandler.END
def error(self, update, context):
"""Log Errors"""
try:
if len(context.error.args) > 0:
err_message = f"ERROR: {context.error.args[0]}"
elif "message" in context.error:
err_message = f"ERROR: {context.error.message}"
self.helper.send_telegram_message(update, err_message, new_message=True)
except Exception as err: # pylint: disable=broad-except
print(err)
self.helper.logger.error(
msg="Exception while handling an update:", exc_info=context.error
)
try:
if "HTTPError" in context.error.args[0]:
while self.checkconnection() is False:
self.helper.logger.warning("No internet connection found")
self.updater.start_polling(poll_interval=30)
sleep(30)
self.updater.start_polling()
return
except Exception:
pass
def done(self, update, context):
"""added for conversations to end"""
return ConversationHandler.END
def checkconnection(self) -> bool:
"""internet connection check"""
try:
urllib.request.urlopen("https://api.telegram.org")
return True
except Exception:
print("No internet connection")
return False
def exception_exchange(self, update, context):
"""start new bot ask which exchange"""
self._question_which_exchange(update, context)
return EXCEPT_EXCHANGE
def exception_pair(self, update, context):
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return
self._answer_which_exchange(update, context)
self._question_which_pair(update, context)
return EXCEPT_MARKET
def exception_add(self, update, context):
"""start bot - save if required ask if want to start"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
self.helper.logger.info("called exception_add")
self._answer_which_pair(update, context)
self.helper.read_data()
if "scannerexceptions" not in self.helper.data:
self.helper.data.update({"scannerexceptions": {}})
if self.pair not in self.helper.data["scannerexceptions"]:
write_ok, try_count = False, 0
while not write_ok and try_count <= 5:
try_count += 1
self.helper.data["scannerexceptions"].update({self.pair: {}})
write_ok = self.helper.write_data()
if not write_ok:
sleep(1)
self.helper.send_telegram_message(
update,
f"{self.pair} Added to Scanner Exception List \u2705",
ReplyKeyboardRemove(),
context,
)
else:
self.helper.send_telegram_message(
update,
f"{self.pair} Already on exception list",
ReplyKeyboardRemove(),
context,
)
return ConversationHandler.END
def exception_remove(self, update, context):
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return
self.control.ask_exception_bot_list(update, context)
return
def marginrequest(self, update, context):
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return
self.handler.ask_margin_type(None, context)
return
def deleterequest(self, update, context):
"""ask which bot to delete"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return
self.control.ask_delete_bot_list(update, context)
def scanning(self, update, context):
"""calling /startscanner from Telegram command list"""
if not self._check_if_allowed(context._user_id_and_data[0], update):
return
self.handler.get_scanner_options(None)
return
def cleandata(self, update, context) -> None:
"""calling /cleandata from Telegram command list"""
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return
self.helper.clean_data_folder()
self.actions.get_bot_info(None, context)
self.helper.send_telegram_message(update, "<b>Operation Complete</b>", context=context)
def statstwo(self, update, context):
jsonfiles = os.listdir(os.path.join(self.helper.datafolder, "telegram_data"))
for file in jsonfiles:
exchange = "coinbasepro"
if file.__contains__("output.json"):
if file.__contains__("coinbasepro"):
exchange = "coinbasepro"
elif file.__contains__("coinbase"):
exchange = "coinbase"
if file.__contains__("binance"):
exchange = "binance"
if file.__contains__("kucoin"):
exchange = "kucoin"
self.helper.send_telegram_message(
update, "<i>Gathering Stats, please wait...</i>", context=context
)
with open(
os.path.join(self.helper.datafolder, "telegram_data", file),
"r",
encoding="utf8",
) as json_file:
data = json.load(json_file)
pairs = ""
for pair in data:
if pair.__contains__("DOWN") or pair.__contains__("UP"):
continue
# count += 1
pairs = pairs + pair + " "
output = subprocess.getoutput(
f"python3 pycryptobot.py --stats --exchange {exchange} --statgroup {pairs} "
)
self.helper.send_telegram_message(update, output, context=context)
sleep(30)
self.helper.send_telegram_message(
update, "Pausing before next set", context=context
)
def get_bot_list(self, update, context):
if not self._check_if_allowed(
context._user_id_and_data[0], update
): # pylint: disable=protected-access
return None
# self.handler.get_bot_options(update)
# return
buttons = []
for market in self.helper.get_active_bot_list("active"):
read_ok = self.helper.read_data(market)
if read_ok and "botcontrol" in self.helper.data:
buttons.append(
InlineKeyboardButton(market, callback_data=f"bot_{market}")
)
if len(buttons) > 0:
self.helper.send_telegram_message(
update,
"<b>Select a market</b>",
self.control.sort_inline_buttons(buttons, "bot"),
context=context,
)
else:
self.helper.send_telegram_message(
update, "<b>No bots found.</b>", context=context
)
def request(self, update, context):
userid = context._user_id_and_data[0] # pylint: disable=protected-access
if self._check_if_allowed(userid, update):
self.helper.load_config()
key_markup = self.handler.get_request()
self.helper.send_telegram_message(
update, "<b>PyCryptoBot Command Panel.</b>", key_markup, context
)
def main():
"""Start the bot."""
# Create telegram bot configuration
botconfig = TelegramBot()
# Get the dispatcher to register handlers
dp = botconfig.updater.dispatcher
# Information commands
dp.add_handler(CommandHandler("help", botconfig.help))
dp.add_handler(CommandHandler("margins", botconfig.marginrequest, Filters.all))
dp.add_handler(CommandHandler("trades", botconfig.trades, Filters.text))
# General Action Command
dp.add_handler(CommandHandler("setcommands", botconfig.setcommands))
dp.add_handler(
CommandHandler("scanner", botconfig.scanning, Filters.text)
)
dp.add_handler(CommandHandler("cleandata", botconfig.cleandata, Filters.text))
dp.add_handler(
CommandHandler("removeexception", botconfig.exception_remove, Filters.text)
)
dp.add_handler(CommandHandler("ex", botconfig.get_bot_list))
dp.add_handler(CommandHandler("statsgroup", botconfig.statstwo))
# Response to Question handler
dp.add_handler(CallbackQueryHandler(botconfig.handler.get_response))
dp.add_handler(CommandHandler("controlPanel", botconfig.request))
conversation_exception = ConversationHandler(
entry_points=[CommandHandler("addexception", botconfig.exception_exchange)],
states={
EXCEPT_EXCHANGE: [
MessageHandler(
Filters.text, botconfig.exception_pair, pass_user_data=True
)
],
EXCEPT_MARKET: [
MessageHandler(
Filters.text, botconfig.exception_add, pass_user_data=True
)
],
},
fallbacks=[("Done", botconfig.done)],
)
conversation_stats = ConversationHandler(
entry_points=[CommandHandler("stats", botconfig.statsrequest)],
states={
CHOOSING: [
MessageHandler(
Filters.text, botconfig.stats_exchange_received, pass_user_data=True
)
],
TYPING_REPLY: [
MessageHandler(
Filters.text, botconfig.stats_pair_received, pass_user_data=True
)
],
},
fallbacks=[("Done", botconfig.done)],
)
conversation_newbot = ConversationHandler(
entry_points=[CommandHandler("addnew", botconfig.newbot_request)],
states={
EXCHANGE: [MessageHandler(Filters.text, botconfig.newbot_exchange)],
MARKET: [MessageHandler(Filters.text, botconfig.newbot_market)],
ANYOVERRIDES: [
MessageHandler(Filters.text, botconfig.newbot_any_overrides)
],
OVERRIDES: [MessageHandler(Filters.text, botconfig.newbot_overrides)],
SAVE: [MessageHandler(Filters.text, botconfig.newbot_save)],
START: [MessageHandler(Filters.text, botconfig.newbot_start)],
},
fallbacks=[("Done", botconfig.done)],
)
dp.add_handler(conversation_stats)
dp.add_handler(conversation_newbot)
dp.add_handler(conversation_exception)
# log all errors
dp.add_error_handler(botconfig.error)
botconfig.helper.clean_data_folder()
# Start the Bot
botconfig.updater.start_polling()
botconfig.helper.logger.info("Telegram Bot is listening")
botconfig.setcommands(None, None)
botconfig.updater.bot.send_message(
text="Online and ready.", chat_id=botconfig.helper.config["telegram"]["user_id"]
)
if botconfig.helper.autostart:
botconfig.actions.start_open_orders(None, None)
botconfig.handler._check_scheduled_job()
botconfig.actions.start_market_scan(None, None, use_default_scanner=botconfig.helper.use_default_scanner)
# Run the bot until you press Ctrl-C
# since start_polling() is non-blocking and will stop the bot gracefully.
botconfig.updater.idle()
if __name__ == "__main__":
main()