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/insert_account_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 choose_account(message: Message):
22
token = get_token(message.from_user.id)
23
24
choose_account = InlineKeyboardMarkup()
25
with Client(str(token)) as client:
26
acc = client.users.get_accounts()
27
acc_sand = client.sandbox.get_sandbox_accounts()
28
for i in acc.accounts:
29
if i.type == 1:
30
choose_account.add(InlineKeyboardButton(text=f"{i.name}", callback_data=f"account:{i.id}:{i.type}:{i.access_level}"))
31
for i in acc_sand.accounts:
32
choose_account.add(InlineKeyboardButton(text=f"Песочница", callback_data=f"account:{i.id}:sandbox:{i.access_level}"))
33
34
await message.answer("Внимание! Не забудьте остановить торговые стратегии (в случае необходимости) перед сменой "
35
"аккаунта!")
36
await message.answer("Выберите аккаунт:", reply_markup=choose_account)
37
38
39
"""
40
Второй хэндлер, который исполняется в состоянии s_wait_figi
41
"""
42
43
44
@dp.callback_query_handler(lambda c: c.data and c.data.startswith("account"))
45
async def account_finish(callback_query):
46
data = callback_query.data.split(":")
47
48
account_id = data[1]
49
account_type = data[2]
50
account_access = data[3]
51
52
connection = sl.connect("db/BotDB.db")
53
cursor = connection.cursor()
54
cursor.execute('UPDATE users SET account_id = ?, account_type = ?, account_access = ? WHERE user_id = ?;',
55
(str(account_id), str(account_type), str(account_access), callback_query.from_user.id))
56
connection.commit()
57
58
await bot.delete_message(chat_id=callback_query.from_user.id, message_id=callback_query.message.message_id)
59
await bot.send_message(chat_id=callback_query.from_user.id, text=f"Аккаунт успешно изменён!",
60
reply_markup=get_start_menu(user_id=callback_query.from_user.id))
61
62