-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
handlers.py
430 lines (363 loc) · 14.4 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
import os
import subprocess
from shutil import copyfile
from telegram import Bot, ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
CallbackContext,
CommandHandler,
ConversationHandler,
Filters,
MessageHandler,
)
from btb_manager_telegram import (
DELETE_DB,
EDIT_COIN_LIST,
EDIT_USER_CONFIG,
MENU,
UPDATE_BTB,
UPDATE_TG,
buttons,
logger,
settings,
)
from btb_manager_telegram.utils import (
find_and_kill_binance_trade_bot_process,
kill_btb_manager_telegram_process,
)
def menu(update: Update, _: CallbackContext) -> int:
logger.info(f"Menu selector. ({update.message.text})")
keyboard = [
[
"💵 Current value",
],
["📈 Progress", "➗ Current ratios"],
["🔍 Check bot status", "⌛ Trade History"],
["🛠 Maintenance", "⚙️ Configurations"],
]
config_keyboard = [
["▶ Start trade bot", "⏹ Stop trade bot"],
["📜 Read last log lines", "❌ Delete database"],
["⚙ Edit user.cfg", "👛 Edit coin list"],
["📤 Export database", "⬅️ Back"],
]
maintenance_keyboard = [
["Update Telegram Bot"],
["Update Binance Trade Bot"],
["⬅️ Back"],
]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
reply_markup_config = ReplyKeyboardMarkup(config_keyboard, resize_keyboard=True)
reply_markup_maintenance = ReplyKeyboardMarkup(
maintenance_keyboard, resize_keyboard=True
)
if update.message.text in ["Begin", "⬅️ Back"]:
message = "Please select one of the options."
update.message.reply_text(message, reply_markup=reply_markup)
elif update.message.text in ["Go back", "OK", "⚙️ Configurations"]:
message = "Please select one of the options."
update.message.reply_text(message, reply_markup=reply_markup_config)
elif update.message.text in ["🛠 Maintenance", "Cancel update", "OK 👌"]:
message = "Please select one of the options."
update.message.reply_text(message, reply_markup=reply_markup_maintenance)
elif update.message.text == "💵 Current value":
for mes in buttons.current_value():
update.message.reply_text(
mes, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
elif update.message.text == "📈 Progress":
for mes in buttons.check_progress():
update.message.reply_text(
mes, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
elif update.message.text == "➗ Current ratios":
for mes in buttons.current_ratios():
update.message.reply_text(
mes, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
elif update.message.text == "🔍 Check bot status":
update.message.reply_text(buttons.check_status(), reply_markup=reply_markup)
elif update.message.text == "⌛ Trade History":
for mes in buttons.trade_history():
update.message.reply_text(
mes, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
elif update.message.text == "▶ Start trade bot":
update.message.reply_text(
buttons.start_bot(),
reply_markup=reply_markup_config,
parse_mode="MarkdownV2",
)
elif update.message.text == "⏹ Stop trade bot":
update.message.reply_text(buttons.stop_bot(), reply_markup=reply_markup_config)
elif update.message.text == "📜 Read last log lines":
update.message.reply_text(
buttons.read_log(),
reply_markup=reply_markup_config,
parse_mode="MarkdownV2",
)
elif update.message.text == "❌ Delete database":
re = buttons.delete_db()
if re[1]:
kb = [["⚠ Confirm", "Go back"]]
update.message.reply_text(
re[0],
reply_markup=ReplyKeyboardMarkup(kb, resize_keyboard=True),
parse_mode="MarkdownV2",
)
return DELETE_DB
else:
update.message.reply_text(
re[0], reply_markup=reply_markup_config, parse_mode="MarkdownV2"
)
elif update.message.text == "⚙ Edit user.cfg":
re = buttons.edit_user_cfg()
if re[1]:
update.message.reply_text(
re[0], reply_markup=ReplyKeyboardRemove(), parse_mode="MarkdownV2"
)
return EDIT_USER_CONFIG
else:
update.message.reply_text(
re[0], reply_markup=reply_markup_config, parse_mode="MarkdownV2"
)
elif update.message.text == "👛 Edit coin list":
re = buttons.edit_coin()
if re[1]:
update.message.reply_text(
re[0], reply_markup=ReplyKeyboardRemove(), parse_mode="MarkdownV2"
)
return EDIT_COIN_LIST
else:
update.message.reply_text(
re[0], reply_markup=reply_markup_config, parse_mode="MarkdownV2"
)
elif update.message.text == "📤 Export database":
re = buttons.export_db()
update.message.reply_text(
re[0], reply_markup=reply_markup_config, parse_mode="MarkdownV2"
)
if re[1] is not None:
bot = Bot(settings.TOKEN)
bot.send_document(
chat_id=update.message.chat_id,
document=re[1],
filename="crypto_trading.db",
)
elif update.message.text == "Update Telegram Bot":
re = buttons.update_tg_bot()
if re[1]:
kb = [["Update", "Cancel update"]]
update.message.reply_text(
re[0],
reply_markup=ReplyKeyboardMarkup(kb, resize_keyboard=True),
parse_mode="MarkdownV2",
)
return UPDATE_TG
else:
update.message.reply_text(
re[0],
reply_markup=reply_markup_maintenance,
parse_mode="MarkdownV2",
)
elif update.message.text == "Update Binance Trade Bot":
re = buttons.update_btb()
if re[1]:
kb = [["Update", "Cancel update"]]
update.message.reply_text(
re[0],
reply_markup=ReplyKeyboardMarkup(kb, resize_keyboard=True),
parse_mode="MarkdownV2",
)
return UPDATE_BTB
else:
update.message.reply_text(
re[0],
reply_markup=reply_markup_maintenance,
parse_mode="MarkdownV2",
)
return MENU
def start(update: Update, _: CallbackContext) -> int:
logger.info("Started conversation.")
keyboard = [["Begin"]]
message = (
f"Hi *{update.message.from_user.first_name}*\!\n"
f"Welcome to _Binace Trade Bot Manager Telegram_\.\n\n"
f"This Telegram bot was developed by @lorcalhost\.\n"
f"Find out more about the project [here](https://github.com/lorcalhost/BTB-manager-telegram)\.\n\n"
f"If you like the bot please [consider supporting the project 🍻](https://www.buymeacoffee.com/lorcalhost)\."
)
reply_markup = ReplyKeyboardMarkup(
keyboard, one_time_keyboard=True, resize_keyboard=True
)
update.message.reply_text(
message,
reply_markup=reply_markup,
parse_mode="MarkdownV2",
disable_web_page_preview=True,
)
return MENU
def edit_coin(update: Update, _: CallbackContext) -> int:
logger.info(f"Editing coin list. ({update.message.text})")
if update.message.text != "/stop":
message = (
f"✔ Successfully edited coin list file to:\n\n"
f"```\n"
f"{update.message.text}\n"
f"```".replace(".", "\.")
)
coin_file_path = os.path.join(settings.ROOT_PATH, "supported_coin_list")
try:
copyfile(coin_file_path, f"{coin_file_path}.backup")
with open(coin_file_path, "w") as f:
f.write(update.message.text + "\n")
except Exception:
message = "❌ Unable to edit coin list file\."
else:
message = "👌 Exited without changes\.\nYour `supported_coin_list` file was *not* modified\."
keyboard = [["Go back"]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
update.message.reply_text(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
return MENU
def edit_user_config(update: Update, _: CallbackContext) -> int:
logger.info(f"Editing user configuration. ({update.message.text})")
if update.message.text != "/stop":
message = (
f"✔ Successfully edited user configuration file to:\n\n"
f"```\n"
f"{update.message.text}\n"
f"```".replace(".", "\.")
)
user_cfg_file_path = os.path.join(settings.ROOT_PATH, "user.cfg")
try:
copyfile(user_cfg_file_path, f"{user_cfg_file_path}.backup")
with open(user_cfg_file_path, "w") as f:
f.write(update.message.text + "\n\n\n")
except Exception:
message = "❌ Unable to edit user configuration file\."
else:
message = (
"👌 Exited without changes\.\n" "Your `user.cfg` file was *not* modified\."
)
keyboard = [["Go back"]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
update.message.reply_text(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
return MENU
def delete_db(update: Update, _: CallbackContext) -> int:
logger.info(
f"Asking if the user really wants to delete the db. ({update.message.text})"
)
if update.message.text != "Go back":
message = "✔ Successfully deleted database file\."
db_file_path = os.path.join(settings.ROOT_PATH, "data/crypto_trading.db")
try:
copyfile(db_file_path, f"{db_file_path}.backup")
os.remove(db_file_path)
except Exception:
message = "❌ Unable to delete database file\."
else:
message = "👌 Exited without changes\.\n" "Your database was *not* deleted\."
keyboard = [["OK"]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
update.message.reply_text(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
return MENU
def update_tg_bot(update: Update, _: CallbackContext) -> int:
logger.info(f"Updating BTB Manager Telegram. ({update.message.text})")
if update.message.text != "Cancel update":
message = (
"The bot is updating\.\n"
"Wait a few seconds then start the bot again with /start"
)
keyboard = [["/start"]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
update.message.reply_text(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
try:
subprocess.call(
"git pull && $(which python3) -m pip install -r requirements.txt --upgrade && "
"$(which python3) -m btb_manager_telegram &",
shell=True,
)
kill_btb_manager_telegram_process()
except Exception:
message = "Unable to update BTB Manager Telegram"
update.message.reply_text(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
else:
message = (
"👌 Exited without changes\.\n" "BTB Manager Telegram was *not* updated\."
)
keyboard = [["OK 👌"]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
update.message.reply_text(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
return MENU
def update_btb(update: Update, _: CallbackContext) -> int:
logger.info(f"Updating Binance Trade Bot. ({update.message.text})")
keyboard = [["OK 👌"]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
if update.message.text != "Cancel update":
message = (
"The bot is updating\.\n"
"Wait a few seconds, the bot will restart automatically\."
)
update.message.reply_text(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
try:
find_and_kill_binance_trade_bot_process()
subprocess.call(
f"cd {settings.ROOT_PATH} && "
f"git pull && "
f"$(which python3) -m pip install -r requirements.txt --upgrade && "
f"$(which python3) -m binance_trade_bot &",
shell=True,
)
except Exception:
message = "Unable to update Binance Trade Bot"
update.message.reply_text(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
else:
message = "👌 Exited without changes\.\n" "Binance Trade Bot was *not* updated\."
update.message.reply_text(
message, reply_markup=reply_markup, parse_mode="MarkdownV2"
)
return MENU
def cancel(update: Update, _: CallbackContext) -> int:
logger.info("Conversation canceled.")
update.message.reply_text(
"Bye! I hope we can talk again some day.",
reply_markup=ReplyKeyboardRemove(),
)
return ConversationHandler.END
MENU_HANDLER = MessageHandler(
Filters.regex(
"^(Begin|💵 Current value|📈 Progress|➗ Current ratios|🔍 Check bot status|⌛ Trade History|🛠 Maintenance|"
"⚙️ Configurations|▶ Start trade bot|⏹ Stop trade bot|📜 Read last log lines|❌ Delete database|"
"⚙ Edit user.cfg|👛 Edit coin list|📤 Export database|Update Telegram Bot|Update Binance Trade Bot|"
"⬅️ Back|Go back|OK|Cancel update|OK 👌)$"
),
menu,
)
ENTRY_POINT_HANDLER = CommandHandler(
"start", start, Filters.user(user_id=eval(settings.USER_ID))
)
EDIT_COIN_LIST_HANDLER = MessageHandler(Filters.regex("(.*?)"), edit_coin)
EDIT_USER_CONFIG_HANDLER = MessageHandler(Filters.regex("(.*?)"), edit_user_config)
DELETE_DB_HANDLER = MessageHandler(Filters.regex("^(⚠ Confirm|Go back)$"), delete_db)
UPDATE_TG_HANDLER = MessageHandler(
Filters.regex("^(Update|Cancel update)$"), update_tg_bot
)
UPDATE_BTB_HANDLER = MessageHandler(
Filters.regex("^(Update|Cancel update)$"), update_btb
)
FALLBACK_HANDLER = CommandHandler("cancel", cancel)