yangtb24 commited on
Commit
9baf5b5
·
verified ·
1 Parent(s): 6b9e821

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -5
app.py CHANGED
@@ -120,9 +120,27 @@ class EventEmitter:
120
  {"document": [content], "metadata": [{"name": title, "source": url, "html": False}]},
121
  )
122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  async def set_bot_commands(bot: Bot):
125
- try:
 
 
126
  url = f"{CF_WORKER_URL}/bot{TELEGRAM_BOT_TOKEN}/deleteMyCommands"
127
  async with aiohttp.ClientSession() as session:
128
  async with session.post(url) as response:
@@ -143,9 +161,12 @@ async def set_bot_commands(bot: Bot):
143
  logging.error(f"Telegram set commands failed: {response.status}, {error_text}")
144
  return
145
  logging.info("Telegram commands set successfully")
146
- except Exception as e:
147
- logging.error(f"Error setting Telegram commands: {e}")
148
-
 
 
 
149
 
150
  def parse_command(user_message):
151
  command = user_message.split(" ")[0]
@@ -535,10 +556,19 @@ def get_help_message():
535
 
536
  async def main():
537
  logging.basicConfig(level=logging.INFO)
 
 
 
 
 
538
  application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
539
 
540
  # Set bot commands
541
- await set_bot_commands(application.bot)
 
 
 
 
542
 
543
  # Command handlers
544
  application.add_handler(CommandHandler("start", handle_telegram_update))
 
120
  {"document": [content], "metadata": [{"name": title, "source": url, "html": False}]},
121
  )
122
 
123
+ async def check_cf_worker_health():
124
+ try:
125
+ url = f"{CF_WORKER_URL}/health"
126
+ async with aiohttp.ClientSession() as session:
127
+ async with session.get(url) as response:
128
+ if response.ok:
129
+ logging.info("Cloudflare Worker health check passed.")
130
+ return True
131
+ else:
132
+ error_text = await response.text()
133
+ logging.error(f"Cloudflare Worker health check failed: {response.status}, {error_text}")
134
+ return False
135
+ except Exception as e:
136
+ logging.error(f"Cloudflare Worker health check error: {e}")
137
+ return False
138
+
139
 
140
  async def set_bot_commands(bot: Bot):
141
+ max_retries = 3
142
+ for attempt in range(max_retries):
143
+ try:
144
  url = f"{CF_WORKER_URL}/bot{TELEGRAM_BOT_TOKEN}/deleteMyCommands"
145
  async with aiohttp.ClientSession() as session:
146
  async with session.post(url) as response:
 
161
  logging.error(f"Telegram set commands failed: {response.status}, {error_text}")
162
  return
163
  logging.info("Telegram commands set successfully")
164
+ return
165
+ except Exception as e:
166
+ logging.error(f"Error setting Telegram commands, attempt {attempt + 1}: {e}")
167
+ if attempt == max_retries - 1:
168
+ raise
169
+ await asyncio.sleep(2) # Wait for 2 second before retrying
170
 
171
  def parse_command(user_message):
172
  command = user_message.split(" ")[0]
 
556
 
557
  async def main():
558
  logging.basicConfig(level=logging.INFO)
559
+
560
+ if not await check_cf_worker_health():
561
+ logging.error("Cloudflare Worker health check failed. Exiting.")
562
+ return
563
+
564
  application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
565
 
566
  # Set bot commands
567
+ try:
568
+ await set_bot_commands(application.bot)
569
+ except Exception as e:
570
+ logging.error(f"Failed to set bot commands after multiple retries: {e}")
571
+ return
572
 
573
  # Command handlers
574
  application.add_handler(CommandHandler("start", handle_telegram_update))