Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,157 +1,80 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import httpx
|
| 5 |
from fastapi import FastAPI, Request
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# -------------------------
|
| 10 |
-
BOT_TOKEN = "8749818258:AAFls-Y46BXW-b94E8-HQTDCMosYfeEmB8E"
|
| 11 |
-
API_BASE = f"https://api.telegram.org/bot{BOT_TOKEN}"
|
| 12 |
-
FILE_BASE = f"https://api.telegram.org/file/bot{BOT_TOKEN}"
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
-
logging.basicConfig(level=logging.INFO)
|
| 16 |
|
| 17 |
user_files = {}
|
| 18 |
|
| 19 |
-
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
@app.post("/webhook")
|
| 58 |
async def webhook(request: Request):
|
| 59 |
-
update = await request.json()
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if "message" not in update:
|
| 63 |
-
return {"ok": True}
|
| 64 |
-
|
| 65 |
-
message = update["message"]
|
| 66 |
-
chat_id = message["chat"]["id"]
|
| 67 |
-
user_id = message["from"]["id"]
|
| 68 |
-
|
| 69 |
-
# -------------------------
|
| 70 |
-
# Commands
|
| 71 |
-
# -------------------------
|
| 72 |
-
if "text" in message and message["text"].startswith("/"):
|
| 73 |
-
cmd = message["text"]
|
| 74 |
-
|
| 75 |
-
if cmd == "/start":
|
| 76 |
-
await tg_api("sendMessage", {
|
| 77 |
-
"chat_id": chat_id,
|
| 78 |
-
"text": "Send a file, then reply with:\n"
|
| 79 |
-
"document / audio / video / photo / voice / animation"
|
| 80 |
-
})
|
| 81 |
-
return {"ok": True}
|
| 82 |
-
|
| 83 |
-
if cmd == "/reset":
|
| 84 |
-
user_files.pop(user_id, None)
|
| 85 |
-
await tg_api("sendMessage", {
|
| 86 |
-
"chat_id": chat_id,
|
| 87 |
-
"text": "Session cleared."
|
| 88 |
-
})
|
| 89 |
-
return {"ok": True}
|
| 90 |
-
|
| 91 |
-
# -------------------------
|
| 92 |
-
# File Reception
|
| 93 |
-
# -------------------------
|
| 94 |
-
media_types = ["document", "audio", "video", "photo", "voice", "animation"]
|
| 95 |
-
|
| 96 |
-
for media in media_types:
|
| 97 |
-
if media in message:
|
| 98 |
-
if media == "photo":
|
| 99 |
-
file_id = message["photo"][-1]["file_id"]
|
| 100 |
-
else:
|
| 101 |
-
file_id = message[media]["file_id"]
|
| 102 |
-
|
| 103 |
-
user_files[user_id] = file_id
|
| 104 |
-
|
| 105 |
-
await tg_api("sendMessage", {
|
| 106 |
-
"chat_id": chat_id,
|
| 107 |
-
"text": f"Received as {media}.\nNow reply with desired method."
|
| 108 |
-
})
|
| 109 |
-
|
| 110 |
-
return {"ok": True}
|
| 111 |
-
|
| 112 |
-
# -------------------------
|
| 113 |
-
# Conversion
|
| 114 |
-
# -------------------------
|
| 115 |
-
if "text" in message:
|
| 116 |
-
desired = message["text"].lower().strip()
|
| 117 |
-
|
| 118 |
-
if user_id not in user_files:
|
| 119 |
-
await tg_api("sendMessage", {
|
| 120 |
-
"chat_id": chat_id,
|
| 121 |
-
"text": "Send a file first."
|
| 122 |
-
})
|
| 123 |
-
return {"ok": True}
|
| 124 |
-
|
| 125 |
-
file_id = user_files[user_id]
|
| 126 |
-
|
| 127 |
-
method_map = {
|
| 128 |
-
"document": "sendDocument",
|
| 129 |
-
"audio": "sendAudio",
|
| 130 |
-
"video": "sendVideo",
|
| 131 |
-
"photo": "sendPhoto",
|
| 132 |
-
"voice": "sendVoice",
|
| 133 |
-
"animation": "sendAnimation"
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
if desired not in method_map:
|
| 137 |
-
await tg_api("sendMessage", {
|
| 138 |
-
"chat_id": chat_id,
|
| 139 |
-
"text": "Unknown format."
|
| 140 |
-
})
|
| 141 |
-
return {"ok": True}
|
| 142 |
-
|
| 143 |
-
# Step 1: Get file path
|
| 144 |
-
file_path = await get_file_path(file_id)
|
| 145 |
-
|
| 146 |
-
# Step 2: Download file
|
| 147 |
-
local_file = await download_file(file_path)
|
| 148 |
-
|
| 149 |
-
# Step 3: Re-upload as desired type
|
| 150 |
-
with open(local_file, "rb") as f:
|
| 151 |
-
await tg_api(method_map[desired], {
|
| 152 |
-
"chat_id": chat_id
|
| 153 |
-
}, files={desired: f})
|
| 154 |
-
|
| 155 |
-
return {"ok": True}
|
| 156 |
-
|
| 157 |
return {"ok": True}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import telebot
|
| 3 |
+
import requests
|
|
|
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
|
| 6 |
+
TOKEN = "8749818258:AAFls-Y46BXW-b94E8-HQTDCMosYfeEmB8E"
|
| 7 |
+
bot = telebot.TeleBot(TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
|
|
| 10 |
|
| 11 |
user_files = {}
|
| 12 |
|
| 13 |
+
# -----------------------
|
| 14 |
+
# FILE HANDLER
|
| 15 |
+
# -----------------------
|
| 16 |
+
@bot.message_handler(content_types=['document','video','audio','photo','voice','animation'])
|
| 17 |
+
def handle_file(message):
|
| 18 |
+
user_files[message.from_user.id] = message
|
| 19 |
+
|
| 20 |
+
bot.send_message(
|
| 21 |
+
message.chat.id,
|
| 22 |
+
"File received.\nReply with:\n"
|
| 23 |
+
"document / video / audio / photo / voice / animation"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# -----------------------
|
| 27 |
+
# CONVERSION HANDLER
|
| 28 |
+
# -----------------------
|
| 29 |
+
@bot.message_handler(func=lambda m: True)
|
| 30 |
+
def convert(message):
|
| 31 |
+
desired = message.text.lower()
|
| 32 |
+
|
| 33 |
+
if message.from_user.id not in user_files:
|
| 34 |
+
bot.send_message(message.chat.id, "Send a file first.")
|
| 35 |
+
return
|
| 36 |
+
|
| 37 |
+
original = user_files[message.from_user.id]
|
| 38 |
+
file_id = None
|
| 39 |
+
|
| 40 |
+
if original.document:
|
| 41 |
+
file_id = original.document.file_id
|
| 42 |
+
elif original.video:
|
| 43 |
+
file_id = original.video.file_id
|
| 44 |
+
elif original.audio:
|
| 45 |
+
file_id = original.audio.file_id
|
| 46 |
+
elif original.photo:
|
| 47 |
+
file_id = original.photo[-1].file_id
|
| 48 |
+
elif original.voice:
|
| 49 |
+
file_id = original.voice.file_id
|
| 50 |
+
elif original.animation:
|
| 51 |
+
file_id = original.animation.file_id
|
| 52 |
+
|
| 53 |
+
file_info = bot.get_file(file_id)
|
| 54 |
+
file_url = f"https://api.telegram.org/file/bot{TOKEN}/{file_info.file_path}"
|
| 55 |
+
|
| 56 |
+
file_bytes = requests.get(file_url).content
|
| 57 |
+
|
| 58 |
+
if desired == "video":
|
| 59 |
+
bot.send_video(message.chat.id, file_bytes)
|
| 60 |
+
elif desired == "audio":
|
| 61 |
+
bot.send_audio(message.chat.id, file_bytes)
|
| 62 |
+
elif desired == "document":
|
| 63 |
+
bot.send_document(message.chat.id, file_bytes)
|
| 64 |
+
elif desired == "photo":
|
| 65 |
+
bot.send_photo(message.chat.id, file_bytes)
|
| 66 |
+
elif desired == "voice":
|
| 67 |
+
bot.send_voice(message.chat.id, file_bytes)
|
| 68 |
+
elif desired == "animation":
|
| 69 |
+
bot.send_animation(message.chat.id, file_bytes)
|
| 70 |
+
else:
|
| 71 |
+
bot.send_message(message.chat.id, "Unknown format.")
|
| 72 |
+
|
| 73 |
+
# -----------------------
|
| 74 |
+
# WEBHOOK ROUTE
|
| 75 |
+
# -----------------------
|
| 76 |
@app.post("/webhook")
|
| 77 |
async def webhook(request: Request):
|
| 78 |
+
update = telebot.types.Update.de_json(await request.json())
|
| 79 |
+
bot.process_new_updates([update])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
return {"ok": True}
|