Sheeturt commited on
Commit
6f9fb4d
Β·
verified Β·
1 Parent(s): 407d2ff

Fix: switch from polling to webhook mode (polling fails on HuggingFace due to SSL timeout to api.telegram.org)

Browse files
Files changed (1) hide show
  1. host.py +25 -22
host.py CHANGED
@@ -26,22 +26,23 @@ import atexit
26
  import requests # For polling exceptions
27
 
28
  # --- Flask Keep Alive ---
29
- from flask import Flask
30
  from threading import Thread
31
 
32
  app = Flask('')
33
 
34
  @app.route('/')
35
  def home():
36
- return "I am uggfy File Host"
37
- def run_flask():
38
- port = int(os.environ.get("PORT", 7860))
39
- app.run(host='0.0.0.0', port=port)
40
- def keep_alive():
41
- t = Thread(target=run_flask)
42
- t.daemon = True # Allows program to exit even if this thread is running
43
- t.start()
44
- print("Flask Keep-Alive server started.")
 
45
  # --- End Flask Keep Alive ---
46
 
47
  # --- Auto Ping for Hugging Face Spaces ---
@@ -2227,16 +2228,18 @@ if __name__ == '__main__':
2227
  logger.info("="*40 + "\nπŸ€– Bot Starting Up...\n" + f"🐍 Python: {sys.version.split()[0]}\n" +
2228
  f"πŸ”§ Base Dir: {BASE_DIR}\nπŸ“ Upload Dir: {UPLOAD_BOTS_DIR}\n" +
2229
  f"πŸ“Š Data Dir: {IROTECH_DIR}\nπŸ”‘ Owner ID: {OWNER_ID}\nπŸ›‘οΈ Admins: {admin_ids}\n" + "="*40)
2230
- keep_alive()
2231
  start_auto_ping()
2232
- try: bot.remove_webhook()
2233
- except: pass
2234
- logger.info("πŸš€ Starting polling...")
2235
- while True:
2236
- try:
2237
- bot.infinity_polling(logger_level=logging.INFO)
2238
- except requests.exceptions.ConnectionError as ce: logger.error(f"Polling ConnectionError: {ce}. Retrying in 15s..."); time.sleep(15)
2239
- except Exception as e:
2240
- logger.critical(f"πŸ’₯ Unrecoverable polling error: {e}", exc_info=True)
2241
- logger.info("Restarting polling in 30s due to critical error..."); time.sleep(30)
2242
- finally: logger.warning("Polling attempt finished. Will restart if in loop."); time.sleep(1)
 
 
 
 
26
  import requests # For polling exceptions
27
 
28
  # --- Flask Keep Alive ---
29
+ from flask import Flask, request
30
  from threading import Thread
31
 
32
  app = Flask('')
33
 
34
  @app.route('/')
35
  def home():
36
+ return "I am uggfy File Host"
37
+
38
+ @app.route('/webhook', methods=['POST'])
39
+ def webhook():
40
+ if request.headers.get('content-type') == 'application/json':
41
+ json_string = request.get_data().decode('utf-8')
42
+ update = telebot.types.Update.de_json(json_string)
43
+ bot.process_new_updates([update])
44
+ return 'ok', 200
45
+ return 'bad request', 400
46
  # --- End Flask Keep Alive ---
47
 
48
  # --- Auto Ping for Hugging Face Spaces ---
 
2228
  logger.info("="*40 + "\nπŸ€– Bot Starting Up...\n" + f"🐍 Python: {sys.version.split()[0]}\n" +
2229
  f"πŸ”§ Base Dir: {BASE_DIR}\nπŸ“ Upload Dir: {UPLOAD_BOTS_DIR}\n" +
2230
  f"πŸ“Š Data Dir: {IROTECH_DIR}\nπŸ”‘ Owner ID: {OWNER_ID}\nπŸ›‘οΈ Admins: {admin_ids}\n" + "="*40)
 
2231
  start_auto_ping()
2232
+ # Set webhook
2233
+ WEBHOOK_URL = os.environ.get('SPACE_HOST', 'https://sheeturt-kgffk.hf.space')
2234
+ WEBHOOK_PATH = '/webhook'
2235
+ full_webhook_url = WEBHOOK_URL.rstrip('/') + WEBHOOK_PATH
2236
+ try:
2237
+ bot.remove_webhook()
2238
+ time.sleep(1)
2239
+ bot.set_webhook(url=full_webhook_url)
2240
+ logger.info(f"βœ… Webhook set to: {full_webhook_url}")
2241
+ except Exception as e:
2242
+ logger.error(f"❌ Failed to set webhook: {e}")
2243
+ logger.info("πŸš€ Starting Flask webhook server...")
2244
+ port = int(os.environ.get("PORT", 7860))
2245
+ app.run(host='0.0.0.0', port=port)