Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_trading_bot-master/bot/handlers/sandbox_handlers.py
5936 views
1
from main import dp, bot
2
from aiogram.types import Message
3
from bot.keyboards.start_menu_keyboard import get_start_menu
4
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
5
import sqlite3 as sl
6
from tinkoff.invest import Client
7
from config.personal_data import get_token
8
9
"""
10
11
Тут представлены все хэндлеры, которые отвечают за создание или закрытие песочницы
12
13
"""
14
15
"""
16
Открытие песочницы
17
"""
18
19
20
@dp.message_handler(state="*", text="Открыть песочницу")
21
async def create_sandbox(message: Message):
22
with Client(get_token(message.from_user.id)) as client:
23
acc = client.sandbox.get_sandbox_accounts().accounts
24
if len(acc) == 0:
25
client.sandbox.open_sandbox_account()
26
await message.answer("Аккаунт песочницы успешно создан!")
27
else:
28
await message.answer("У Вас уже есть аккаунт в песочнице!",
29
reply_markup=get_start_menu(user_id=message.from_user.id))
30
31
32
"""
33
Закрытие песочницы
34
Выводит список всех аккаунтов
35
"""
36
37
38
@dp.message_handler(state="*", text="Закрыть песочницу")
39
async def delete_sandbox_start(message: Message):
40
with Client(get_token(message.from_user.id)) as client:
41
acc = client.sandbox.get_sandbox_accounts().accounts
42
43
if len(acc) == 0:
44
await message.answer("У Вас ещё нет аккаунта в песочнице!:",
45
reply_markup=get_start_menu(user_id=message.from_user.id))
46
else:
47
delete_sandbox = InlineKeyboardMarkup()
48
for i in acc:
49
delete_sandbox.add(InlineKeyboardButton(text=f"{i.id}", callback_data=f"sandbox:close:{i.id}"))
50
51
await message.answer("Выберите аккаунт:",
52
reply_markup=delete_sandbox)
53
54
55
"""
56
Закрытие аккаунта
57
Принимает id песочницы и закрывает её
58
"""
59
60
61
@dp.callback_query_handler(lambda c: c.data and c.data.startswith('sandbox:close'))
62
async def close_sandbox_finish(callback_query):
63
data = callback_query.data.split(":")
64
65
account_id = data[2]
66
67
with Client(get_token(callback_query.from_user.id)) as client:
68
client.sandbox.close_sandbox_account(account_id=account_id)
69
connection = sl.connect("db/BotDB.db")
70
cursor = connection.cursor()
71
72
cursor.execute('DELETE FROM str1_config WHERE user_id=? AND account_id = ?',
73
(callback_query.from_user.id, account_id))
74
75
connection.commit()
76
77
with Client(get_token(callback_query.from_user.id)) as client:
78
acc = client.sandbox.get_sandbox_accounts().accounts
79
80
if len(acc) == 0:
81
await bot.edit_message_text(chat_id=callback_query.from_user.id,
82
message_id=callback_query.message.message_id, text="Все аккаунты закрыты!")
83
else:
84
delete_sandbox = InlineKeyboardMarkup()
85
for i in acc:
86
delete_sandbox.add(InlineKeyboardButton(text=f"{i.id}", callback_data=f"sandbox:close:{i.id}"))
87
88
await bot.edit_message_text(chat_id=callback_query.from_user.id,
89
message_id=callback_query.message.message_id, text="Выберите аккаунт:",
90
reply_markup=delete_sandbox)
91
92