Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,18 +3,13 @@ import telebot
|
|
3 |
import logging
|
4 |
import httpx
|
5 |
from flask import Flask, request, jsonify
|
6 |
-
import
|
7 |
-
|
8 |
-
# Suppress experimental warnings
|
9 |
-
os.environ['HF_HUB_DISABLE_EXPERIMENTAL_WARNING'] = '1'
|
10 |
|
11 |
# Configuration
|
|
|
12 |
BOT_TOKEN = '7484321656:AAExhpS7sOGMu2BCuPQrDjuXpY3sEQmBgfY'
|
13 |
WEBHOOK_SECRET = 'A3%26c8%21jP%23xZ1v*Qw5kL%5E0tR%40u9%25yS6' # URL-encoded secret
|
14 |
|
15 |
-
# Proxy Configuration
|
16 |
-
PROXY_URL = 'http://eR3LhYeoZXNWhIp:[email protected]:58874'
|
17 |
-
|
18 |
# Initialize the Telegram bot
|
19 |
bot = telebot.TeleBot(BOT_TOKEN)
|
20 |
|
@@ -43,33 +38,35 @@ def handle_update():
|
|
43 |
|
44 |
if message_text:
|
45 |
try:
|
46 |
-
#
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
# Log the response
|
50 |
logger.debug(f"Generated response: {response_text}")
|
51 |
-
|
52 |
-
# Send the response to the chat
|
53 |
bot.send_message(chat_id, response_text, parse_mode='Markdown')
|
54 |
logger.info(f"Response sent to chat_id {chat_id}")
|
55 |
|
56 |
except Exception as e:
|
57 |
logger.error(f"Error during processing: {e}")
|
58 |
-
error_message = "Sorry, I can't
|
59 |
bot.send_message(chat_id, error_message, parse_mode='Markdown')
|
60 |
logger.error(f"Error message sent to chat_id {chat_id}")
|
61 |
|
62 |
return jsonify({'status': 'ok'}), 200
|
63 |
|
64 |
async def set_telegram_webhook():
|
65 |
-
"""Sets the webhook for the Telegram bot using a proxy."""
|
66 |
webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
|
67 |
retry_attempts = 5
|
68 |
retry_delay = 1 # seconds
|
69 |
|
70 |
for attempt in range(retry_attempts):
|
71 |
try:
|
72 |
-
async with httpx.AsyncClient(
|
73 |
response = await client.post(
|
74 |
f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
|
75 |
data={"url": webhook_url},
|
@@ -81,7 +78,7 @@ async def set_telegram_webhook():
|
|
81 |
elif result.get('error_code') == 429:
|
82 |
retry_after = result['parameters'].get('retry_after', retry_delay)
|
83 |
logger.warning(f"Rate limit exceeded. Retrying after {retry_after} seconds.")
|
84 |
-
|
85 |
else:
|
86 |
logger.error(f"Failed to set webhook: {result}")
|
87 |
return
|
@@ -91,7 +88,6 @@ async def set_telegram_webhook():
|
|
91 |
|
92 |
def run_flask_app():
|
93 |
"""Launches the Flask app and sets the Telegram webhook."""
|
94 |
-
import asyncio
|
95 |
asyncio.run(set_telegram_webhook())
|
96 |
app.run(host="0.0.0.0", port=5000, debug=True)
|
97 |
|
|
|
3 |
import logging
|
4 |
import httpx
|
5 |
from flask import Flask, request, jsonify
|
6 |
+
import asyncio
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
13 |
# Initialize the Telegram bot
|
14 |
bot = telebot.TeleBot(BOT_TOKEN)
|
15 |
|
|
|
38 |
|
39 |
if message_text:
|
40 |
try:
|
41 |
+
# Forward message to Gradio and get the response
|
42 |
+
response = requests.post('http://localhost:7860/gradio', json={'message': message_text})
|
43 |
+
if response.status_code == 200:
|
44 |
+
response_text = response.json().get('response', 'Sorry, I cannot process this request.')
|
45 |
+
else:
|
46 |
+
response_text = 'Error occurred while processing your request.'
|
47 |
|
48 |
+
# Log and send the response
|
49 |
logger.debug(f"Generated response: {response_text}")
|
|
|
|
|
50 |
bot.send_message(chat_id, response_text, parse_mode='Markdown')
|
51 |
logger.info(f"Response sent to chat_id {chat_id}")
|
52 |
|
53 |
except Exception as e:
|
54 |
logger.error(f"Error during processing: {e}")
|
55 |
+
error_message = "Sorry, I can't answer this query right now but I will improve from time to time."
|
56 |
bot.send_message(chat_id, error_message, parse_mode='Markdown')
|
57 |
logger.error(f"Error message sent to chat_id {chat_id}")
|
58 |
|
59 |
return jsonify({'status': 'ok'}), 200
|
60 |
|
61 |
async def set_telegram_webhook():
|
62 |
+
"""Sets the webhook for the Telegram bot without using a proxy."""
|
63 |
webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
|
64 |
retry_attempts = 5
|
65 |
retry_delay = 1 # seconds
|
66 |
|
67 |
for attempt in range(retry_attempts):
|
68 |
try:
|
69 |
+
async with httpx.AsyncClient() as client:
|
70 |
response = await client.post(
|
71 |
f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
|
72 |
data={"url": webhook_url},
|
|
|
78 |
elif result.get('error_code') == 429:
|
79 |
retry_after = result['parameters'].get('retry_after', retry_delay)
|
80 |
logger.warning(f"Rate limit exceeded. Retrying after {retry_after} seconds.")
|
81 |
+
await asyncio.sleep(retry_after)
|
82 |
else:
|
83 |
logger.error(f"Failed to set webhook: {result}")
|
84 |
return
|
|
|
88 |
|
89 |
def run_flask_app():
|
90 |
"""Launches the Flask app and sets the Telegram webhook."""
|
|
|
91 |
asyncio.run(set_telegram_webhook())
|
92 |
app.run(host="0.0.0.0", port=5000, debug=True)
|
93 |
|