Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,14 @@ import os
|
|
| 2 |
import telebot
|
| 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 |
-
# Proxy Configuration
|
| 14 |
-
PROXY_URL = 'http://eR3LhYeoZXNWhIp:clIvQ2hSkO5CtLl@107.180.131.170:58874'
|
| 15 |
|
| 16 |
# Initialize the Telegram bot
|
| 17 |
bot = telebot.TeleBot(BOT_TOKEN)
|
|
@@ -42,15 +40,11 @@ def handle_update():
|
|
| 42 |
if message_text:
|
| 43 |
try:
|
| 44 |
# Forward message to Gradio and get the response
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
else:
|
| 51 |
-
return 'Error occurred while processing your request.'
|
| 52 |
-
|
| 53 |
-
response_text = asyncio.run(get_gradio_response(message_text))
|
| 54 |
|
| 55 |
# Log and send the response
|
| 56 |
logger.debug(f"Generated response: {response_text}")
|
|
@@ -66,14 +60,14 @@ def handle_update():
|
|
| 66 |
return jsonify({'status': 'ok'}), 200
|
| 67 |
|
| 68 |
async def set_telegram_webhook():
|
| 69 |
-
"""Sets the webhook for the Telegram bot
|
| 70 |
webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
|
| 71 |
retry_attempts = 5
|
| 72 |
retry_delay = 1 # seconds
|
| 73 |
|
| 74 |
for attempt in range(retry_attempts):
|
| 75 |
try:
|
| 76 |
-
async with httpx.AsyncClient(
|
| 77 |
response = await client.post(
|
| 78 |
f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
|
| 79 |
data={"url": webhook_url},
|
|
@@ -81,6 +75,8 @@ async def set_telegram_webhook():
|
|
| 81 |
result = response.json()
|
| 82 |
if result.get('ok'):
|
| 83 |
logger.info("Webhook set successfully.")
|
|
|
|
|
|
|
| 84 |
return
|
| 85 |
elif result.get('error_code') == 429:
|
| 86 |
retry_after = result['parameters'].get('retry_after', retry_delay)
|
|
|
|
| 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
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Initialize the Telegram bot
|
| 15 |
bot = telebot.TeleBot(BOT_TOKEN)
|
|
|
|
| 40 |
if message_text:
|
| 41 |
try:
|
| 42 |
# Forward message to Gradio and get the response
|
| 43 |
+
response = requests.post('http://localhost:7860/gradio', json={'message': message_text})
|
| 44 |
+
if response.status_code == 200:
|
| 45 |
+
response_text = response.json().get('response', 'Sorry, I cannot process this request.')
|
| 46 |
+
else:
|
| 47 |
+
response_text = 'Error occurred while processing your request.'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Log and send the response
|
| 50 |
logger.debug(f"Generated response: {response_text}")
|
|
|
|
| 60 |
return jsonify({'status': 'ok'}), 200
|
| 61 |
|
| 62 |
async def set_telegram_webhook():
|
| 63 |
+
"""Sets the webhook for the Telegram bot."""
|
| 64 |
webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
|
| 65 |
retry_attempts = 5
|
| 66 |
retry_delay = 1 # seconds
|
| 67 |
|
| 68 |
for attempt in range(retry_attempts):
|
| 69 |
try:
|
| 70 |
+
async with httpx.AsyncClient() as client:
|
| 71 |
response = await client.post(
|
| 72 |
f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
|
| 73 |
data={"url": webhook_url},
|
|
|
|
| 75 |
result = response.json()
|
| 76 |
if result.get('ok'):
|
| 77 |
logger.info("Webhook set successfully.")
|
| 78 |
+
# Notify power user
|
| 79 |
+
bot.send_message(POWER_USER_ID, "🚀 The webhook has been successfully connected! 🎉", parse_mode='Markdown')
|
| 80 |
return
|
| 81 |
elif result.get('error_code') == 429:
|
| 82 |
retry_after = result['parameters'].get('retry_after', retry_delay)
|