-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
1208 lines (1006 loc) · 47.1 KB
/
bot.py
File metadata and controls
1208 lines (1006 loc) · 47.1 KB
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import json
import datetime
import time
import requests
import pytz
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
import aiohttp
import config
import database
# Кеш для хранения отформатированных сообщений
_formatted_data_cache = {}
# Кэш для хранения статуса администратора (ключ: user_id, значение: {is_admin: bool, timestamp: time})
_admin_status_cache = {}
# Create scheduler for daily notifications
scheduler = AsyncIOScheduler(timezone=pytz.UTC)
# Создаем сессию один раз при запуске бота
session = None
async def setup_aiohttp_session():
global session
session = aiohttp.ClientSession()
# Function to log bot actions
async def log_bot_action(action, user_id=None, username=None):
"""Log bot actions and save to database if user_id is provided"""
timestamp = datetime.datetime.now().isoformat()
user_info = f"[USER:{user_id}|{username}]" if user_id else ""
print(f"[{timestamp}] {user_info} Bot Action: {action}")
# Логируем действие в базу данных
# (будет пропущено для админов и игнорируемых действий - см. log_user_action)
if user_id:
await database.log_user_action(action, user_id, username)
# Асинхронная функция для отправки запросов к Telegram API
async def send_telegram_request_async(method, params=None):
"""Send request to Telegram API using async client"""
global session
url = f"{config.TELEGRAM_API_URL}/{method}"
try:
async with session.post(url, json=params if params else {}) as response:
return await response.json()
except Exception as e:
print(f"Error sending Telegram request: {e}")
# Пробуем восстановить сессию, если она закрылась
if session.closed:
session = aiohttp.ClientSession()
async with session.post(url, json=params if params else {}) as response:
return await response.json()
raise
# Асинхронная обертка для синхронного кода
def send_telegram_request(method, params=None):
"""Create async task for telegram request and return placeholder"""
asyncio.create_task(send_telegram_request_async(method, params))
return {"ok": True, "result": []} # Возвращаем заглушку вместо None
# Оптимизированное форматирование для частых случаев
def format_top_apy_data(data, position):
"""Format APY data for display with ranking"""
if not data:
return "No data available"
# КЛЮЧЕВОЕ ИЗМЕНЕНИЕ: добавляем APY в ключ кэша, чтобы учитывать изменения в значениях
cache_key = f"{data.get('pool_id', '')}_{data.get('apy')}_{position}"
if cache_key in _formatted_data_cache:
return _formatted_data_cache[cache_key]
# Extract protocol name from pool_id
pool_id = data.get('pool_id', '')
protocol_parts = pool_id.split('_')
# Проверяем, есть ли как минимум 3 части в pool_id
if len(protocol_parts) >= 3:
# Берем третий элемент и все последующие, объединяя их снова с '_'
protocol = '_'.join(protocol_parts[2:])
else:
# Если частей меньше 3, используем первую часть или пустую строку
protocol = protocol_parts[0] if protocol_parts else ''
# Format TVL
tvl = data.get('tvl', 0)
if tvl >= 1000000:
tvl_formatted = f"${tvl / 1000000:.1f}M"
elif tvl >= 1000:
tvl_formatted = f"${tvl / 1000:.1f}K"
else:
tvl_formatted = f"${tvl:.0f}"
# Select emoji for position
position_emoji = '🥇' if position == 1 else '🥈' if position == 2 else '🥉' if position == 3 else '🏅'
# Безопасное форматирование APY данных с проверкой на None и нулевые значения
apy = data.get('apy')
apy_base = data.get('apy_base')
apy_reward = data.get('apy_reward')
apy_mean_30d = data.get('apy_mean_30d')
# Проверка и форматирование для всех значений APY
apy_total = f"{apy:.2f}%" if apy is not None else 'N/A'
apy_base_fmt = f"{apy_base:.2f}%" if apy_base is not None else 'N/A'
apy_reward_fmt = f"{apy_reward:.2f}%" if apy_reward is not None else 'N/A'
apy_mean_30d_fmt = f"{apy_mean_30d:.2f}%" if apy_mean_30d is not None else 'N/A'
# Экранируем спецсимволы в названии протокола
protocol_safe = protocol.replace('_', '\\_').replace('*', '\\*').replace('[', '\\[').replace(']', '\\]')
# Add link to pool site, if available
site_link = f" ├ [Pool Site]({data.get('site_url')})\n" if data.get('site_url') else ''
try:
result = (
f"{position_emoji} *{data.get('asset')}* on *{data.get('chain')}*\n"
f" ┌ Protocol: *{protocol_safe}*\n"
f"{site_link}"
f" ├ APY Total: *{apy_total}*\n"
f" ├ APY Base: {apy_base_fmt}\n"
f" ├ APY Reward: {apy_reward_fmt}\n"
f" ├ Avg APY 30d: {apy_mean_30d_fmt}\n"
f" └ TVL: {tvl_formatted}"
)
except Exception as e:
print(f"[FORMAT ERROR] Error formatting pool data: {e}")
# Упрощённое форматирование в случае ошибки
result = (
f"{position_emoji} Asset: {data.get('asset')} on {data.get('chain')}\n"
f"Protocol: {protocol}\n"
f"APY: {apy_total}\n"
f"TVL: {tvl_formatted}"
)
# Сохраняем и возвращаем результат
_formatted_data_cache[cache_key] = result
return result
# Function to create paginated assets keyboard
async def create_paginated_assets_keyboard(page=0, items_per_page=12):
"""Create a keyboard with pagination for assets"""
assets = await database.get_all_assets()
# Calculate assets for the current page
start_index = page * items_per_page
end_index = min(start_index + items_per_page, len(assets))
page_assets = assets[start_index:end_index]
# Format keyboard: 3 assets per row
keyboard = []
row = []
for i, asset in enumerate(page_assets):
row.append({"text": asset, "callback_data": f"asset_{asset}"})
# Add 3 buttons per row
if len(row) == 3 or i == len(page_assets) - 1:
keyboard.append(row.copy())
row = []
# Add navigation buttons
nav_row = []
if page > 0:
nav_row.append({"text": "⬅️ Previous", "callback_data": f"page_{page-1}"})
# Изменяем кнопку Back - убираем иконку, если она есть
nav_row.append({"text": "Back", "callback_data": "back_to_main"})
if end_index < len(assets):
nav_row.append({"text": "Next ➡️", "callback_data": f"page_{page+1}"})
keyboard.append(nav_row)
return {"inline_keyboard": keyboard}
# Function to create paginated chains keyboard
async def create_paginated_chains_keyboard(page=0, items_per_page=12):
"""Create a keyboard with pagination for chains"""
chains = await database.get_all_chains()
# Calculate chains for the current page
start_index = page * items_per_page
end_index = min(start_index + items_per_page, len(chains))
page_chains = chains[start_index:end_index]
# Format keyboard: 3 chains per row
keyboard = []
row = []
for i, chain in enumerate(page_chains):
row.append({"text": chain, "callback_data": f"chain_{chain}"})
# Add 3 buttons per row
if len(row) == 3 or i == len(page_chains) - 1:
keyboard.append(row)
row = []
# Add pagination buttons if needed
has_prev = page > 0
has_next = end_index < len(chains)
pagination_row = []
if has_prev:
pagination_row.append({"text": "◀️ Prev", "callback_data": f"chains_page_{page-1}"})
pagination_row.append({"text": "Back", "callback_data": "back_to_main"})
if has_next:
pagination_row.append({"text": "Next ▶️", "callback_data": f"chains_page_{page+1}"})
if pagination_row:
keyboard.append(pagination_row)
return {"inline_keyboard": keyboard}
# Function to create main menu
async def create_main_menu(user_id):
"""Create the main menu with admin options if applicable"""
# Проверка прав администратора с использованием кэша
is_admin = False
# Проверяем кэш - если запись свежее 1 часа, используем кэшированный результат
if str(user_id) in _admin_status_cache and (time.time() - _admin_status_cache[str(user_id)]["timestamp"]) < 3600:
is_admin = _admin_status_cache[str(user_id)]["is_admin"]
else:
# Запрашиваем права из базы
is_admin = await database.is_user_admin(user_id)
# Кэшируем результат
_admin_status_cache[str(user_id)] = {"is_admin": is_admin, "timestamp": time.time()}
# Basic buttons for all users
keyboard = {
"inline_keyboard": [
[{"text": "🥇 TOP-1", "callback_data": "show_top_1"}],
[{"text": "🥉 TOP-3", "callback_data": "show_top_3"}],
[{"text": "💲 Select Assets", "callback_data": "show_assets"}],
[{"text": "🔗 Select Chains", "callback_data": "show_chains"}],
[{"text": "💻 Request Feature", "callback_data": "feedback"}]
]
}
# Add analytics button only for admins
if is_admin:
keyboard["inline_keyboard"].append([{"text": "Analytics", "callback_data": "show_analytics"}])
return keyboard
# Process incoming message
async def process_message(message):
"""Process incoming Telegram messages"""
# Extract message data
message_id = message.get('message_id')
chat_id = message.get('chat', {}).get('id')
text = message.get('text', '')
user_id = message.get('from', {}).get('id')
username = message.get('from', {}).get('username', f"user_{user_id}")
# Process commands
if text == '/start':
await handle_start_command(chat_id, user_id, username)
elif text == '/top':
await handle_top_command(chat_id)
elif text == '/assets':
await handle_assets_command(chat_id)
elif text == '/chains':
# Команда доступна всем пользователям
# Получаем информацию о задаче уведомления
notification_job = scheduler.get_job('daily_notification')
if notification_job:
next_run = notification_job.next_run_time
now = datetime.datetime.now(pytz.UTC)
time_until_next = next_run - now
# Отправляем информацию о следующем запуске
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": f"📅 Next notification scheduled for:\n{next_run.strftime('%Y-%m-%d %H:%M:%S')} UTC\n\n⏱️ Time remaining: {time_until_next}"
})
else:
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": "❌ Notification task not found!"
})
elif text == '/refresh' and await database.is_user_admin(user_id):
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": "🔄 Forcing refresh of all caches..."
})
success = await database.force_refresh_all_caches()
if success:
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": "✅ All caches successfully updated with current data"
})
else:
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": "❌ Error updating caches"
})
# Handle /start command
async def handle_start_command(chat_id, user_id, username):
"""Handle the /start command"""
await log_bot_action("start command", user_id, username)
# Register user
await database.get_or_create_user(user_id, username)
keyboard = await create_main_menu(user_id)
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": (
"Welcome to the Stablecoin Yield Bot by Yieldex!\n"
"This bot tracks the most profitable stablecoin pools in the DeFi market sorted by total APY (native+reward) and shares updates daily.\n\n"
"What would you like to do next?\n\n"
"_(The data about the best pool is sent at 12:00 UTC daily)_"
),
"reply_markup": keyboard,
"parse_mode": "Markdown"
})
# Handle /top command
async def handle_top_command(chat_id):
"""Handle the /top command"""
top_apys = await database.get_top_three_apy()
if top_apys:
message = "✨ TOP STABLE OPPORTUNITIES ✨\n\n"
for i, item in enumerate(top_apys):
message += format_top_apy_data(item, i + 1)
if i < len(top_apys) - 1:
message += "\n\n"
# Add back button
back_keyboard = {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": message,
"parse_mode": "Markdown",
"reply_markup": back_keyboard,
"disable_web_page_preview": True
})
else:
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": "Failed to retrieve data about the best APY.",
"reply_markup": {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
})
# Handle /assets command
async def handle_assets_command(chat_id):
"""Handle the /assets command to show list of assets"""
assets = await database.get_all_assets()
if not assets:
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": "Sorry, I couldn't retrieve the list of assets at the moment. Please try again later.",
"reply_markup": {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
})
return
# Show assets with pagination
keyboard = await create_paginated_assets_keyboard()
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": "Select Asset:",
"reply_markup": keyboard
})
# Handle /chains command
async def handle_chains_command(chat_id):
"""Handle the /chains command to show list of chains"""
chains = await database.get_all_chains()
if not chains:
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": "Sorry, I couldn't retrieve the list of chains at the moment. Please try again later.",
"reply_markup": {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
})
return
# Show chains with pagination
keyboard = await create_paginated_chains_keyboard()
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": "Select Chain:",
"reply_markup": keyboard
})
# Handle callback queries
async def handle_callback_query(callback_query):
"""Handle callback queries from inline buttons"""
# Объявление global в начале функции
global _formatted_data_cache
# Сначала отвечаем на callback, чтобы убрать индикатор загрузки сразу
query_id = callback_query.get("id")
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
# Затем извлекаем другие данные
message_id = callback_query.get("message", {}).get("message_id")
chat_id = callback_query.get("message", {}).get("chat", {}).get("id")
data = callback_query.get("data")
user_id = callback_query.get("from", {}).get("id")
username = callback_query.get("from", {}).get("username", f"user_{user_id}")
# Log action in background, не ждем результат
asyncio.create_task(log_bot_action(data, user_id, username))
# Обработка callback...
# Handle 'show_top_3' button
if data == "show_top_3":
# Очистка кэша перед запросом свежих данных
_formatted_data_cache.clear()
top_apys = await database.get_top_three_apy()
if top_apys:
# Format date as DD/MM/YY
today = datetime.datetime.now()
formatted_date = today.strftime("%d/%m/%y")
message = f"💰TOP STABLECOIN POOLS {formatted_date}\n\n"
for i, item in enumerate(top_apys):
message += format_top_apy_data(item, i + 1)
if i < len(top_apys) - 1:
message += "\n\n"
message += "\n\n_Only the pools with more than $1M TVL are shown_"
# Add back button
back_keyboard = {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": message,
"reply_markup": back_keyboard,
"parse_mode": "Markdown",
"disable_web_page_preview": True
})
else:
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": "Failed to retrieve data about the top APY opportunities.",
"reply_markup": {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle 'show_assets' button
if data == "show_assets":
keyboard = await create_paginated_assets_keyboard()
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": "Select Asset:",
"reply_markup": keyboard
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle 'show_top_1' button
if data == "show_top_1":
await handle_show_top_1(chat_id, message_id, query_id)
return
# Handle 'feedback' button
if data == "feedback":
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": "To request a feature or leave feedback, feel free to send a DM to @konstantin_hardcore",
"reply_markup": {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle 'back_to_main' button
if data == "back_to_main":
keyboard = await create_main_menu(user_id)
# Проверяем, откуда пришел запрос (из текста сообщения)
message_text = callback_query.get("message", {}).get("text", "")
# Если запрос пришел из списка активов или цепей, редактируем сообщение
if message_text in ["Select Asset:", "Select Chain:"]:
# Редактируем текущее сообщение
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": (
"Welcome to the Stablecoin Yield Bot by Yieldex!\n"
"This bot tracks the most profitable stablecoin pools in the DeFi market sorted by total APY (native+reward) and shares updates daily.\n\n"
"What would you like to do next?\n\n"
"_(The data about the best pool is sent at 12:00 UTC daily)_"
),
"reply_markup": keyboard,
"parse_mode": "Markdown"
})
else:
# Для остальных случаев создаем новое сообщение (как раньше)
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": (
"Welcome to the Stablecoin Yield Bot by Yieldex!\n"
"This bot tracks the most profitable stablecoin pools in the DeFi market sorted by total APY (native+reward) and shares updates daily.\n\n"
"What would you like to do next?\n\n"
"_(The data about the best pool is sent at 12:00 UTC daily)_"
),
"reply_markup": keyboard,
"parse_mode": "Markdown"
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle asset selection
if data.startswith("asset_"):
asset = data[6:] # Extract asset name
top_asset_apy = await database.get_top_apy_for_asset(asset)
if top_asset_apy:
message = f"*Top APY for {asset}*\n\n"
for i, item in enumerate(top_asset_apy):
message += format_top_apy_data(item, i + 1)
if i < len(top_asset_apy) - 1:
message += "\n\n"
# Add back buttons
back_keyboard = {
"inline_keyboard": [
[{"text": "Back to Assets", "callback_data": "show_assets"}],
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": message,
"reply_markup": back_keyboard,
"parse_mode": "Markdown",
"disable_web_page_preview": True
})
else:
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": f"Failed to retrieve APY data for asset {asset}.",
"reply_markup": {
"inline_keyboard": [
[{"text": "Back to Assets", "callback_data": "show_assets"}],
[{"text": "Back to Main Menu", "callback_data": "back_to_main"}]
]
}
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle pagination
if data.startswith("page_"):
page = int(data.split("_")[1])
keyboard = await create_paginated_assets_keyboard(page)
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": "Select Asset:",
"reply_markup": keyboard
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle 'show_menu' button
if data == "show_menu":
keyboard = await create_main_menu(user_id)
await send_telegram_request_async("sendMessage", {
"chat_id": chat_id,
"text": (
"Welcome to the Stablecoin Yield Bot by Yieldex!\n"
"This bot tracks the most profitable stablecoin pools in the DeFi market sorted by total APY (native+reward) and shares updates daily.\n\n"
"What would you like to do next?\n\n"
"_(The data about the best pool is sent at 12:00 UTC daily)_"
),
"reply_markup": keyboard,
"parse_mode": "Markdown"
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle 'show_analytics' button
if data == "show_analytics":
is_admin = await database.is_user_admin(user_id)
if not is_admin:
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": "Access denied. This feature is available only for administrators.",
"reply_markup": {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
analytics = await database.get_analytics()
if not analytics:
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": "Failed to generate analytics report.",
"reply_markup": {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Function to safely escape markdown characters
def escape_markdown(text):
if not isinstance(text, str):
text = str(text)
return text.replace('_', '\\_').replace('*', '\\*').replace('[', '\\[').replace(']', '\\]').replace('(', '\\(').replace(')', '\\)').replace('~', '\\~').replace('`', '\\`').replace('>', '\\>').replace('#', '\\#').replace('+', '\\+').replace('-', '\\-').replace('=', '\\=').replace('|', '\\|').replace('{', '\\{').replace('}', '\\}').replace('.', '\\.').replace('!', '\\!')
# Format the report
message = "📊 *Bot Analytics Report*\n\n"
# User information
message += "👥 *Users*\n"
message += f"• Total: {analytics['new_users']['total']}\n"
message += f"• New today: {analytics['new_users']['today']}\n"
message += f"• New this week: {analytics['new_users']['week']}\n"
message += f"• New this month: {analytics['new_users']['month']}\n\n"
# All-time actions
message += "🔄 *All Time Actions*\n"
if analytics['actions']:
for item in analytics['actions']:
message += f"• {escape_markdown(item['action'])}: {item['count']}\n"
else:
message += "No actions recorded.\n"
# Today's actions
message += "\n📈 *Today's Actions*\n"
if analytics['today_actions']:
for item in analytics['today_actions']:
message += f"• {escape_markdown(item['action'])}: {item['count']}\n"
else:
message += "No actions recorded today.\n"
# Add back button
back_keyboard = {
"inline_keyboard": [
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": message,
"reply_markup": back_keyboard,
"parse_mode": "Markdown",
"disable_web_page_preview": True
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle 'show_chains' button
if data == "show_chains":
keyboard = await create_paginated_chains_keyboard()
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": "Select Chain:",
"reply_markup": keyboard
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle chain pagination
if data.startswith("chains_page_"):
page = int(data.split("_")[2])
keyboard = await create_paginated_chains_keyboard(page)
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": "Select Chain:",
"reply_markup": keyboard
})
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Handle chain selection
if data.startswith("chain_"):
chain = data[6:] # Extract chain name
await log_bot_action(f"chain_{chain}", user_id, username)
try:
top_apys = await database.get_top_apy_for_chain(chain)
if top_apys:
message = f"✨ TOP OPPORTUNITIES ON {chain.upper()} ✨\n\n"
for i, item in enumerate(top_apys):
try:
pool_text = format_top_apy_data(item, i + 1)
message += pool_text
if i < len(top_apys) - 1:
message += "\n\n"
except Exception as e:
print(f"Error formatting pool {i+1}: {e}")
message += f"Pool {i+1}: {item.get('asset')} with APY {item.get('apy', 'N/A')}%\n"
# Add back button
back_keyboard = {
"inline_keyboard": [
[{"text": "Back to Chains", "callback_data": "show_chains"}],
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
try:
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": message,
"parse_mode": "Markdown",
"reply_markup": back_keyboard,
"disable_web_page_preview": True
})
except Exception as e:
print(f"Error sending message: {e}")
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": f"Error formatting message for {chain}. Technical details: {str(e)}",
"reply_markup": back_keyboard
})
else:
await send_telegram_request_async("editMessageText", {
"chat_id": chat_id,
"message_id": message_id,
"text": f"No data available for chain '{chain}'. This chain may not have any pools or may be listed under a different name.",
"reply_markup": {
"inline_keyboard": [
[{"text": "Back to Chains", "callback_data": "show_chains"}],
[{"text": "Back to Menu", "callback_data": "back_to_main"}]
]
}
})
except Exception as e:
print(f"Error processing chain '{chain}': {e}")
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
return
# Answer callback query to remove loading state
await send_telegram_request_async("answerCallbackQuery", {
"callback_query_id": query_id
})
async def send_daily_notification():
"""Send daily notification to all subscribed users"""
global _formatted_data_cache
timestamp = datetime.datetime.now().isoformat()
print(f"[{timestamp}] ⏰ Executing scheduled daily notification")
try:
print(f"[{timestamp}] 🔄 Forcing full cache refresh")
success = await database.force_refresh_all_caches()
if success:
print(f"[{timestamp}] ✅ Cache forcefully refreshed with new data")
else:
print(f"[{timestamp}] ⚠️ Cache refresh failed, using existing data")
_formatted_data_cache.clear()
top_apy = await database.get_top_apy()
if not top_apy:
print(f"[{timestamp}] ❌ Failed to retrieve top APY data, aborting notification")
return
subscribers = await database.get_subscribed_users()
print(f"[{timestamp}] 📋 Found {len(subscribers)} subscribers")
menu_keyboard = {
"inline_keyboard": [
[{"text": "Open Main Menu", "callback_data": "show_menu"}]
]
}
today = datetime.datetime.now()
formatted_date = today.strftime("%d/%m/%y")
message = f"💰TOP STABLECOIN POOL {formatted_date}\n\n"
message += format_top_apy_data(top_apy, 1)
message += "\n\n_Only the pools with more than $1M TVL are shown_"
sent_count = 0
error_count = 0
for user in subscribers:
user_id = user.get('telegram_id')
print(f"[{timestamp}] 📤 Sending notification to user {user_id}")
try:
result = await send_telegram_request_async("sendMessage", {
"chat_id": user_id,
"text": message,
"parse_mode": "Markdown",
"reply_markup": menu_keyboard,
"disable_web_page_preview": True
})
if result and result.get("ok"):
print(f"[{timestamp}] ✅ Notification sent to user {user_id}")
sent_count += 1
await log_bot_action("notification_sent", user_id, user.get("username"))
else:
print(f"[{timestamp}] ⚠️ Failed to send notification to user {user_id}: {result}")
try:
numeric_id = int(user_id)
result = await send_telegram_request_async("sendMessage", {
"chat_id": numeric_id,
"text": message,
"parse_mode": "Markdown",
"reply_markup": menu_keyboard,
"disable_web_page_preview": True
})
if result and result.get("ok"):
print(f"[{timestamp}] ✅ Notification sent to user {numeric_id} (numeric)")
sent_count += 1
await log_bot_action("notification_sent", user_id, user.get("username"))
else:
print(f"[{timestamp}] ❌ Failed with numeric ID for user {user_id}: {result}")
error_count += 1
except Exception as e:
print(f"[{timestamp}] ❌ Error sending with numeric ID to {user_id}: {e}")
error_count += 1
except Exception as e:
print(f"[{timestamp}] ❌ Error sending notification to {user_id}: {e}")
error_count += 1
print(f"[{timestamp}] 📊 Notification summary: {sent_count} sent, {error_count} errors")
except Exception as e:
print(f"[{timestamp}] 🚨 CRITICAL ERROR in send_daily_notification: {e}")
# Очищаем форматированный кэш
_formatted_data_cache = {}
# Setup polling
async def poll_updates():
"""Poll for updates from Telegram"""
offset = None
while True:
try:
params = {"timeout": 30}
if offset:
params["offset"] = offset
# Используем асинхронную версию вместо синхронной
response = await send_telegram_request_async("getUpdates", params)
if response and response.get("ok") and response.get("result"):
updates = response["result"]
for update in updates:
offset = update["update_id"] + 1
if "message" in update and "text" in update["message"]:
await process_message(update["message"])
if "callback_query" in update:
await handle_callback_query(update["callback_query"])
# If no updates, wait a bit to avoid hammering the API
if not response or not response.get("result"):
await asyncio.sleep(1)
except Exception as e:
print(f"Error in polling: {e}")
await asyncio.sleep(5) # Wait a bit longer if there's an error
# Установка задачи на обновление кэша каждые 15 минут
async def setup_cache_updater():
"""Настройка планировщика обновления кэша с обработкой ошибок"""
print("[SETUP] Setting up cache updater job...")
scheduler.add_job(
database.update_all_caches,
'interval',
minutes=15,
id='cache_updater',
replace_existing=True
)
print("[SETUP] Cache updater job scheduled to run every 15 minutes")