AdityaAdaki commited on
Commit
4a38b92
Β·
1 Parent(s): fcf08b5
Files changed (1) hide show
  1. app.py +43 -3
app.py CHANGED
@@ -7,6 +7,7 @@ from werkzeug.utils import secure_filename
7
  import re
8
  import asyncio
9
  from telegram import Bot
 
10
 
11
  # Load environment variables
12
  load_dotenv()
@@ -25,6 +26,30 @@ TELEGRAM_BOT_TOKEN = "7868898974:AAH2p7sBkj0TH4sD__giOmUt-2ZXn7sg0_c"
25
  TELEGRAM_CHAT_ID = "5397241102" # Hardcoded chat ID
26
  bot = Bot(token=TELEGRAM_BOT_TOKEN)
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  async def send_invalid_certificate_notification(cert_name, details):
29
  """Send Telegram notification for invalid certificate."""
30
  try:
@@ -36,12 +61,27 @@ Details: {details}
36
 
37
  Please review this certificate as soon as possible.
38
  """
39
- await bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=message, parse_mode='HTML')
 
 
 
 
 
40
  return True
41
- except Exception as e:
42
- print(f"Failed to send Telegram notification: {str(e)}")
 
43
  return False
44
 
 
 
 
 
 
 
 
 
 
45
  def check_validity_from_response(response_text):
46
  """Extract validity status from Gemini's response."""
47
  # Look for validity indicators in the response
 
7
  import re
8
  import asyncio
9
  from telegram import Bot
10
+ from telegram.error import TelegramError
11
 
12
  # Load environment variables
13
  load_dotenv()
 
26
  TELEGRAM_CHAT_ID = "5397241102" # Hardcoded chat ID
27
  bot = Bot(token=TELEGRAM_BOT_TOKEN)
28
 
29
+ # Add this function to verify bot connection
30
+ async def verify_bot_connection():
31
+ try:
32
+ bot_info = await bot.get_me()
33
+ print(f"βœ… Successfully connected to Telegram bot: @{bot_info.username}")
34
+
35
+ # Verify chat ID
36
+ try:
37
+ await bot.send_message(
38
+ chat_id=TELEGRAM_CHAT_ID,
39
+ text="πŸ”„ Certificate validation system is online and connected!"
40
+ )
41
+ print(f"βœ… Successfully sent test message to chat ID: {TELEGRAM_CHAT_ID}")
42
+ return True
43
+ except TelegramError as chat_error:
44
+ print(f"❌ Failed to send message to chat ID {TELEGRAM_CHAT_ID}")
45
+ print(f"Error: {chat_error}")
46
+ return False
47
+
48
+ except TelegramError as e:
49
+ print(f"❌ Failed to connect to Telegram bot")
50
+ print(f"Error: {e}")
51
+ return False
52
+
53
  async def send_invalid_certificate_notification(cert_name, details):
54
  """Send Telegram notification for invalid certificate."""
55
  try:
 
61
 
62
  Please review this certificate as soon as possible.
63
  """
64
+ result = await bot.send_message(
65
+ chat_id=TELEGRAM_CHAT_ID,
66
+ text=message,
67
+ parse_mode='HTML'
68
+ )
69
+ print(f"βœ… Successfully sent notification to Telegram")
70
  return True
71
+ except TelegramError as e:
72
+ print(f"❌ Failed to send Telegram notification: {str(e)}")
73
+ print(f"Chat ID used: {TELEGRAM_CHAT_ID}")
74
  return False
75
 
76
+ # Modify the app initialization to verify bot connection
77
+ @app.before_first_request
78
+ def initialize():
79
+ print("πŸ”„ Initializing Telegram bot connection...")
80
+ loop = asyncio.new_event_loop()
81
+ asyncio.set_event_loop(loop)
82
+ loop.run_until_complete(verify_bot_connection())
83
+ loop.close()
84
+
85
  def check_validity_from_response(response_text):
86
  """Extract validity status from Gemini's response."""
87
  # Look for validity indicators in the response