AdityaAdaki commited on
Commit
6c7193e
Β·
1 Parent(s): 133ed51
Files changed (2) hide show
  1. app.py +22 -12
  2. requirements.txt +2 -1
app.py CHANGED
@@ -8,6 +8,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()
@@ -23,15 +24,20 @@ model = genai.GenerativeModel("gemini-1.5-flash")
23
 
24
  # Telegram Configuration
25
  TELEGRAM_BOT_TOKEN = "7868898974:AAH2p7sBkj0TH4sD__giOmUt-2ZXn7sg0_c"
26
- TELEGRAM_CHAT_ID = "5397241102" # Hardcoded chat ID
27
- bot = Bot(token=TELEGRAM_BOT_TOKEN)
 
 
 
 
 
28
 
29
  async def verify_bot_connection():
30
  try:
 
31
  bot_info = await bot.get_me()
32
  print(f"βœ… Successfully connected to Telegram bot: @{bot_info.username}")
33
 
34
- # Verify chat ID
35
  try:
36
  await bot.send_message(
37
  chat_id=TELEGRAM_CHAT_ID,
@@ -44,21 +50,15 @@ async def verify_bot_connection():
44
  print(f"Error: {chat_error}")
45
  return False
46
 
47
- except TelegramError as e:
48
  print(f"❌ Failed to connect to Telegram bot")
49
  print(f"Error: {e}")
50
  return False
51
 
52
- # Initialize bot connection when the app starts
53
- print("πŸ”„ Initializing Telegram bot connection...")
54
- loop = asyncio.new_event_loop()
55
- asyncio.set_event_loop(loop)
56
- loop.run_until_complete(verify_bot_connection())
57
- loop.close()
58
-
59
  async def send_invalid_certificate_notification(cert_name, details):
60
  """Send Telegram notification for invalid certificate."""
61
  try:
 
62
  message = f"""
63
  🚨 Invalid Certificate Detected 🚨
64
 
@@ -74,11 +74,21 @@ Please review this certificate as soon as possible.
74
  )
75
  print(f"βœ… Successfully sent notification to Telegram")
76
  return True
77
- except TelegramError as e:
78
  print(f"❌ Failed to send Telegram notification: {str(e)}")
79
  print(f"Chat ID used: {TELEGRAM_CHAT_ID}")
80
  return False
81
 
 
 
 
 
 
 
 
 
 
 
82
  def check_validity_from_response(response_text):
83
  """Extract validity status from Gemini's response."""
84
  # Look for validity indicators in the response
 
8
  import asyncio
9
  from telegram import Bot
10
  from telegram.error import TelegramError
11
+ import httpx
12
 
13
  # Load environment variables
14
  load_dotenv()
 
24
 
25
  # Telegram Configuration
26
  TELEGRAM_BOT_TOKEN = "7868898974:AAH2p7sBkj0TH4sD__giOmUt-2ZXn7sg0_c"
27
+ TELEGRAM_CHAT_ID = "5397241102"
28
+
29
+ # Initialize bot with custom client
30
+ async def init_bot():
31
+ async with httpx.AsyncClient(timeout=30.0) as client:
32
+ bot = Bot(token=TELEGRAM_BOT_TOKEN, request=client)
33
+ return bot
34
 
35
  async def verify_bot_connection():
36
  try:
37
+ bot = await init_bot()
38
  bot_info = await bot.get_me()
39
  print(f"βœ… Successfully connected to Telegram bot: @{bot_info.username}")
40
 
 
41
  try:
42
  await bot.send_message(
43
  chat_id=TELEGRAM_CHAT_ID,
 
50
  print(f"Error: {chat_error}")
51
  return False
52
 
53
+ except Exception as e:
54
  print(f"❌ Failed to connect to Telegram bot")
55
  print(f"Error: {e}")
56
  return False
57
 
 
 
 
 
 
 
 
58
  async def send_invalid_certificate_notification(cert_name, details):
59
  """Send Telegram notification for invalid certificate."""
60
  try:
61
+ bot = await init_bot()
62
  message = f"""
63
  🚨 Invalid Certificate Detected 🚨
64
 
 
74
  )
75
  print(f"βœ… Successfully sent notification to Telegram")
76
  return True
77
+ except Exception as e:
78
  print(f"❌ Failed to send Telegram notification: {str(e)}")
79
  print(f"Chat ID used: {TELEGRAM_CHAT_ID}")
80
  return False
81
 
82
+ # Initialize bot connection when the app starts
83
+ try:
84
+ print("πŸ”„ Initializing Telegram bot connection...")
85
+ loop = asyncio.new_event_loop()
86
+ asyncio.set_event_loop(loop)
87
+ loop.run_until_complete(verify_bot_connection())
88
+ loop.close()
89
+ except Exception as e:
90
+ print(f"❌ Failed to initialize Telegram bot: {str(e)}")
91
+
92
  def check_validity_from_response(response_text):
93
  """Extract validity status from Gemini's response."""
94
  # Look for validity indicators in the response
requirements.txt CHANGED
@@ -2,4 +2,5 @@ flask
2
  python-dotenv
3
  google-generativeai
4
  Werkzeug
5
- python-telegram-bot
 
 
2
  python-dotenv
3
  google-generativeai
4
  Werkzeug
5
+ python-telegram-bot
6
+ httpx>=0.24.0