Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import google.generativeai as genai
|
|
| 4 |
import logging
|
| 5 |
import httpx
|
| 6 |
from flask import Flask, request, jsonify
|
|
|
|
| 7 |
|
| 8 |
# Suppress experimental warnings
|
| 9 |
os.environ['HF_HUB_DISABLE_EXPERIMENTAL_WARNING'] = '1'
|
|
@@ -97,19 +98,30 @@ def handle_update():
|
|
| 97 |
async def set_telegram_webhook():
|
| 98 |
"""Sets the webhook for the Telegram bot using a proxy."""
|
| 99 |
webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
def run_flask_app():
|
| 115 |
"""Launches the Flask app and sets the Telegram webhook."""
|
|
|
|
| 4 |
import logging
|
| 5 |
import httpx
|
| 6 |
from flask import Flask, request, jsonify
|
| 7 |
+
import time
|
| 8 |
|
| 9 |
# Suppress experimental warnings
|
| 10 |
os.environ['HF_HUB_DISABLE_EXPERIMENTAL_WARNING'] = '1'
|
|
|
|
| 98 |
async def set_telegram_webhook():
|
| 99 |
"""Sets the webhook for the Telegram bot using a proxy."""
|
| 100 |
webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
|
| 101 |
+
retry_attempts = 5
|
| 102 |
+
retry_delay = 1 # seconds
|
| 103 |
+
|
| 104 |
+
for attempt in range(retry_attempts):
|
| 105 |
+
try:
|
| 106 |
+
async with httpx.AsyncClient(proxies=PROXY_URL) as client:
|
| 107 |
+
response = await client.post(
|
| 108 |
+
f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
|
| 109 |
+
data={"url": webhook_url},
|
| 110 |
+
)
|
| 111 |
+
result = response.json()
|
| 112 |
+
if result.get('ok'):
|
| 113 |
+
logger.info("Webhook set successfully.")
|
| 114 |
+
return
|
| 115 |
+
elif result.get('error_code') == 429:
|
| 116 |
+
retry_after = result['parameters'].get('retry_after', retry_delay)
|
| 117 |
+
logger.warning(f"Rate limit exceeded. Retrying after {retry_after} seconds.")
|
| 118 |
+
time.sleep(retry_after)
|
| 119 |
+
else:
|
| 120 |
+
logger.error(f"Failed to set webhook: {result}")
|
| 121 |
+
return
|
| 122 |
+
except httpx.RequestError as e:
|
| 123 |
+
logger.error(f"Request exception: {e}")
|
| 124 |
+
return
|
| 125 |
|
| 126 |
def run_flask_app():
|
| 127 |
"""Launches the Flask app and sets the Telegram webhook."""
|