-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
846 lines (777 loc) · 36.1 KB
/
bot.py
File metadata and controls
846 lines (777 loc) · 36.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
import asyncio
import logging
import os
import re
import requests
import telegram
from dotenv import load_dotenv
from telegram import BotCommand, InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.constants import ChatAction
from telegram.ext import (
Application,
CallbackContext, # Added
CallbackQueryHandler,
CommandHandler,
MessageHandler,
filters,
)
from telegram.request import HTTPXRequest
from core.dashboard import (
clear_user_dashboard,
get_user_dashboard,
remove_tracked_wallet,
)
# --- Research Agent Integration ---
from core.research_agent import (
research_agent_handler,
send_research_agent_miniapp_button,
)
from core.token_stats import process_token, show_top_holders
from core.token_stats import token_prompt as core_token_prompt # Rename to avoid clash
from core.wallet_tracker import (
process_wallet,
show_recent_transactions,
wallet_tracking_job, # Import job directly
)
from core.wallet_tracker import (
wallet_prompt as core_wallet_prompt, # Rename to avoid clash
)
from core.whale_alerts import ( # Import job directly
remove_whale_alert_handler, # Added
whale_alert_job,
whale_alerts_command,
)
from core.whale_alerts import (
set_threshold_prompt as core_set_threshold_prompt, # Rename to avoid clash
)
class VybeScopeBot:
def __init__(self):
# Load environment variables
load_dotenv()
self.TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
if not self.TELEGRAM_TOKEN:
raise ValueError("TELEGRAM_TOKEN not found in environment variables.")
self.VYBE_API_KEY = os.getenv("VYBE_API_KEY")
self.WHALE_ALERT_INTERVAL_SECONDS = int(
os.getenv("WHALE_ALERT_INTERVAL_SECONDS", 60)
)
self.WALLET_TRACKING_INTERVAL_SECONDS = int(
os.getenv("WALLET_TRACKING_INTERVAL_SECONDS", 60)
)
self.user_thresholds = {}
self.user_states = {}
self.logger = logging.getLogger(__name__)
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
self.application = None
async def start(self, update: Update, context: CallbackContext) -> None:
"""Sends the welcome message and main menu."""
await context.bot.send_chat_action(
chat_id=update.effective_chat.id, action=ChatAction.TYPING
)
user = update.effective_user.first_name
welcome_message = (
f"🚀 Welcome to VybeScope🔭, *{user}*! \n\n"
"Explore powerful tools to track whale alerts, analyze token statistics, "
"and monitor wallet activity using the Vybe API.\n\n"
"Select an option below to begin your journey! 👇"
)
# Main menu keyboard
keyboard = [
[InlineKeyboardButton("Dashboard 📊", callback_data="dashboard")],
[
InlineKeyboardButton("Whale Alerts 🐋", callback_data="whale_alerts"),
InlineKeyboardButton(
"Wallet Tracker 💼", callback_data="wallet_tracker"
),
],
[
InlineKeyboardButton(
"Token Statistics 📈", callback_data="token_stats"
),
],
[
InlineKeyboardButton(
"Quick Commands ⚡", callback_data="quick_commands"
),
InlineKeyboardButton(
"🆕Research Agent 🤖", callback_data="research_agent"
),
],
]
reply_markup = InlineKeyboardMarkup(keyboard)
# Clear any previous state for the user
if update.effective_user and update.effective_user.id in self.user_states:
del self.user_states[update.effective_user.id]
# Determine message object (could be from command or callback)
message = (
update.callback_query.message if update.callback_query else update.message
)
if not message: # Handle cases where message might be missing
return
# Try sending the photo first only on initial /start command
if (
update.message
and update.message.text
and update.message.text.startswith("/start")
):
try:
await message.reply_photo(photo=open("assets/vybe_banner.png", "rb"))
except FileNotFoundError:
self.logger.error(
"Error: assets/vybe_banner.png not found. Skipping photo."
)
except telegram.error.BadRequest as e:
if "not found" in str(e):
self.logger.error(
"Error: assets/vybe_banner.png not found. Skipping photo."
)
else:
self.logger.error(
f"Error sending start photo (BadRequest): {e}. Skipping photo."
)
except Exception as e:
self.logger.error(f"Error sending start photo: {e}. Skipping photo.")
# Send or edit the welcome message
try:
if update.callback_query:
if getattr(message, "text", None):
await message.edit_text(
text=welcome_message,
reply_markup=reply_markup,
parse_mode="Markdown",
)
else:
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=welcome_message,
reply_markup=reply_markup,
parse_mode="Markdown",
)
else:
await message.reply_text(
text=welcome_message,
reply_markup=reply_markup,
parse_mode="Markdown",
)
except telegram.error.BadRequest as e:
if "message is not modified" in str(e):
self.logger.info("Welcome message already shown.")
else:
self.logger.error(f"Error sending/editing welcome message: {e}")
except Exception as e:
self.logger.error(f"Unexpected error in start handler: {e}")
async def threshold_command(self, update: Update, context: CallbackContext) -> None:
"""Handles the /threshold command, triggers the prompt."""
await context.bot.send_chat_action(
chat_id=update.effective_chat.id, action=ChatAction.TYPING
)
await core_set_threshold_prompt(update, context, self.user_states)
async def token_command(self, update: Update, context: CallbackContext) -> None:
"""Handles the /token command, triggers the prompt."""
await context.bot.send_chat_action(
chat_id=update.effective_chat.id, action=ChatAction.TYPING
)
await core_token_prompt(update, context, self.user_states)
async def wallet_command(self, update: Update, context: CallbackContext) -> None:
"""Handles the /wallet command, triggers the prompt."""
await context.bot.send_chat_action(
chat_id=update.effective_chat.id, action=ChatAction.TYPING
)
await core_wallet_prompt(update, context, self.user_states)
async def check_command(self, update: Update, context: CallbackContext) -> None:
"""Handles the /check command, directly checks highest tx."""
await context.bot.send_chat_action(
chat_id=update.effective_chat.id, action=ChatAction.TYPING
)
await whale_alerts_command(update, context)
async def agent_command(self, update: Update, context: CallbackContext) -> None:
"""Handles the /agent command, opens the Research Agent mini app."""
await send_research_agent_miniapp_button(update, context)
async def dashboard_command(self, update: Update, context: CallbackContext) -> None:
"""Shows the user's dashboard: tracked wallets and whale alert settings."""
await context.bot.send_chat_action(
chat_id=update.effective_chat.id, action=ChatAction.TYPING
)
user_id = update.effective_user.id
dashboard = get_user_dashboard(user_id)
wallets = dashboard.get("wallets", [])
# Get tracked tokens and their settings
from core.dashboard import (
get_token_alert_settings,
get_tracked_whale_alert_tokens,
)
tracked_tokens = get_tracked_whale_alert_tokens(user_id)
token_settings = {
token: get_token_alert_settings(user_id, token) for token in tracked_tokens
}
is_empty = not wallets and not tracked_tokens
if is_empty:
msg = "📊 *Your Dashboard is Empty!*\n\n"
msg += "Add a wallet or set up whale alerts for tokens to get started."
keyboard = [
[
InlineKeyboardButton(
"Add Wallet ➕", callback_data="dashboard_add_wallet"
),
InlineKeyboardButton(
"Remove Wallet ➖", callback_data="dashboard_remove_wallet"
),
],
[
InlineKeyboardButton(
"Add/Remove Whale Alerts ⚙", callback_data="whale_alerts"
),
InlineKeyboardButton(
"🗑️ Clear Dashboard", callback_data="dashboard_clear"
),
],
[
InlineKeyboardButton("Back to Main Menu 🔙", callback_data="start"),
],
]
else:
msg = "📊 *Your Dashboard*\n\n"
# Display wallets section
msg += f"💼 *Tracked Wallets ({len(wallets)}):*\n"
if wallets:
for w in wallets:
msg += f"`{w}`\n"
else:
msg += "_None yet. Add one Immediately!_\n"
# Display whale alert section with total count
msg += (
f"\n🐋 *Whale Alert Settings ({len(tracked_tokens)} tokens tracked):*\n"
)
# Display tracked tokens section if any exist
if tracked_tokens:
msg += "*Tracked Tokens:*\n"
for token in tracked_tokens:
settings = token_settings[token]
status = (
"🟢 Enabled"
if settings.get("enabled", False)
else "🔴 Disabled"
)
token_threshold = settings.get("threshold", 50000)
msg += f"• `{token}`\n"
msg += f" Status: {status}\n"
msg += f" Threshold: ${token_threshold:,.2f}\n"
msg += "\n\nUse the buttons below to manage your dashboard."
keyboard = [
[
InlineKeyboardButton(
"Add Wallet ➕", callback_data="dashboard_add_wallet"
),
InlineKeyboardButton(
"Remove Wallet ➖", callback_data="dashboard_remove_wallet"
),
],
[
InlineKeyboardButton(
"Add/Remove Whale Alerts⚙", callback_data="whale_alerts"
),
InlineKeyboardButton(
"🗑️ Clear Dashboard", callback_data="dashboard_clear"
),
],
[
InlineKeyboardButton("Back to Main Menu 🔙", callback_data="start"),
],
]
reply_markup = InlineKeyboardMarkup(keyboard)
try:
if update.callback_query:
await update.callback_query.message.edit_text(
text=msg, reply_markup=reply_markup, parse_mode="Markdown"
)
else:
await update.message.reply_text(
text=msg, reply_markup=reply_markup, parse_mode="Markdown"
)
except Exception as e:
self.logger.error(f"Error in dashboard_command: {e}")
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=msg,
reply_markup=reply_markup,
parse_mode="Markdown",
)
async def handle_text(self, update: Update, context: CallbackContext) -> None:
"""Handles text inputs based on the current user state."""
await context.bot.send_chat_action(
chat_id=update.effective_user.id, action=ChatAction.TYPING
)
if not update.message or not update.message.text:
return
user_id = update.effective_user.id
text = update.message.text.strip()
if text.startswith("/agent"):
await send_research_agent_miniapp_button(update, context)
return
if user_id not in self.user_states:
await update.message.reply_text(
"🤔 Not sure what you mean. Use /start to see the main menu."
)
return
state = self.user_states.pop(user_id)
if state == "awaiting_threshold":
if text.lower() == "skip":
await whale_alerts_command(update, context)
return
try:
threshold_value = float(text)
if threshold_value <= 0:
keyboard = [
[
InlineKeyboardButton(
"Set Threshold Again 💰", callback_data="set_threshold"
)
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text(
"❌ Threshold must be a positive number!",
reply_markup=reply_markup,
)
self.user_states[user_id] = "awaiting_threshold"
return
self.user_thresholds[user_id] = threshold_value
await update.message.reply_text(
f"✅ Threshold set to ${threshold_value:,.2f}! Future alert feature is pending.",
)
await whale_alerts_command(update, context)
except ValueError:
keyboard = [
[
InlineKeyboardButton(
"Set Threshold Again 💰", callback_data="set_threshold"
)
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text(
"❌ Invalid amount! Please enter a number (e.g., 10000).",
reply_markup=reply_markup,
)
self.user_states[user_id] = "awaiting_threshold"
elif state == "awaiting_token":
# Process token input and handle unknown symbol by re-triggering prompt
status = await process_token(user_id, text, context)
if status == "unknown_symbol":
# Trigger the token prompt again (same as /token or token_stats callback)
await core_token_prompt(update, context, self.user_states)
return
elif state == "awaiting_wallet":
status = await process_wallet(user_id, text, context)
if status == "validation_error":
await update.message.reply_text(
"❌ Invalid Solana wallet address format. Please ensure it is a valid Solana address (e.g., 3qArN...)."
)
await update.message.reply_text(
"🔍 Enter a Solana wallet address to track its activity (e.g., 3qArN...):"
)
self.user_states[user_id] = "awaiting_wallet"
elif status == "empty_input":
await update.message.reply_text(
"❌ Wallet address cannot be empty! Please enter a valid Solana address."
)
await update.message.reply_text(
"🔍 Enter a Solana wallet address to track its activity (e.g., 3qArN...):"
)
self.user_states[user_id] = "awaiting_wallet"
# For other statuses like "processed_successfully", "processed_successfully_no_tokens",
# "processing_failed_api", "processing_failed_unexpected",
# messages are sent by process_wallet, and the state is considered handled.
elif state == "dashboard_awaiting_add_wallet":
status = await process_wallet(
user_id, text, context
) # process_wallet calls add_tracked_wallet
if status == "validation_error":
await update.message.reply_text(
"❌ Invalid Solana wallet address format. Please ensure it is a valid Solana address (e.g., 3qArN...)."
)
await update.message.reply_text(
"💼 Please enter a valid wallet address to add:"
)
self.user_states[user_id] = "dashboard_awaiting_add_wallet"
elif status == "empty_input":
await update.message.reply_text(
"❌ Wallet address cannot be empty! Please enter a valid Solana address."
)
await update.message.reply_text(
"💼 Please enter a valid wallet address to add:"
)
self.user_states[user_id] = "dashboard_awaiting_add_wallet"
elif status in [
"processed_successfully",
"processed_successfully_no_tokens",
"processing_failed_api",
"processing_failed_unexpected",
]:
# process_wallet sent its message (success or API error).
# Now, show the dashboard.
await self.dashboard_command(update, context)
# If state is not re-assigned (e.g. for successful processing), it's cleared by pop.
elif state == "dashboard_awaiting_remove_wallet":
removed = remove_tracked_wallet(user_id, text)
if removed:
await update.message.reply_text(
f"✅ Wallet `{text}` removed from wallet tracking!"
)
else:
await update.message.reply_text(
f"❌ Wallet `{text}` is not in your dashboard, so it cannot be removed."
)
await self.dashboard_command(update, context)
elif state == "dashboard_awaiting_add_whale_alert":
from core.dashboard import add_tracked_whale_alert_token
# Validate token address format before processing
if not re.match(r"^[1-9A-HJ-NP-Za-km-z]{32,44}$", text):
await update.message.reply_text(
"❌ Invalid Solana token address format. Please ensure it is a valid Solana address (e.g., So1111... or similar)."
)
self.user_states[user_id] = (
"dashboard_awaiting_add_whale_alert" # Keep user in the same state
)
return
added = add_tracked_whale_alert_token(user_id, text)
if added:
await update.message.reply_text(
f"✅ Token `{text}` added to your whale alerts!"
)
else:
await update.message.reply_text(
f"❌ Token `{text}` is already in your whale alerts."
)
await self.dashboard_command(update, context)
elif state == "dashboard_awaiting_remove_whale_alert":
from core.dashboard import remove_tracked_whale_alert_token
removed = remove_tracked_whale_alert_token(user_id, text)
if removed:
await update.message.reply_text(
f"✅ Token `{text}` removed from your whale alerts!"
)
else:
await update.message.reply_text(
f"❌ Token `{text}` is not in your whale alerts."
)
await self.dashboard_command(update, context)
elif state.startswith("awaiting_token_threshold_"):
token_address = state.replace("awaiting_token_threshold_", "")
try:
threshold_value = float(text)
if threshold_value <= 0:
await update.message.reply_text(
"❌ Threshold must be a positive number!"
)
self.user_states[user_id] = (
f"awaiting_token_threshold_{token_address}"
)
return
from core.dashboard import set_token_alert_threshold
set_token_alert_threshold(user_id, token_address, threshold_value)
await update.message.reply_text(
f"✅ Whale alert threshold for `{token_address}` set to ${threshold_value:,.2f}!"
)
await whale_alerts_command(update, context)
except ValueError:
await update.message.reply_text(
"❌ Invalid amount! Please enter a number (e.g., 5000)."
)
self.user_states[user_id] = f"awaiting_token_threshold_{token_address}"
else:
self.logger.warning(f"User {user_id} was in an unknown state: {state}")
await update.message.reply_text("Something went wrong. Please try /start.")
async def button_handler(self, update: Update, context: CallbackContext) -> None:
"""Handles inline keyboard button clicks."""
await context.bot.send_chat_action(
chat_id=update.effective_user.id, action=ChatAction.TYPING
)
query = update.callback_query
if not query or not query.data:
self.logger.warning("Button handler received invalid query object.")
return
await query.answer()
user_id = query.from_user.id
callback_data = query.data
if callback_data == "start":
await self.start(update, context)
elif callback_data == "whale_alerts":
await whale_alerts_command(update, context)
elif callback_data in ["toggle_whale_on", "toggle_whale_off"]:
# Deprecated - redirect to whale alerts page
await whale_alerts_command(update, context)
elif callback_data == "set_threshold":
await core_set_threshold_prompt(update, context, self.user_states)
elif callback_data == "token_stats":
await core_token_prompt(update, context, self.user_states)
elif callback_data == "wallet_tracker":
await core_wallet_prompt(update, context, self.user_states)
elif callback_data == "dashboard":
await self.dashboard_command(update, context)
elif callback_data == "dashboard_add_wallet":
self.user_states[user_id] = "dashboard_awaiting_add_wallet"
await query.message.reply_text("💼 Enter the wallet address to add:")
elif callback_data == "dashboard_remove_wallet":
self.user_states[user_id] = "dashboard_awaiting_remove_wallet"
await query.message.reply_text("💼 Enter the wallet address to remove:")
elif callback_data == "dashboard_set_threshold":
# Redirect to whale alerts page for token-specific settings
await whale_alerts_command(update, context)
elif callback_data == "dashboard_add_whale_alert":
self.user_states[user_id] = "dashboard_awaiting_add_whale_alert"
await query.message.reply_text(
"🐋 Enter the token address to add to whale alerts:"
)
elif callback_data == "dashboard_remove_whale_alert":
self.user_states[user_id] = "dashboard_awaiting_remove_whale_alert"
await query.message.reply_text(
"🐋 Enter the token address to remove from whale alerts:"
)
elif callback_data == "dashboard_clear":
cleared = clear_user_dashboard(user_id)
if cleared:
await query.message.reply_text(
"🗑️ Dashboard cleared!", parse_mode="Markdown"
)
else:
await query.message.reply_text(
"Dashboard was already empty.", parse_mode="Markdown"
)
await self.dashboard_command(update, context)
elif callback_data == "quick_commands":
quick_commands_msg = (
"*⚡ Quick Commands & Features*\n\n"
"Use these commands for quick access to features:\n\n"
"*/start* – Show main menu & restart the bot.\n"
"*/dashboard* – View your personal dashboard (tracked wallets & whale alert settings).\n"
"*/wallet <address>* – Add a new wallet to track or view an existing tracked wallet.\n"
"*/token <address>* – Get statistics and information for a specific Solana token.\n"
"*/whalealerts* – Manage whale alert notifications, add/remove tokens, set thresholds, and toggle alerts.\n"
"*/agent* – Open the Research Agent mini app for advanced AI analytics.\n\n"
"*💡 Other Tips:*\n"
"• Use the interactive buttons in chat for most actions.\n"
"• Directly send a wallet or token address to the bot for quick info.\n"
"• The bot guides you with prompts for most operations."
)
close_markup = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
"Close ❌", callback_data="close_quick_commands"
)
]
]
)
await query.message.reply_text(
quick_commands_msg, parse_mode="Markdown", reply_markup=close_markup
)
return
elif callback_data == "close_quick_commands":
try:
await query.message.delete()
except Exception as e:
self.logger.warning(f"Failed to delete quick commands message: {e}")
return
elif callback_data.startswith("show_top_holders_"):
token_address = callback_data.replace("show_top_holders_", "")
await show_top_holders(user_id, token_address, context)
elif callback_data.startswith("token_stats_back_"):
try:
await query.message.delete()
except Exception as e:
self.logger.warning(f"Failed to delete top holders message: {e}")
return
elif callback_data.startswith("show_recent_tx_"):
wallet_address = callback_data.replace("show_recent_tx_", "")
# Call our new function to show recent transactions
await show_recent_transactions(update, context, wallet_address)
elif callback_data.startswith("remove_wallet_"):
wallet_address = callback_data.replace("remove_wallet_", "")
removed = remove_tracked_wallet(user_id, wallet_address)
if removed:
await query.message.reply_text(
f"✅ Wallet `{wallet_address}` removed from wallet tracking!",
parse_mode="Markdown",
)
else:
await query.message.reply_text(
f"❌ Wallet `{wallet_address}` was not found in your dashboard.",
parse_mode="Markdown",
)
await self.dashboard_command(update, context) # Show updated dashboard
elif callback_data.startswith("recent_tx_back_"):
try:
await query.message.delete()
except Exception as e:
self.logger.warning(f"Failed to delete recent tx message: {e}")
return
elif callback_data.startswith("track_whale_alert_"):
# Call our new function that handles adding tokens to whale alerts
from core.whale_alerts import track_token_whale_alert
await track_token_whale_alert(update, context)
elif callback_data.startswith("add_whale_alert_token_"):
token_address = callback_data.replace("add_whale_alert_token_", "")
from core.dashboard import add_tracked_whale_alert_token
add_tracked_whale_alert_token(user_id, token_address)
await query.message.reply_text(
f"✅ Token `{token_address}` added to your whale alerts!"
)
elif callback_data.startswith("remove_whale_alert_token_"):
token_address = callback_data.replace("remove_whale_alert_token_", "")
from core.dashboard import remove_tracked_whale_alert_token
remove_tracked_whale_alert_token(user_id, token_address)
await query.message.reply_text(
f"✅ Token `{token_address}` removed from your whale alerts!"
)
elif callback_data.startswith("toggle_token_on:") or callback_data.startswith(
"toggle_token_off:"
):
from core.dashboard import set_token_alert_enabled
parts = callback_data.split(":", 1)
if len(parts) == 2:
token_address = parts[1]
enable = callback_data.startswith("toggle_token_on:")
set_token_alert_enabled(user_id, token_address, enable)
await query.message.reply_text(
f"{'✅ Enabled' if enable else '❌ Disabled'} whale alert for token `{token_address}`.",
parse_mode="Markdown",
)
await whale_alerts_command(update, context)
elif callback_data.startswith("disable_alert:"):
from core.dashboard import set_token_alert_enabled
token_address = callback_data.split(":", 1)[1]
set_token_alert_enabled(user_id, token_address, False)
await query.message.reply_text(
f"❌ Disabled whale alert for token `{token_address}`.",
parse_mode="Markdown",
)
await whale_alerts_command(update, context)
elif callback_data.startswith(
"set_token_threshold:"
) or callback_data.startswith("change_threshold:"):
await self.set_token_threshold_prompt_wrapper(
update, context
) # Modified to call wrapper
else:
self.logger.info(f"Received unhandled callback_data: {callback_data}")
async def set_token_threshold_prompt_wrapper(
self, update: Update, context: CallbackContext
) -> None:
"""Wrapper to call core_set_threshold_prompt with user_states."""
await core_set_threshold_prompt(update, context, self.user_states)
async def error_handler(self, update: object, context: CallbackContext) -> None:
"""Logs errors and sends a user-friendly message."""
self.logger.error(
msg="Exception while handling an update:", exc_info=context.error
)
chat_id = None
if isinstance(update, Update):
if update.effective_chat:
chat_id = update.effective_chat.id
elif update.effective_user:
chat_id = update.effective_user.id
error_message = (
"❌ Oops! Something went wrong on my end. Please try again later."
)
if isinstance(context.error, telegram.error.BadRequest):
if "message is not modified" in str(context.error):
self.logger.info("Attempted to edit message with identical content.")
return
elif "message to edit not found" in str(context.error):
self.logger.warning("Attempted to edit a message that was not found.")
error_message = (
"❌ Sorry, the message you interacted with might be too old."
)
elif isinstance(context.error, requests.RequestException):
self.logger.error(
f"Network error connecting to external API: {context.error}"
)
error_message = "❌ Network error: Could not connect to external services. Please try again later."
elif isinstance(context.error, telegram.error.Forbidden):
self.logger.warning(
f"Forbidden error: {context.error}. Bot might be blocked by the user {chat_id}."
)
return
elif isinstance(context.error, telegram.error.NetworkError):
self.logger.error(
f"Telegram Network error: {context.error}. Retrying or waiting might help."
)
error_message = (
"❌ Network error communicating with Telegram. Please try again."
)
if chat_id:
try:
await context.bot.send_message(chat_id=chat_id, text=error_message)
except Exception as e:
self.logger.error(
f"Failed to send error message to user {chat_id}: {e}"
)
def run(self):
self.logger.info("Initializing VybeScope Bot...")
request = HTTPXRequest(
connection_pool_size=10,
read_timeout=30.0,
connect_timeout=30.0,
)
self.application = (
Application.builder().token(self.TELEGRAM_TOKEN).request(request).build()
)
async def set_bot_commands():
commands = [
BotCommand("start", "Show main menu and restart the bot"),
BotCommand("dashboard", "View your dashboard and tracked wallets"),
BotCommand("wallet", "Track or view a wallet's activity"),
BotCommand("token", "Get stats for a Solana token"),
BotCommand("whalealerts", "Manage whale alert notifications"),
BotCommand("agent", "Open the Research Agent mini app"),
]
await self.application.bot.set_my_commands(commands)
asyncio.get_event_loop().run_until_complete(set_bot_commands())
self.application.add_handler(CommandHandler("start", self.start))
self.application.add_handler(CommandHandler("token", self.token_command))
self.application.add_handler(CommandHandler("wallet", self.wallet_command))
self.application.add_handler(
CommandHandler("whalealerts", whale_alerts_command)
)
self.application.add_handler(
CommandHandler("dashboard", self.dashboard_command)
)
self.application.add_handler(CommandHandler("agent", self.agent_command))
self.application.add_handler(
MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_text)
)
self.application.add_handler(
CallbackQueryHandler(research_agent_handler, pattern="^research_agent$")
)
# Specific patterned handlers should come before the generic button_handler
self.application.add_handler(
CallbackQueryHandler(
remove_whale_alert_handler, pattern="^delete_token_alert:"
)
)
self.application.add_handler(
CallbackQueryHandler(self.button_handler)
) # Generic handler last
self.application.add_error_handler(self.error_handler)
# Use Telegram's JobQueue to schedule whale alerts
self.application.job_queue.run_repeating(
wallet_tracking_job,
interval=self.WALLET_TRACKING_INTERVAL_SECONDS,
name="wallet_tracking_job",
data=self.application, # Pass application instance
)
self.application.job_queue.run_repeating(
whale_alert_job,
interval=self.WHALE_ALERT_INTERVAL_SECONDS,
name="whale_alert_job",
data=self.application, # Pass application instance
)
self.logger.info("Starting bot polling...")
self.application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
bot = VybeScopeBot()
bot.run()