measmonysuon commited on
Commit
cac2559
·
verified ·
1 Parent(s): 8453328

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -10,6 +10,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
 
@@ -39,11 +42,15 @@ def handle_update():
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}")
@@ -59,14 +66,14 @@ def handle_update():
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},
 
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:[email protected]:58874'
15
+
16
  # Initialize the Telegram bot
17
  bot = telebot.TeleBot(BOT_TOKEN)
18
 
 
42
  if message_text:
43
  try:
44
  # Forward message to Gradio and get the response
45
+ async def get_gradio_response(message_text):
46
+ async with httpx.AsyncClient(proxies=PROXY_URL) as client:
47
+ response = await client.post('http://localhost:7860/gradio', json={'message': message_text})
48
+ if response.status_code == 200:
49
+ return response.json().get('response', 'Sorry, I cannot process this request.')
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
  return jsonify({'status': 'ok'}), 200
67
 
68
  async def set_telegram_webhook():
69
+ """Sets the webhook for the Telegram bot using a proxy."""
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(proxies=PROXY_URL) as client:
77
  response = await client.post(
78
  f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
79
  data={"url": webhook_url},