This repository has been archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qiwi.py
284 lines (239 loc) · 12.3 KB
/
qiwi.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
from __future__ import annotations
import logging
from datetime import datetime
# from glQiwiApi.qiwi.clients.wallet.types import TransactionType, Transaction
from pyrogram import Client, filters
from pyrogram.types import Message
# noinspection PyUnresolvedReferences
from utils.misc import modules_help, prefix
# from utils.scripts import format_exc
from utils.scripts import import_library, format_exc
# noinspection PyUnresolvedReferences
from utils.db import db
# from glQiwiApi import QiwiWallet, QiwiP2PClient
QiwiApi = import_library("glQiwiApi")
QiwiWallet = QiwiApi.QiwiWallet
QiwiP2PClient = QiwiApi.QiwiP2PClient
TransactionType = QiwiApi.qiwi.clients.wallet.types.TransactionType
Transaction = QiwiApi.qiwi.clients.wallet.types.Transaction
class P2pNotInitializedError(Exception):
pass
p2p_error = P2pNotInitializedError('Пожалуйста, привяжите p2p токен: .qp2p [token]')
class BasicQiwi:
def __init__(self, token: str = None, number: str = None,
secret_p2p: str = None):
self.token = token
self.number = number
self.secret_p2p = secret_p2p
self.qiwi = QiwiWallet(api_access_token=self.token,
phone_number=self.number)
if self.secret_p2p:
try:
self.p2p = QiwiP2PClient(secret_p2p=self.secret_p2p)
except Exception as ex:
self.p2p = None
logging.warning('Failed to create P2P client: %s', ex)
async def set_p2p(self, secret_p2p: str):
self.p2p = QiwiP2PClient(secret_p2p=secret_p2p)
self.secret_p2p = secret_p2p
@property
async def balance(self):
await self.qiwi.get_list_of_balances()
return await self.qiwi.get_balance()
@property
async def nickname(self):
return await self.qiwi.get_nickname()
async def get_history(self, limit: int = 5,
trans_type: TransactionType | str = TransactionType.ALL,
start_date: datetime = None,
end_date: datetime = None, offset: int = 0):
result = rsource = await self.qiwi.history(rows=limit,
transaction_type=trans_type,
start_date=start_date,
end_date=end_date)
if offset and len(result) // offset > 0:
result = []
for i in range(0, len(result), offset):
result.append(rsource[i])
return result
@staticmethod
async def trans_to_str(transaction: Transaction):
return f'<b>Транзакция:</b> <code>#{transaction.id} ({transaction.type})</code>\n' \
f'<b>Дата:</b> <code>{transaction.date.strftime("%d.%m.%Y %H:%M")}</code>\n' \
f'<b>Сумма:</b> <code>{transaction.total}</code>\n' \
f'<b>Куда:</b> <code>{transaction.to_account}</code>'
@property
async def last_trans(self):
trans = await self.get_history(limit=1)
if len(trans) == 0:
return "⛔ Не найдено транзакций."
return await self.trans_to_str(trans[0])
async def send_money(self, number: str, amount: int | float | str,
comment: str = None):
return await self.qiwi.transfer_money(to_phone_number=number, amount=amount,
comment=comment)
async def send_card(self, number: str, amount: int | float | str):
return await self.qiwi.transfer_money_to_card(card_number=number, amount=amount)
async def create_p2p(self, amount: int | float | str,
bill_id: str = None, comment: str = None):
if self.p2p is None:
raise p2p_error
return await self.p2p.create_p2p_bill(amount=amount,
bill_id=bill_id,
comment=comment)
async def get_p2p_bill(self, bill_id: str):
if self.p2p is None:
raise p2p_error
bill = await self.p2p.get_bill_by_id(bill_id=bill_id)
if bill is None:
return "<b>⛔ Счёт не найден.</b>"
status = "✅ Оплачен" if bill.status == "PAID" else "💤 Ожидает оплаты" \
if bill.status == "WAITING" else "" if bill.status == "EXPIRED" else "🚫 Отклонён"
return f'<b>Платеж</b> <code>#{bill.id}</code>:\n' \
f'<b>Сумма:</b> {bill.amount} руб.\n' \
f'<b>Статус:</b> {status}'
@property
async def text(self):
return f'<b>Телефон:</b> <code>{self.number}</code>\n' \
f'<b>Баланс:</b> <code>{await self.balance}</code>\n' \
f'<b>Никнейм:</b> <code>{await self.nickname}</code>\n' \
f'<b>Последняя транзакция:</b>\n{await self.last_trans}'
async def get_limits(self):
result = await self.qiwi.get_limits()
text = ''
for name, limit in result.items():
text += f'<b>{name}</b>: <code>{limit}</code>\n'
return text
class Config:
qiwis = db.get('lordcodes.qiwi', 'token')
if qiwis:
qiwi = BasicQiwi(token=qiwis, number=db.get('lordcodes.qiwi', 'number'),
secret_p2p=db.get('lordcodes.qiwi', 'secret_p2p'))
else:
qiwi = None
commands = ['qiwi', 'qset', 'qp2p', 'qdel',
'qsend', 'qcard', 'qcheck', 'qhistory',
'qlimits']
# noinspection PyUnusedLocal
@Client.on_message(
filters.command(commands, prefix) & filters.me
)
async def qiwi_handler(client: Client, message: Message):
try:
cmd = message.command[0]
args = message.command[1:]
await message.edit('Обрабатываю команду...')
if cmd == 'qset':
if len(args) < 2:
return await message.edit(f'<b>Используйте:</b> <code>{prefix}qset *[token] *[number] [p2p]</code>')
p2p = args[2] if len(args) > 2 else None
try:
Config.qiwi = BasicQiwi(token=args[0], number=args[1], secret_p2p=p2p)
except Exception as ex:
Config.qiwi = None
return await message.edit('<b>Не удалось создать клиент Qiwi:</b>\n'
f'{format_exc(ex)}')
db.set('lordcodes.qiwi', 'token', args[0])
db.set('lordcodes.qiwi', 'number', args[1])
if p2p:
db.set('lordcodes.qiwi', 'secret_p2p', p2p)
return await message.edit('<b>Настройки qiwi сохранены.</b>')
elif cmd == 'qcheck':
if len(args) < 1:
return await message.edit(f'<b>Используйте:</b> <code>{prefix}qcheck *[token]</code>')
token = args[0]
try:
client = BasicQiwi(token=token)
return await message.edit(f'<b>✅ Токен</b> <code>{token}</code> <b>валид.</b>')
except Exception as ex:
return await message.edit(f'<b>⛔ Токен</b> <code>{token}</code> <b>невалид.</b>\n'
f'{format_exc(ex)}')
if Config.qiwi is None:
return await message.edit('<b>Вы не создали клиент. Введите:</b> '
'<code>{prefix}qset *[token] *[number] [p2p]</code>')
if cmd == 'qiwi':
return await message.edit(await Config.qiwi.text)
elif cmd == 'qp2p':
if len(args) < 1:
return await message.edit(f'<b>Используйте:</b> <code>{prefix}qp2p *[secret_p2p]</code>')
try:
await Config.qiwi.set_p2p(args[0])
db.set('lordcodes.qiwi', 'secret_p2p', args[0])
except Exception as ex:
return await message.edit('<b>⛔ Не удалось установить p2p secret token.</b>\n'
f'{format_exc(ex)}')
elif cmd == 'qdel':
db.set('lordcodes.qiwi', 'token', None)
db.set('lordcodes.qiwi', 'number', None)
db.set('lordcodes.qiwi', 'secret_p2p', None)
return await message.edit('<b>Данные от Qiwi удалены с бд.</b>')
elif cmd == 'qlimits':
if len(args) > 0:
token = args[0]
try:
client = BasicQiwi(token=token)
except Exception as ex:
return await message.edit(f'<b>⛔ Не удалось создать клиент с токеном:'
f'</b> <code>{args[0]}</code>\n'
f'{format_exc(ex)}')
else:
client = Config.qiwi
text = f'<b>Лимиты для клиента:</b> <code>{client.number if client.number else client.token}</code>\n' \
f'{await client.get_limits()}'
return await message.edit(text)
elif cmd in ['qsend', 'qcard']:
if len(args) < 2:
return await message.edit(f'<b>Используйте:</b> <code>{prefix}qsend [номер] [сумма] *[коммент]</code>')
try:
amount = float(args[1].replace(',', '.'))
except ValueError:
return await message.edit('<b>Сумма должна быть числом.</b>')
if amount < 1:
return await message.edit('<b>Сумма должна быть больше 0.</b>')
try:
comment = args[2] if len(args) > 2 else None
await Config.qiwi.send_money(number=args[0], amount=amount, comment=comment) if cmd == 'qsend' else \
await Config.qiwi.send_card(number=args[0], amount=amount)
await message.edit(f'<b>✅ Отправлено:</b> <code>{amount}</code> <b>на номер</b> <code>{args[0]}</code>')
except Exception as ex:
return await message.edit('<b>Не удалось отправить деньги:</b>\n'
f'{format_exc(ex)}')
elif cmd == 'qhistory':
if len(args) > 0:
a = args[0].lower()
typ = 'in' if 'in' in a else 'out' if 'out' in a else TransactionType.ALL
else:
typ = TransactionType.ALL
if len(args) > 1:
try:
limit = int(args[1])
except ValueError:
limit = 5
else:
limit = 5
if len(args) > 2:
try:
offset = int(args[2])
except ValueError:
offset = 0
else:
offset = 0
history = await Config.qiwi.get_history(limit=limit, offset=offset,
trans_type=typ)
text = '\n➖➖➖➖➖➖➖➖➖\n'.join(await BasicQiwi.trans_to_str(_i) for _i in history)[:4096]
return await message.edit(text)
else:
return await message.edit(f'<b>Используйте:</b> <code>{prefix}help qiwi</code> чтобы узнать команды модуля.')
except Exception as ex:
return await message.edit(f'<b>Qiwi module:</b>\n{format_exc(ex)}')
modules_help['qiwi'] = {
'qset *[токен] *[номер] [p2p]': 'Установить кошелек Qiwi',
'qp2p *[p2p]': 'Установить p2p secret token Qiwi',
'qdel': 'Удалить кошелёк с бд',
'qiwi': 'Получить информацию о кошельке',
'qsend *[номер] *[сумма] [коммент]': 'Отправить средства на кошелёк',
'qcard *[номер] *[сумма]': 'Отправить средства на карту',
'qhistory [in|out|all] [лимит] [offset]': 'Список транзакций (default: all, 5, 0)',
'qcheck *[токен]': 'Чекнуть токен на валидность',
'qlimits [токен]': 'Чекнуть токен или свой аккаунт на лимиты',
}