-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
695 lines (639 loc) · 29.4 KB
/
main.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
import time
import telebot
import logging
from config import *
from funcs import *
from buttons import *
from messages import *
from pymongo import MongoClient
bot = telebot.TeleBot(BOT_TOKEN)
client = MongoClient(
"mongodb+srv://usanumberplug:[email protected]/?retryWrites=true&w=majority"
"&appName=InfinityShop")
db = client.TGEscrow
users = db.users
trades = db.trades
successful = db.successfultrades
user_messages = {}
forwarded_messages = {}
logging.basicConfig(level=logging.INFO)
# SOME FUNCS
def check_user_in_channels(user_id, channels):
results = {}
for channel in channels:
try:
member_status = bot.get_chat_member(channel, user_id)
if member_status.status in ['member', 'administrator', 'creator']:
results[channel] = 'Present'
else:
results[channel] = 'Not a member'
except telebot.apihelper.ApiTelegramException as e:
results[channel] = f'Error: {e.result_json["description"]}'
return results
def get_user_first_name(user_id):
try:
user_info = bot.get_chat(user_id)
return user_info.first_name
except telebot.apihelper.ApiTelegramException as e:
print(f"Error: {e.result_json['description']}")
return None
@bot.message_handler(commands=['start'])
def start_handler(message):
sender = message.from_user.id
fname = message.from_user.first_name
command_args = message.text.split()
try:
check_results = check_user_in_channels(sender, MUST_JOIN)
not_in_channels = [channel for channel, status in check_results.items() if status != 'Present']
if not_in_channels:
missing_channels = ', '.join(not_in_channels)
bot.send_message(
message.chat.id,
f"To use this bot, you need to join the following channels:\n{missing_channels}\nPlease join and try "
f"again."
)
else:
if len(message.text.split()) > 1:
parameter = message.text.split()[1]
if parameter.startswith('trade_'):
trade_code = parameter.split('_')[1]
bot.send_message(
message.chat.id,
f"Trade Code Received: {trade_code}\nConnecting with your Trading Partner..."
)
start_new_trade_func(message, trade_code)
else:
bot.send_message(
message.chat.id,
"Invalid parameter received."
)
else:
bot.send_message(
message.chat.id,
START_MESSAGE.format(fname, bot.get_me().first_name),
reply_markup=terms_regulations(),
parse_mode="Markdown"
)
except telebot.apihelper.ApiTelegramException as e:
bot.send_message(message.chat.id, f"Error Occurred: {e}")
@bot.message_handler(commands=['register'])
def registration(message):
sender = message.from_user.id
fname = message.from_user.first_name
try:
check_results = check_user_in_channels(sender, MUST_JOIN)
not_in_channels = [channel for channel, status in check_results.items() if status != 'Present']
if not_in_channels:
missing_channels = ', '.join(not_in_channels)
bot.send_message(
message.chat.id,
f"To use this bot, you need to join the following channels:\n{missing_channels}\nPlease join and try "
f"again."
)
else:
bot.send_message(
message.chat.id,
f"Hello {fname},\n"
f"To register, simply send us your wallet address where you will receive your funds. "
f"We recommend verified wallets like Binance, OKX or Bitget.\n\n"
f"To proceed, please send your LiteCoin[LTC] wallet address. Additional coins will be supported in the "
f"future."
)
bot.register_next_step_handler(message, add_usdt_address)
except telebot.apihelper.ApiTelegramException as e:
bot.send_message(message.chat.id, f"Error Occurred: {e}")
def add_usdt_address(message):
sender = message.from_user.id
fname = message.from_user.first_name
address = message.text.strip()
registered = users.find_one({'_id': sender})
if registered:
bot.send_message(message.chat.id, f"You are already registered in our database.\nTo change your address, "
f"send /update.")
else:
valid = verify_address(address)
if valid:
users.insert_one({
"_id": sender,
'address': address
})
bot.send_message(
message.chat.id,
f"Hello {fname}, \n"
f"Address: {address} has been added to your account. You are now set to start your selling and buying.\n"
f"Initiate a new trade with /newtrade"
)
bot.send_message(
message.chat.id,
GET_CRYPTO,
parse_mode='Markdown'
)
else:
bot.send_message(
message.chat.id,
f"Please provide a valid LTC Address."
)
# To Initialize a new trade between two parties.
@bot.message_handler(commands=['newtrade'])
def newt_rade(message):
sender = message.from_user.id
fname = message.from_user.first_name
try:
# Check if the user is in the required channels
check_results = check_user_in_channels(sender, MUST_JOIN)
not_in_channels = [channel for channel, status in check_results.items() if status != 'Present']
if not_in_channels:
# Notify user of missing channels and exit
missing_channels = ', '.join(not_in_channels)
bot.send_message(
message.chat.id,
f"To use this bot, you need to join the following channels:\n{missing_channels}\n"
f"Please join and try again."
)
return # Exit after notifying about missing channels
# Check if the user already has an active trade
get_trade = trades.find_one({
"$or": [
{"partyone": sender},
{"partytwo": sender}
]
})
if get_trade:
# Check if the trade is active
active = get_trade.get('active', False)
if active:
bot.send_message(
message.chat.id,
"Sorry, You can have a single trade active at once."
)
return # Exit since the trade is active
# Create a new trade if no active trade exists
code = generate_linking_code() # Ensure this function generates unique codes
trades.insert_one({
"_id": code,
"partyone": sender,
"active": True # Optional: Set as active if your logic requires it
})
bot.send_message(
message.chat.id,
f"Hello {fname},\n"
f"Please use this link to initiate a new trade. Send it to the other partner to establish a connection.\n\n"
f"Trade Link: `https://t.me/{bot.get_me().username}?start=trade_{code}` (Click to Copy)\n"
f"Wishing you safe trading on Telegram!",
parse_mode="Markdown"
)
except telebot.apihelper.ApiTelegramException as e:
bot.send_message(message.chat.id, f"Error Occurred: {e}")
except Exception as e:
# Catch other potential exceptions, such as database errors
bot.send_message(message.chat.id, f"An unexpected error occurred: {e}")
def start_new_trade_func(message, trade_id):
sender = message.from_user.id
fname = message.from_user.first_name
trade = trades.find_one({"_id": trade_id})
if trade:
party_one = trade.get('partyone')
party_two = trade.get('partytwo')
if party_two:
bot.reply_to(message, "The trade is already full. A trade code can only be used between two parties")
else:
trades.update_one({"_id": trade_id},
{"$set": {'partytwo': sender, 'active': True}})
bot.send_message(message.chat.id,
f"Hello {fname}, \n"
f"Successfully Connected to Your Partner.")
bot.send_message(party_one,
f"Hello {get_user_first_name(party_one)}, \n"
f"Your new trading partner is {fname}."
)
else:
bot.send_message(
message.chat.id,
"Invalid Trade ID"
)
@bot.message_handler(commands=['requestpayment'])
def create_payout(message):
sender = message.from_user.id
fname = message.from_user.first_name
try:
check_results = check_user_in_channels(sender, MUST_JOIN)
not_in_channels = [channel for channel, status in check_results.items() if status != 'Present']
if not_in_channels:
missing_channels = ', '.join(not_in_channels)
bot.send_message(
message.chat.id,
f"To use this bot, you need to join the following channels:\n{missing_channels}\nPlease join and try "
f"again."
)
else:
get_trade = trades.find_one({
"$or": [
{"partyone": sender},
{"partytwo": sender}
]
})
if get_trade:
active = get_trade.get('active',
False)
if active:
party_one = get_trade.get('partyone')
party_two = get_trade.get('partytwo')
if not party_two:
bot.send_message(
message.chat.id,
"Sorry, You have an active trade but no trading partner. Share your link to create payout"
)
else:
partytwo = users.find_one({"_id": party_two})
partyone = users.find_one({"_id": party_one})
if not partyone or not partytwo:
bot.send_message(
message.chat.id,
"Please make sure that both of you (Active on this trade) have started the bot and "
"registered."
)
else:
bot.send_message(
message.chat.id,
"Please set the amount to be paid by the buyer."
)
bot.register_next_step_handler(message, request_payment)
else:
bot.send_message(
message.chat.id,
"Sorry, You do not have an active trade. sTART one using /newtrade"
)
else:
# Handle the case where no trade was found
bot.send_message(
message.chat.id,
"No trade found. Please create a new trade using /newtrade."
)
except telebot.apihelper.ApiTelegramException as e:
bot.send_message(message.chat.id, f"Error Occurred: {e}")
def request_payment(message):
sender = message.from_user.id
fname = message.from_user.first_name
amount = message.text.strip()
if not float(amount):
bot.reply_to(message, "Please enter a valid amount.")
else:
# new_amount = calculate_total_deposit(amount)
trade = trades.find_one({
"$or": [
{"partyone": sender},
{"partytwo": sender}
]
})
partyone = trade.get('partyone')
partytwo = trade.get('partytwo')
if trade:
try:
status, address, qrcode, track_id = generate_payment_request(amount)
if status != 100:
bot.send_message(
message.chat.id,
"Request was unsuccessful, Please try again later."
)
else:
if message.from_user.id == partyone:
trades.update_one({"partyone": partyone},
{"$set": {'amount': amount}})
bot.send_photo(
partytwo,
photo=qrcode,
caption=f"Payment request successful.\n\n"
f"Please send your funds to this address within the next hour. After one hour, "
f"any funds sent will be considered lost.\n"
f"Address: `{address}` (CLICK TO COPY)\n\n"
f"You can also scan the QR code in any wallet to deposit the funds directly.",
parse_mode='Markdown',
reply_markup=check_deposit_status(track_id)
)
bot.send_photo(
message.chat.id,
photo=qrcode,
caption=f"Your payment request has been sent successfully.\n\n"
f"Use this tracking ID to monitor your partner's progress: `{track_id}`\n"
f"An address has been sent to your partner's account.\n\n"
f"They can also scan the QR code in any wallet to deposit the funds directly.",
parse_mode='Markdown',
reply_markup=check_deposit_status(track_id)
)
elif message.from_user.id == partytwo:
trades.update_one({"partytwo": partytwo},
{"$set": {'amount': amount}})
bot.send_photo(
partyone,
photo=qrcode,
caption=f"Payment request successful.\n\n"
f"Please send your funds to this address within the next hour. After one hour, "
f"any funds sent will be considered lost.\n"
f"Address: `{address}` (CLICK TO COPY\n\n"
f"You can also scan the QR code in any wallet to deposit the funds directly.",
parse_mode='Markdown',
reply_markup=check_deposit_status(track_id)
)
bot.send_photo(
message.chat.id,
photo=qrcode,
caption=f"Your payment request has been sent successfully.\n\n"
f"Use this tracking ID to monitor your partner's progress: `{track_id}`\n"
f"An address has been sent to your partner's account.\n\n"
f"They can also scan the QR code in any wallet to deposit the funds directly.",
parse_mode='Markdown',
reply_markup=check_deposit_status(track_id)
)
except telebot.apihelper.ApiTelegramException as e:
bot.send_message(
message.chat.id,
f"Error Occurred: {e}."
)
def check_payment_statuses(message, track_id):
status = check_payment_status(track_id)
if status != 100:
bot.send_message(
message.chat.id,
"Payment not received yet."
)
else:
bot.send_message(
message.chat.id,
f"Deposit was successful.\n\nPlease release the service/good to receive your payment"
)
def send_report(message):
pass
@bot.callback_query_handler(func=lambda call: True)
def callback_handler(call):
if call.data == "tnc":
bot.send_message(
call.message.chat.id,
TERMS_AND_CONDITIONS,
parse_mode='Markdown',
reply_markup=accept_regulations()
)
elif call.data == "accept":
bot.send_message(
call.message.chat.id,
"Thanks for accepting to the TOS. To proceed, Send the /register command."
)
elif call.data == "reject":
bot.send_message(
call.message.chat.id,
"Sorry to see that you cannot compile to our Terms and Conditions.Please block this bot"
)
elif call.data.startswith("status_"):
try:
trackid = call.data.split("_")[1]
status, resp = check_payment_status(trackid)
if status != 100:
bot.send_message(
call.message.chat.id,
f"Payment not received yet.Check again later.\n Current Response: {resp}"
)
elif status == 100:
if resp == "Waiting":
bot.send_message(
call.message.chat.id,
f"Your payment have been successfully received.\n Current Response: {resp}"
)
elif resp == "Rejected":
bot.send_message(
call.message.chat.id,
f"Your payment has been rejected.\n Current Response: {resp}"
)
elif resp == "Confirming":
bot.send_message(
call.message.chat.id,
f"Your payment has been confirmed.\n Current Response: {resp}"
)
elif resp == "Paid":
bot.send_message(
call.message.chat.id,
f"The payment has been processed. The funds will be released to the seller upon your "
f"confirmation of delivery. Current Response: {resp}.\n\n"
f"To the seller: You can now deliver the goods/services to the buyer. The buyer should verify "
f"the received items. If satisfied, please send /release.\n\n"
f"The seller can also initiate the /release command and await the buyer's confirmation.",
reply_markup=finalize_trade()
)
except telebot.apihelper.ApiTelegramException as e:
bot.send_message(
call.message.chat.id,
"Error Occurred: {e}."
)
elif call.data == "report":
bot.send_message(
call.message.chat.id,
"Please send us your complains.(It can be either photo or text We will take an action as soon as possible."
)
bot.register_next_step_handler(call.message, send_report)
elif call.data == 'finalize':
sender = call.message.from_user.id
fname = call.message.from_user.first_name
try:
get_trade_id = trades.find_one({
"$or": [
{"partyone": sender},
{"partytwo": sender}
]
})
if get_trade_id:
trade_id = get_trade_id.get("_id")
partyone = get_trade_id.get("partyone")
partytwo = get_trade_id.get("partytwo")
amount = get_trade_id.get("amount")
party_one = users.find_one({'_id': partyone})
party_two = users.find_one({'_id': partytwo})
pone_address = party_one.get("address")
ptwo_address = party_two.get("address")
try:
if sender == partyone:
status, track_id, result = create_payout_to_seller(ptwo_address, amount)
if status != 100:
bot.send_message(
call.message.chat.id,
f"Some error occurred.\n Current Response: {result}\nPlease forward this message to"
f"the support."
)
elif status == 100:
if status != 100:
bot.send_message(
call.message.chat.id,
f"Payment has been Initiated.Check status by clicking below button.\n Current "
f"Response: {result}\n",
reply_markup=check_pay_status(track_id)
)
if sender == partytwo:
status, track_id, result = create_payout_to_seller(pone_address, amount)
if status != 100:
bot.send_message(
call.message.chat.id,
f"Some error occurred during initiation of the payment.\n Current Response: {result}\n"
f"Please forward this message to"
f"the support."
)
elif status == 100:
if status != 100:
bot.send_message(
call.message.chat.id,
f"Payment has been Initiated.Check status by clicking below button.\n Current "
f"Response: {result}\n",
reply_markup=check_pay_status(track_id)
)
except telebot.apihelper.ApiTelegramException as e:
bot.send_message(
call.message.chat.id,
f"Error Occurred: {e}."
)
except Exception as e:
bot.send_message(
call.message.chat.id,
f"Error Occurred: {e}."
)
elif call.data.startswith('pstatus_'):
sender = call.message.from_user.id
get_trade_id = trades.find_one({
"$or": [
{"partyone": sender},
{"partytwo": sender}
]
})
if get_trade_id:
trade_id = get_trade_id.get("_id")
partyone = get_trade_id.get("partyone")
partytwo = get_trade_id.get("partytwo")
amount = get_trade_id.get("amount")
party_one = users.find_one({'_id': partyone})
party_two = users.find_one({'_id': partytwo})
pone_address = party_one.get("address")
ptwo_address = party_two.get("address")
try:
trackid = call.data.split("_")[1]
status, result = check_pay_status(trackid)
if sender == partyone:
status, track_id, result = create_payout_to_seller(ptwo_address, amount)
if status != 100:
bot.send_message(
call.message.chat.id,
f"Some error occurred.\n Current Response: {result}\nPlease forward this message to"
f"the support."
)
elif status == 100:
if result == "Processing":
bot.send_message(
call.message.chat.id,
f"Please be patient as we process your payment.\n Current Response: {result}"
)
elif result == "Confirming":
bot.send_message(
call.message.chat.id,
"Your payment has been sent and is awaiting blockchain confirmation. Please be patient.\n"
f"Current Response: {result}"
)
elif result == "Complete":
trades.update_one({"partyone": partyone},
{"$set": {'active': False}})
bot.send_message(
call.message.chat.id,
"Please check your wallet to confirm your payment.\n"
f"Current Response: {result}. If not yet, Please wait and keep checking.\n"
f"You can contact support if no payment is received after 30 minutes"
)
mess = f"""
***New Trade Finalized.***
***Status:*** _{result}_
***Trade ID:*** _{trade_id}_
***Party One:*** _{party_one}_
***Party Two:*** _{party_two}_
***Amount:*** _{amount}_
***P.O Address:*** _{pone_address}_
***P.T Address:*** _{ptwo_address}_
"""
bot.send_message(
LOGS_CHANNEL,
mess,
parse_mode="Markdown",
)
elif sender == party_two:
status, track_id, result = create_payout_to_seller(pone_address, amount)
if status != 100:
bot.send_message(
call.message.chat.id,
f"Some error occurred.\n Current Response: {result}\nPlease forward this message to"
f"the support."
)
elif status == 100:
if result == "Processing":
bot.send_message(
call.message.chat.id,
f"Please be patient as we process your payment.\n Current Response: {result}"
)
elif result == "Confirming":
bot.send_message(
call.message.chat.id,
"Your payment has been sent and is awaiting blockchain confirmation. Please be "
"patient.\n"
f"Current Response: {result}"
)
elif result == "Complete":
trades.update_one({"partytwo": partytwo},
{"$set": {'active': False}})
bot.send_message(
call.message.chat.id,
"Please check your wallet to confirm your payment.\n"
f"Current Response: {result}. If not yet, Please wait and keep checking.\n"
f"You can contact support if no payment is received after 30 minutes"
)
mess = f"""
***New Trade Finalized.***
***Status:*** ___{result}___
***Trade ID:*** ___{trade_id}___
***Party One:*** ___{party_one}___
***Party Two:*** ___{party_two}___
***Amount:*** ___{amount}___
***P.O Address:*** ___{pone_address}___
***P.T Address:*** ___{ptwo_address}___
"""
bot.send_message(
LOGS_CHANNEL,
mess,
parse_mode="Markdown",
)
except Exception as e:
bot.send_message(
call.message.chat.id,
f"Error: {e}"
)
@bot.message_handler(func=lambda message: message.chat.type == 'private',
content_types=['text', 'photo', 'audio', 'document', 'video', 'voice', 'video_note', 'sticker',
'location', 'contact'])
def handle_all_messages(message):
if message.chat.id in ADMINS_ID:
# Handle admin's reply
if message.reply_to_message and message.reply_to_message.message_id in forwarded_messages:
original_message_id = forwarded_messages[message.reply_to_message.message_id]
original_user_id = user_messages[original_message_id]['chat_id']
# Forward the appropriate content type back to the user
if message.content_type == 'text':
bot.send_message(original_user_id, message.text)
else:
bot.copy_message(original_user_id, message.chat.id, message.message_id)
print(f"Admin {message.chat.id} replied to user {original_user_id}")
else:
bot.reply_to(message, "Please reply to a forwarded user message.")
else:
original_message_id = message.message_id
user_messages[original_message_id] = {
'chat_id': message.chat.id,
'content_type': message.content_type,
'user_id': message.from_user.id
}
for admin in ADMINS_ID:
forwarded_message = bot.forward_message(admin, message.chat.id, original_message_id)
forwarded_messages[forwarded_message.message_id] = original_message_id
# bot.reply_to(message, "Your message has been forwarded to an admin. Please wait for a response.")
print(f"User {message.chat.id} message forwarded to admin(s)")
if __name__ == '__main__':
print(f"Bot started with id {bot.get_me().id}")
bot.infinity_polling()