Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
-
import telebot
|
| 3 |
import logging
|
| 4 |
import httpx
|
| 5 |
import asyncio
|
| 6 |
from flask import Flask, request, jsonify
|
| 7 |
|
| 8 |
# Configuration
|
| 9 |
-
GOOGLE_API_KEY = 'AIzaSyAYXUMnwmR4nUGDCs97FJJsafcQAPAAuzE'
|
| 10 |
BOT_TOKEN = '7484321656:AAExhpS7sOGMu2BCuPQrDjuXpY3sEQmBgfY'
|
| 11 |
WEBHOOK_SECRET = 'A3%26c8%21jP%23xZ1v*Qw5kL%5E0tR%40u9%25yS6' # URL-encoded secret
|
| 12 |
POWER_USER_ID = 75516649
|
|
@@ -14,10 +12,7 @@ POWER_USER_ID = 75516649
|
|
| 14 |
# Proxy Configuration
|
| 15 |
PROXY_URL = 'http://eR3LhYeoZXNWhIp:clIvQ2hSkO5CtLl@107.180.131.170:58874'
|
| 16 |
|
| 17 |
-
# Initialize
|
| 18 |
-
bot = telebot.TeleBot(BOT_TOKEN)
|
| 19 |
-
|
| 20 |
-
# Configure logging
|
| 21 |
logging.basicConfig(
|
| 22 |
level=logging.DEBUG,
|
| 23 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
@@ -51,13 +46,13 @@ def handle_update():
|
|
| 51 |
|
| 52 |
# Log and send the response
|
| 53 |
logger.debug(f"Generated response: {response_text}")
|
| 54 |
-
|
| 55 |
logger.info(f"Response sent to chat_id {chat_id}")
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
logger.error(f"Error during processing: {e}")
|
| 59 |
error_message = "Sorry, I can't answer this query right now but I will improve from time to time."
|
| 60 |
-
|
| 61 |
logger.error(f"Error message sent to chat_id {chat_id}")
|
| 62 |
|
| 63 |
return jsonify({'status': 'ok'}), 200
|
|
@@ -79,7 +74,7 @@ async def set_telegram_webhook():
|
|
| 79 |
if result.get('ok'):
|
| 80 |
logger.info("Webhook set successfully.")
|
| 81 |
# Notify power user
|
| 82 |
-
|
| 83 |
return
|
| 84 |
elif result.get('error_code') == 429:
|
| 85 |
retry_after = result['parameters'].get('retry_after', retry_delay)
|
|
@@ -92,6 +87,18 @@ async def set_telegram_webhook():
|
|
| 92 |
logger.error(f"Request exception: {e}")
|
| 93 |
return
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
def run_flask_app():
|
| 96 |
"""Launches the Flask app and sets the Telegram webhook."""
|
| 97 |
asyncio.run(set_telegram_webhook())
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import logging
|
| 3 |
import httpx
|
| 4 |
import asyncio
|
| 5 |
from flask import Flask, request, jsonify
|
| 6 |
|
| 7 |
# Configuration
|
|
|
|
| 8 |
BOT_TOKEN = '7484321656:AAExhpS7sOGMu2BCuPQrDjuXpY3sEQmBgfY'
|
| 9 |
WEBHOOK_SECRET = 'A3%26c8%21jP%23xZ1v*Qw5kL%5E0tR%40u9%25yS6' # URL-encoded secret
|
| 10 |
POWER_USER_ID = 75516649
|
|
|
|
| 12 |
# Proxy Configuration
|
| 13 |
PROXY_URL = 'http://eR3LhYeoZXNWhIp:clIvQ2hSkO5CtLl@107.180.131.170:58874'
|
| 14 |
|
| 15 |
+
# Initialize logging
|
|
|
|
|
|
|
|
|
|
| 16 |
logging.basicConfig(
|
| 17 |
level=logging.DEBUG,
|
| 18 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
|
|
| 46 |
|
| 47 |
# Log and send the response
|
| 48 |
logger.debug(f"Generated response: {response_text}")
|
| 49 |
+
bot_send_message(chat_id, response_text)
|
| 50 |
logger.info(f"Response sent to chat_id {chat_id}")
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
logger.error(f"Error during processing: {e}")
|
| 54 |
error_message = "Sorry, I can't answer this query right now but I will improve from time to time."
|
| 55 |
+
bot_send_message(chat_id, error_message)
|
| 56 |
logger.error(f"Error message sent to chat_id {chat_id}")
|
| 57 |
|
| 58 |
return jsonify({'status': 'ok'}), 200
|
|
|
|
| 74 |
if result.get('ok'):
|
| 75 |
logger.info("Webhook set successfully.")
|
| 76 |
# Notify power user
|
| 77 |
+
await bot_send_message(POWER_USER_ID, "🚀 The webhook has been successfully connected! 🎉")
|
| 78 |
return
|
| 79 |
elif result.get('error_code') == 429:
|
| 80 |
retry_after = result['parameters'].get('retry_after', retry_delay)
|
|
|
|
| 87 |
logger.error(f"Request exception: {e}")
|
| 88 |
return
|
| 89 |
|
| 90 |
+
async def bot_send_message(chat_id, text):
|
| 91 |
+
"""Send a message to the specified chat ID using the proxy configuration."""
|
| 92 |
+
async with httpx.AsyncClient(proxies=PROXY_URL) as client:
|
| 93 |
+
response = await client.post(
|
| 94 |
+
f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage",
|
| 95 |
+
data={"chat_id": chat_id, "text": text, "parse_mode": "Markdown"}
|
| 96 |
+
)
|
| 97 |
+
if response.status_code == 200:
|
| 98 |
+
logger.info(f"Message sent successfully to chat_id {chat_id}")
|
| 99 |
+
else:
|
| 100 |
+
logger.error(f"Failed to send message: {response.text}")
|
| 101 |
+
|
| 102 |
def run_flask_app():
|
| 103 |
"""Launches the Flask app and sets the Telegram webhook."""
|
| 104 |
asyncio.run(set_telegram_webhook())
|