measmonysuon commited on
Commit
cb28e95
·
verified ·
1 Parent(s): e337b88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -6,7 +6,7 @@ from flask import Flask, request, jsonify
6
 
7
  # Configuration
8
  BOT_TOKEN = '7484321656:AAExhpS7sOGMu2BCuPQrDjuXpY3sEQmBgfY'
9
- WEBHOOK_SECRET = 'my_secret' # URL-encoded secret
10
  POWER_USER_ID = 75516649
11
 
12
  # Proxy Configuration
@@ -49,9 +49,21 @@ def handle_update():
49
 
50
  if message_text:
51
  try:
52
- response_text = "This is a static response for testing purposes."
53
- asyncio.run(bot_send_message(chat_id, response_text))
54
- logger.info(f"Static response sent to chat_id {chat_id}")
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  except Exception as e:
57
  logger.error(f"Error during processing: {e}")
@@ -61,9 +73,9 @@ def handle_update():
61
 
62
  return jsonify({'status': 'ok'}), 200
63
 
64
- async def set_telegram_webhook():
65
- """Sets the webhook for the Telegram bot with proxy configuration."""
66
- webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
67
  retry_attempts = 5
68
  retry_delay = 1 # seconds
69
 
@@ -91,7 +103,8 @@ async def set_telegram_webhook():
91
 
92
  def run_flask_app():
93
  """Launches the Flask app and sets the Telegram webhook."""
94
- asyncio.run(set_telegram_webhook()) # Set the webhook before starting the Flask app
 
95
  app.run(host="0.0.0.0", port=5000, debug=True)
96
 
97
  if __name__ == "__main__":
 
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
11
 
12
  # Proxy Configuration
 
49
 
50
  if message_text:
51
  try:
52
+ # Forward message to Gradio and get the response
53
+ async def forward_message():
54
+ async with httpx.AsyncClient(proxies=PROXY_URL) as client:
55
+ response = await client.post(
56
+ 'http://localhost:7860/', # Gradio app URL
57
+ json={'text': message_text}
58
+ )
59
+ if response.status_code == 200:
60
+ response_text = response.json().get('data', 'Error processing request.')
61
+ else:
62
+ response_text = 'Error occurred while processing your request.'
63
+ await bot_send_message(chat_id, response_text)
64
+
65
+ asyncio.run(forward_message())
66
+ logger.info(f"Response sent to chat_id {chat_id}")
67
 
68
  except Exception as e:
69
  logger.error(f"Error during processing: {e}")
 
73
 
74
  return jsonify({'status': 'ok'}), 200
75
 
76
+ async def set_telegram_webhook(flask_url):
77
+ """Sets the webhook for the Telegram bot using the provided Flask URL."""
78
+ webhook_url = f"{flask_url}/webhooks/{WEBHOOK_SECRET}"
79
  retry_attempts = 5
80
  retry_delay = 1 # seconds
81
 
 
103
 
104
  def run_flask_app():
105
  """Launches the Flask app and sets the Telegram webhook."""
106
+ flask_url = "https://your-flask-url.com" # Replace with your actual Flask URL
107
+ asyncio.run(set_telegram_webhook(flask_url)) # Set the webhook before starting the Flask app
108
  app.run(host="0.0.0.0", port=5000, debug=True)
109
 
110
  if __name__ == "__main__":