measmonysuon commited on
Commit
a0145ee
·
verified ·
1 Parent(s): 1d25c52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import telebot
3
  import google.generativeai as genai
4
  import logging
5
- import requests
6
  from flask import Flask, request, jsonify
7
 
8
  # Suppress experimental warnings
@@ -14,9 +14,7 @@ BOT_TOKEN = '7484321656:AAExhpS7sOGMu2BCuPQrDjuXpY3sEQmBgfY'
14
  WEBHOOK_SECRET = 'A3%26c8%21jP%23xZ1v*Qw5kL%5E0tR%40u9%25yS6' # URL-encoded secret
15
 
16
  # Proxy Configuration
17
- PROXY = {
18
- 'http': 'http://107.180.131.170:58874@eR3LhYeoZXNWhIp:clIvQ2hSkO5CtLl',
19
- }
20
 
21
  # Initialize the Telegram bot
22
  bot = telebot.TeleBot(BOT_TOKEN)
@@ -96,27 +94,27 @@ def handle_update():
96
 
97
  return jsonify({'status': 'ok'}), 200
98
 
99
- def set_telegram_webhook():
100
  """Sets the webhook for the Telegram bot using a proxy."""
101
  webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
102
  try:
103
- response = requests.post(
104
- f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
105
- data={"url": webhook_url},
106
- proxies=PROXY,
107
- verify=False # **Not recommended for production**
108
- )
109
- result = response.json()
110
- if result.get('ok'):
111
- logger.info("Webhook set successfully.")
112
- else:
113
- logger.error(f"Failed to set webhook: {result}")
114
- except requests.exceptions.RequestException as e:
115
  logger.error(f"Request exception: {e}")
116
 
117
  def run_flask_app():
118
  """Launches the Flask app and sets the Telegram webhook."""
119
- set_telegram_webhook()
 
120
  app.run(host="0.0.0.0", port=5000, debug=True)
121
 
122
  if __name__ == "__main__":
 
2
  import telebot
3
  import google.generativeai as genai
4
  import logging
5
+ import httpx
6
  from flask import Flask, request, jsonify
7
 
8
  # Suppress experimental warnings
 
14
  WEBHOOK_SECRET = 'A3%26c8%21jP%23xZ1v*Qw5kL%5E0tR%40u9%25yS6' # URL-encoded secret
15
 
16
  # Proxy Configuration
17
+ PROXY_URL = 'http://eR3LhYeoZXNWhIp:clIvQ2hSkO5CtLl@107.180.131.170:58874'
 
 
18
 
19
  # Initialize the Telegram bot
20
  bot = telebot.TeleBot(BOT_TOKEN)
 
94
 
95
  return jsonify({'status': 'ok'}), 200
96
 
97
+ async def set_telegram_webhook():
98
  """Sets the webhook for the Telegram bot using a proxy."""
99
  webhook_url = f"https://measmonysuon-flyingbird.hf.space/webhooks/{WEBHOOK_SECRET}"
100
  try:
101
+ async with httpx.AsyncClient(proxies=PROXY_URL) as client:
102
+ response = await client.post(
103
+ f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
104
+ data={"url": webhook_url},
105
+ )
106
+ result = response.json()
107
+ if result.get('ok'):
108
+ logger.info("Webhook set successfully.")
109
+ else:
110
+ logger.error(f"Failed to set webhook: {result}")
111
+ except httpx.RequestError as e:
 
112
  logger.error(f"Request exception: {e}")
113
 
114
  def run_flask_app():
115
  """Launches the Flask app and sets the Telegram webhook."""
116
+ import asyncio
117
+ asyncio.run(set_telegram_webhook())
118
  app.run(host="0.0.0.0", port=5000, debug=True)
119
 
120
  if __name__ == "__main__":