-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
441 lines (353 loc) · 15 KB
/
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
import django
from telegram import (Update, InlineKeyboardButton, InlineKeyboardMarkup, InlineQueryResultArticle,
InputTextMessageContent)
from telegram.ext import (CallbackContext, CommandHandler, ConversationHandler,
InlineQueryHandler, CallbackQueryHandler, MessageHandler, filters, Application)
from telegram.constants import ParseMode
from _helpers.telegram_service import InternalService
from _helpers.logging import logger
from secret import TELEGRAM_BOT_TOKEN
from django.conf import settings
from django.db import IntegrityError
import asyncio
django.setup()
from store.models import Book
from provider.tasks.download_task import download_book
from provider.services import RedirectService
from store.services import AccountCacheService
from account.models import User, Wallet, Plan, CryptoPayment
from account.services import PaymentCacheService
class Main:
@staticmethod
async def start(update: Update, context: CallbackContext):
message = update.message
user = message.from_user
first_usage = False
if not User.objects.filter(user_id=user.id).exists():
User.objects.create(user_id=user.id,
username=user.username,
fullname=user.full_name)
first_usage = True
try:
md5 = context.args[0]
return await Search.download(update, context)
except IndexError:
pass
except TypeError:
pass
user = User.objects.get(user_id=user.id)
keyboard = [
[
InlineKeyboardButton('جستجوی کتاب', switch_inline_query_current_chat='')
]
]
if not user.plan:
# keyboard += [
# [
# InlineKeyboardButton('خرید اشتراک ویژه', callback_data='PAYMENT')
# ]
# ]
pass
reply_markup = InlineKeyboardMarkup(keyboard)
await message.reply_photo(
photo='main_cover.jpg',
caption=settings.TELEGRAM_MESSAGES['start'],
reply_markup=reply_markup,
parse_mode=ParseMode.MARKDOWN_V2,
)
if first_usage:
await message.reply_video(
'help.MP4',
caption=settings.TELEGRAM_MESSAGES['first_help']
)
return settings.STATES['start']
@staticmethod
async def check_verification(message, md5):
user_id = message.from_user.id
verified = InternalService.is_user_verified(user_id=user_id)
if not verified:
advertisers = InternalService.should_join(user_id=user_id)
keyboard = [
[
InlineKeyboardButton(text=advertiser.channel_name,
url=f'https://t.me/{advertiser.channel_id}')
] for advertiser in advertisers
]
keyboard += [
[
InlineKeyboardButton(text='عضو شدم!',
url=f'https://t.me/bookbank_robot?start={md5}')
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await message.reply_text(
settings.TELEGRAM_MESSAGES['is_not_verified'],
reply_markup=reply_markup,
parse_mode=ParseMode.MARKDOWN
)
return False
return True
@staticmethod
async def blind_date(update: Update, context: CallbackContext):
message = update.message
user_id = message.from_user.id
@classmethod
async def menu(cls, update: Update, context: CallbackContext):
message = update.message
user_id = message.from_user.id
keyboard = [
[
InlineKeyboardButton('دانلود', callback_data='DOWNLOAD')
]
]
await message.reply_text(
'OK CLICK',
reply_markup=InlineKeyboardMarkup(keyboard)
)
return 1
@classmethod
async def help(cls, update: Update, context: CallbackContext):
message = update.message
user_id = message.from_user.id
await message.reply_video(
'help.MP4',
caption=settings.TELEGRAM_MESSAGES['help']
)
return 1
class Payment:
@staticmethod
async def payment(update: Update, context: CallbackContext) -> int:
query = update.callback_query
user_id = query.from_user.id
user = User.objects.get(user_id=user_id)
await query.answer()
if CryptoPayment.objects.filter(user_id=user.id,
approved=False).exists():
await query.message.reply_text(
settings.TELEGRAM_MESSAGES['have_false_payment']
)
return ConversationHandler.END
keyboard = [
[
InlineKeyboardButton('کریپتوکارنسی \U0001F4B0', callback_data='cryptocurrency')
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.message.reply_text(
settings.TELEGRAM_MESSAGES['payment'],
reply_markup=reply_markup,
parse_mode=ParseMode.MARKDOWN,
)
return settings.STATES['payment']
@staticmethod
async def plan_selection(update: Update, context: CallbackContext) -> int:
query = update.callback_query
user_id = query.from_user.id
mode = query.data
await query.answer()
keyboard = [
[
InlineKeyboardButton(str(plan), callback_data=plan.id)
] for plan in Plan.objects.filter(mode=mode)
]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(
settings.TELEGRAM_MESSAGES['plan'],
reply_markup=reply_markup,
parse_mode=ParseMode.MARKDOWN
)
return settings.STATES['plan']
@staticmethod
async def crypto_payment(update: Update, context: CallbackContext) -> int:
query = update.callback_query
user_id = query.from_user.id
plan_id = int(query.data)
cache_service = PaymentCacheService()
cache_service.cache_plan(user_id=user_id,
plan_id=plan_id)
await query.answer()
available_networks = set(Wallet.objects.all().values_list('network', flat=True))
network_to_label = dict(Wallet.NetworkChoices.choices)
keyboard = [
[
InlineKeyboardButton(network_to_label[network], callback_data=network)
] for network in available_networks
]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(
settings.TELEGRAM_MESSAGES['crypto_payment_network'],
reply_markup=reply_markup,
parse_mode=ParseMode.MARKDOWN,
)
return settings.STATES['crypto_payment']
@staticmethod
async def crypto_payment_deposit(update: Update, context: CallbackContext) -> int:
query = update.callback_query
user_id = query.from_user.id
network = query.data
cache_service = PaymentCacheService()
cache_service.cache_crypto_network(user_id=user_id,
network=network)
await query.answer()
wallet = Wallet.objects.filter(network=network).last()
plan_id = cache_service.get_plan(user_id=user_id)
if not plan_id:
await query.edit_message_text(
settings.TELEGRAM_MESSAGES['expired']
)
return ConversationHandler.END
plan = Plan.objects.get(pk=plan_id)
await query.edit_message_text(
settings.TELEGRAM_MESSAGES['crypto_payment_deposit'].format(price=plan.price,
wallet=wallet.address,
network=wallet.network),
parse_mode=ParseMode.MARKDOWN,
)
return settings.STATES['crypto_payment_trx']
@staticmethod
async def crypto_payment_save(update: Update, context: CallbackContext) -> int:
message = update.message
user_id = message.from_user.id
user = User.objects.get(user_id=user_id)
tx_hash = message.text
cache_service = PaymentCacheService()
plan_id = cache_service.get_plan(user_id=user_id)
network = cache_service.get_crypto_network(user_id=user_id)
if not plan_id or not network:
await message.reply_text(
settings.TELEGRAM_MESSAGES['expired']
)
return ConversationHandler.END
wallet = Wallet.objects.filter(network=network).last()
try:
CryptoPayment.objects.create(
user=user,
plan_id=plan_id,
transaction_hash=tx_hash.lower().strip(),
wallet=wallet
)
except IntegrityError as ie:
await logger.async_error(
str(ie)
)
await message.reply_text(
settings.TELEGRAM_MESSAGES['you_can\'t_do_it']
)
return ConversationHandler.END
await message.reply_text(
settings.TELEGRAM_MESSAGES['crypto_payment_save']
)
return ConversationHandler.END
class Search:
@staticmethod
async def download_inline(update: Update, context: CallbackContext):
from uuid import uuid4
query = update.inline_query.query
user = update.inline_query.from_user
if query == "":
return
results = [
InlineQueryResultArticle(
id=str(uuid4()),
title=book.title,
input_message_content=InputTextMessageContent(f'/download {book.md5}'),
thumb_url=book.cover_url,
description=f'{book.year + "-" if book.year else ""}{book.extension + "-" if book.extension else ""}'
f'{(book.filesize // 1000000) + 1}MB\n'
f'{book.authors}\n{book.publisher}\n{book.description}'
) for book in Book.objects.filter(document__exact=query).exclude(title__exact='').order_by('document')[:25]
]
response = await update.inline_query.answer(results)
await InternalService.send_info(context=context, info=f'[{user.full_name}](tg://user?id={user.id}) is searching'
f' for {query}! \U0001F60F')
return response
@staticmethod
async def download(update: Update, context: CallbackContext):
message = update.message
user_id = message.from_user.id
user = User.objects.get(user_id=user_id)
try:
md5 = context.args[0]
except IndexError:
return
except TypeError:
return
verified = await Main.check_verification(message=message, md5=md5)
if not verified and not user.plan:
return
account_service = AccountCacheService()
limit = account_service.get_limit(user_id=user_id)
if limit > settings.USER_DOWNLOAD_LIMIT and not user.plan:
await message.reply_text(
settings.TELEGRAM_MESSAGES['limited_download']
)
return
await message.reply_text(
settings.TELEGRAM_MESSAGES['waiting_for_download']
)
try:
book = Book.objects.get(md5=md5)
except Book.DoesNotExist:
return
if book.file:
message_id = book.file
await InternalService.send_info(context,
f'[{user.fullname}](tg://user?id={user.user_id}) is getting {book.title}'
f' from forwarding.')
await InternalService.forward_file(context=context,
file_id=message_id,
to=user_id)
elif book.filesize >= settings.DOWNLOAD_LIMIT_SIZE:
await InternalService.send_info(context,
f'[{user.fullname}](tg://user?id={user.user_id}) is getting '
f'{InternalService.markdown_escape(book.title)}'
f' from link.')
await message.reply_text(
settings.TELEGRAM_MESSAGES['redirect_url'].format(title=InternalService.markdown_escape(book.title[:100]),
extension=book.extension,
authors=InternalService.markdown_escape(book.authors[:50]),
publisher=InternalService.markdown_escape(book.publisher[:50]),
size=(book.filesize // 1000000) + 1,
url=RedirectService().generate_redirect_url(book)),
parse_mode=ParseMode.MARKDOWN
)
else:
asyncio.create_task(download_book(book, context=context, user=message.from_user))
account_service.incr_limit(user_id=user_id)
def main():
application = Application.builder().base_url('http://0.0.0.0:8081/bot').token(TELEGRAM_BOT_TOKEN).build()
# start_handler = CommandHandler('start', Main.start)
menu_handler = ConversationHandler(
entry_points=[CommandHandler('start', Main.start)],
states={
settings.STATES['start']: [
CallbackQueryHandler(Payment.payment, pattern=r'^PAYMENT$'),
],
settings.STATES['payment']: [
CallbackQueryHandler(Payment.plan_selection, pattern=r'^cryptocurrency$'),
],
settings.STATES['plan']: [
CallbackQueryHandler(Payment.crypto_payment, pattern=r'^\d+$'),
],
settings.STATES['crypto_payment']: [
CallbackQueryHandler(Payment.crypto_payment_deposit, pattern=r'^\w+\-?\d+$'),
],
settings.STATES['crypto_payment_trx']: [
MessageHandler(~ filters.COMMAND & filters.Regex(settings.REGEX_PATTERNS['transaction_hash']),
Payment.crypto_payment_save)
]
},
fallbacks=[CommandHandler('start', Main.start)]
)
download_handler = CommandHandler('download', Search.download)
help_handler = CommandHandler('help', Main.help)
# application.add_handler(start_handler)
application.add_handler(menu_handler)
application.add_handler(download_handler)
application.add_handler(help_handler)
application.add_handler(InlineQueryHandler(Search.download_inline))
# Start the Bot
application.run_polling()
if __name__ == '__main__':
django.setup()
main()