Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import threading
|
| 3 |
import telebot
|
| 4 |
-
import requests
|
| 5 |
from fastapi import FastAPI
|
| 6 |
|
| 7 |
TOKEN = "8749818258:AAFls-Y46BXW-b94E8-HQTDCMosYfeEmB8E"
|
|
@@ -11,23 +10,17 @@ app = FastAPI()
|
|
| 11 |
|
| 12 |
user_files = {}
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
# Health Check
|
| 16 |
-
# -------------------------
|
| 17 |
@app.get("/")
|
| 18 |
def root():
|
| 19 |
return {"status": "ok"}
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
# Start Polling on Startup
|
| 23 |
-
# -------------------------
|
| 24 |
@app.on_event("startup")
|
| 25 |
def startup():
|
| 26 |
threading.Thread(target=bot.infinity_polling, daemon=True).start()
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
# Handle File
|
| 30 |
-
# -------------------------
|
| 31 |
@bot.message_handler(content_types=[
|
| 32 |
'document','video','audio','photo','voice','animation'
|
| 33 |
])
|
|
@@ -39,9 +32,7 @@ def handle_file(message):
|
|
| 39 |
"document / video / audio / photo / voice / animation"
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
# Handle Conversion
|
| 44 |
-
# -------------------------
|
| 45 |
@bot.message_handler(func=lambda m: True)
|
| 46 |
def convert(message):
|
| 47 |
desired = message.text.lower().strip()
|
|
@@ -52,6 +43,7 @@ def convert(message):
|
|
| 52 |
|
| 53 |
original = user_files[message.from_user.id]
|
| 54 |
|
|
|
|
| 55 |
file_id = None
|
| 56 |
if original.document:
|
| 57 |
file_id = original.document.file_id
|
|
@@ -66,9 +58,14 @@ def convert(message):
|
|
| 66 |
elif original.animation:
|
| 67 |
file_id = original.animation.file_id
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
file_info = bot.get_file(file_id)
|
| 70 |
-
|
| 71 |
-
file_bytes = requests.get(file_url).content
|
| 72 |
|
| 73 |
if desired == "video":
|
| 74 |
bot.send_video(message.chat.id, file_bytes)
|
|
|
|
| 1 |
import os
|
| 2 |
import threading
|
| 3 |
import telebot
|
|
|
|
| 4 |
from fastapi import FastAPI
|
| 5 |
|
| 6 |
TOKEN = "8749818258:AAFls-Y46BXW-b94E8-HQTDCMosYfeEmB8E"
|
|
|
|
| 10 |
|
| 11 |
user_files = {}
|
| 12 |
|
| 13 |
+
# Health endpoint (HF requirement)
|
|
|
|
|
|
|
| 14 |
@app.get("/")
|
| 15 |
def root():
|
| 16 |
return {"status": "ok"}
|
| 17 |
|
| 18 |
+
# Start polling in background
|
|
|
|
|
|
|
| 19 |
@app.on_event("startup")
|
| 20 |
def startup():
|
| 21 |
threading.Thread(target=bot.infinity_polling, daemon=True).start()
|
| 22 |
|
| 23 |
+
# Store file
|
|
|
|
|
|
|
| 24 |
@bot.message_handler(content_types=[
|
| 25 |
'document','video','audio','photo','voice','animation'
|
| 26 |
])
|
|
|
|
| 32 |
"document / video / audio / photo / voice / animation"
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# Convert by downloading via TeleBot internal system
|
|
|
|
|
|
|
| 36 |
@bot.message_handler(func=lambda m: True)
|
| 37 |
def convert(message):
|
| 38 |
desired = message.text.lower().strip()
|
|
|
|
| 43 |
|
| 44 |
original = user_files[message.from_user.id]
|
| 45 |
|
| 46 |
+
# Extract file_id safely
|
| 47 |
file_id = None
|
| 48 |
if original.document:
|
| 49 |
file_id = original.document.file_id
|
|
|
|
| 58 |
elif original.animation:
|
| 59 |
file_id = original.animation.file_id
|
| 60 |
|
| 61 |
+
if not file_id:
|
| 62 |
+
bot.reply_to(message, "Unsupported type.")
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
# 🔥 This is key:
|
| 66 |
+
# TeleBot handles token internally.
|
| 67 |
file_info = bot.get_file(file_id)
|
| 68 |
+
file_bytes = bot.download_file(file_info.file_path)
|
|
|
|
| 69 |
|
| 70 |
if desired == "video":
|
| 71 |
bot.send_video(message.chat.id, file_bytes)
|