Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import logging
|
|
| 5 |
import httpx
|
| 6 |
from flask import Flask, request, jsonify
|
| 7 |
import time
|
| 8 |
-
import asyncio
|
| 9 |
|
| 10 |
# Suppress experimental warnings
|
| 11 |
os.environ['HF_HUB_DISABLE_EXPERIMENTAL_WARNING'] = '1'
|
|
@@ -15,6 +14,9 @@ GOOGLE_API_KEY = 'AIzaSyAYXUMnwmR4nUGDCs97FJJsafcQAPAAuzE'
|
|
| 15 |
BOT_TOKEN = '7484321656:AAExhpS7sOGMu2BCuPQrDjuXpY3sEQmBgfY'
|
| 16 |
WEBHOOK_SECRET = 'A3%26c8%21jP%23xZ1v*Qw5kL%5E0tR%40u9%25yS6' # URL-encoded secret
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
# Initialize the Telegram bot
|
| 19 |
bot = telebot.TeleBot(BOT_TOKEN)
|
| 20 |
|
|
@@ -94,14 +96,14 @@ def handle_update():
|
|
| 94 |
return jsonify({'status': 'ok'}), 200
|
| 95 |
|
| 96 |
async def set_telegram_webhook():
|
| 97 |
-
"""Sets the webhook for the Telegram bot
|
| 98 |
webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
|
| 99 |
retry_attempts = 5
|
| 100 |
retry_delay = 1 # seconds
|
| 101 |
|
| 102 |
for attempt in range(retry_attempts):
|
| 103 |
try:
|
| 104 |
-
async with httpx.AsyncClient() as client:
|
| 105 |
response = await client.post(
|
| 106 |
f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
|
| 107 |
data={"url": webhook_url},
|
|
@@ -113,7 +115,7 @@ async def set_telegram_webhook():
|
|
| 113 |
elif result.get('error_code') == 429:
|
| 114 |
retry_after = result['parameters'].get('retry_after', retry_delay)
|
| 115 |
logger.warning(f"Rate limit exceeded. Retrying after {retry_after} seconds.")
|
| 116 |
-
|
| 117 |
else:
|
| 118 |
logger.error(f"Failed to set webhook: {result}")
|
| 119 |
return
|
|
@@ -123,6 +125,7 @@ async def set_telegram_webhook():
|
|
| 123 |
|
| 124 |
def run_flask_app():
|
| 125 |
"""Launches the Flask app and sets the Telegram webhook."""
|
|
|
|
| 126 |
asyncio.run(set_telegram_webhook())
|
| 127 |
app.run(host="0.0.0.0", port=5000, debug=True)
|
| 128 |
|
|
|
|
| 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'
|
|
|
|
| 14 |
BOT_TOKEN = '7484321656:AAExhpS7sOGMu2BCuPQrDjuXpY3sEQmBgfY'
|
| 15 |
WEBHOOK_SECRET = 'A3%26c8%21jP%23xZ1v*Qw5kL%5E0tR%40u9%25yS6' # URL-encoded secret
|
| 16 |
|
| 17 |
+
# Proxy Configuration
|
| 18 |
+
PROXY_URL = 'http://eR3LhYeoZXNWhIp:clIvQ2hSkO5CtLl@107.180.131.170:58874'
|
| 19 |
+
|
| 20 |
# Initialize the Telegram bot
|
| 21 |
bot = telebot.TeleBot(BOT_TOKEN)
|
| 22 |
|
|
|
|
| 96 |
return jsonify({'status': 'ok'}), 200
|
| 97 |
|
| 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},
|
|
|
|
| 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
|
|
|
|
| 125 |
|
| 126 |
def run_flask_app():
|
| 127 |
"""Launches the Flask app and sets the Telegram webhook."""
|
| 128 |
+
import asyncio
|
| 129 |
asyncio.run(set_telegram_webhook())
|
| 130 |
app.run(host="0.0.0.0", port=5000, debug=True)
|
| 131 |
|