mirror of
https://github.com/TheCaduceus/FileStreamBot.git
synced 2026-01-15 08:23:28 -03:00
Initial commit.
This commit is contained in:
8
Dockerfile
Normal file
8
Dockerfile
Normal file
@@ -0,0 +1,8 @@
|
||||
FROM python:3.11
|
||||
|
||||
WORKDIR /app
|
||||
COPY . /app
|
||||
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
CMD ["python", "-m", "bot"]
|
||||
115
README.md
Normal file
115
README.md
Normal file
@@ -0,0 +1,115 @@
|
||||
<div align="center"><h1>🌐File Stream Bot</h1>
|
||||
<b>An open-source Python Telegram bot to transmit Telegram files over HTTP.</b>
|
||||
|
||||
<a href="https://t.me/DrFileStreamBot"><b>Demo Bot</b></a>
|
||||
</div><br>
|
||||
|
||||
## **📑 INDEX**
|
||||
|
||||
* [**⚙️ Installation**](#installation)
|
||||
* [Python & Git](#i-1)
|
||||
* [Download](#i-2)
|
||||
* [Requirements](#i-3)
|
||||
* [**📝 Variables**](#variables)
|
||||
* [**🕹 Deployment**](#deployment)
|
||||
* [Locally](#d-1)
|
||||
* [Docker](#d-2)
|
||||
* [**⛑️ Need help!**](#help)
|
||||
* [**❤️ Credits & Thanks**](#credits)
|
||||
|
||||
<a name="installation"></a>
|
||||
|
||||
## ⚙️ Installation
|
||||
|
||||
<a name="i-1"></a>
|
||||
|
||||
**1.Install Python & Git:**
|
||||
|
||||
For Windows:
|
||||
```
|
||||
winget install Python.Python.3.11
|
||||
winget install Git.Git
|
||||
```
|
||||
For Linux:
|
||||
```
|
||||
sudo apt-get update && sudo apt-get install -y python3.11 git pip
|
||||
```
|
||||
For macOS:
|
||||
```
|
||||
brew install python@3.11 git
|
||||
```
|
||||
For Termux:
|
||||
```
|
||||
pkg install python -y
|
||||
pkg install git -y
|
||||
```
|
||||
|
||||
<a name="i-2"></a>
|
||||
|
||||
**2.Download repository:**
|
||||
```
|
||||
git clone https://github.com/TheCaduceus/FileStreamBot.git
|
||||
```
|
||||
|
||||
**3.Change Directory:**
|
||||
|
||||
```
|
||||
cd FileStreamBot
|
||||
```
|
||||
|
||||
<a name="i-3"></a>
|
||||
|
||||
**4.Install requirements:**
|
||||
|
||||
```
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
<a name="variables"></a>
|
||||
|
||||
## 📝 Variables
|
||||
**The variables provided below should either be completed within the [config.py](https://github.com/TheCaduceus/FileStreamBot/blob/main/bot/config.py) file or configured as environment variables.**
|
||||
* `API_ID`|`TELEGRAM_API_ID`: API ID of your Telegram account, can be obtained from [My Telegram](https://my.telegram.org). `int`
|
||||
* `API_HASH`|`TELEGRAM_API_HASH`: API hash of your Telegram account, can be obtained from [My Telegram](https://my.telegram.org). `str`
|
||||
* `OWNER_ID`: ID of your Telegram account, can be obtained by sending **/info** to [@DrFileStreamBot](https://t.me/DrFileStreamBot). `int`
|
||||
* `ALLOWED_USER_IDS`: A list of Telegram account IDs (separated by spaces) that are permitted to use the bot. Leave this field empty to allow anyone to use it. `str`
|
||||
* `BOT_USERNAME`|`TELEGRAM_BOT_USERNAME`: Username of your Telegram bot, create one using [@BotFather](https://t.me/BotFather). `str`
|
||||
* `BOT_TOKEN`|`TELEGRAM_BOT_TOKEN`: Telegram API token of your bot, can be obtained from [@BotFather](https://t.me/BotFather). `str`
|
||||
* `CHANNEL_ID`|`TELEGRAM_CHANNEL_ID`: ID of the channel where bot will forward all files received from users, can be obtained by forwarding any message from channel to [@ShowJsonBot](https://t.me/ShowJsonBot) and then looking from `forward_from_chat` key. `int`
|
||||
* `BOT_WORKERS`: Number of updates bot should process from Telegram at once, by default to 10 updates. `int`
|
||||
* `SECRET_CODE_LENGTH`: Number of characters that file code should contain, by default to 12 characters. `int`
|
||||
* `BASE_URL`: Base URL that bot should use while generating file links, can be FQDN and by default to `127.0.0.1`. `str`
|
||||
* `BIND_ADDRESS`: Bind address for web server, by default to `0.0.0.0` to run on all possible addresses. `str`
|
||||
* `PORT`: Port for web server to run on, by default to `8080`. `int`
|
||||
|
||||
## 🕹 Deployment
|
||||
|
||||
<a name="d-1"></a>
|
||||
|
||||
**1.Running locally:**
|
||||
```
|
||||
python -m bot
|
||||
```
|
||||
|
||||
<a name="d-2"></a>
|
||||
|
||||
**2.Using Docker:** *(Recommended)*
|
||||
* Build own Docker image:
|
||||
```
|
||||
docker build -t file-stream-bot .
|
||||
```
|
||||
* Run the Docker container:
|
||||
```
|
||||
docker run -p 8080:8080 file-stream-bot
|
||||
```
|
||||
|
||||
<a name="help"></a>
|
||||
|
||||
## ⛑️ Need help!
|
||||
- Ask questions or doubts [here](https://t.me/DrDiscussion).
|
||||
|
||||
<a name="credits"></a>
|
||||
|
||||
## ❤️ Credits & Thanks
|
||||
|
||||
[**Dr.Caduceus**](https://github.com/TheCaduceus): Owner & developer of Microsoft E5 Auto Renewal Tool.<br>
|
||||
19
bot/__init__.py
Normal file
19
bot/__init__.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from pyrogram import Client
|
||||
from logging import getLogger
|
||||
from logging.config import dictConfig
|
||||
from .config import Telegram, LOGGER_CONFIG_JSON
|
||||
|
||||
dictConfig(LOGGER_CONFIG_JSON)
|
||||
|
||||
version = 1.0
|
||||
logger = getLogger('bot')
|
||||
|
||||
TelegramBot = Client(
|
||||
name="bot",
|
||||
api_id = Telegram.API_ID,
|
||||
api_hash = Telegram.API_HASH,
|
||||
bot_token = Telegram.BOT_TOKEN,
|
||||
plugins={"root": "bot/plugins"},
|
||||
workers = Telegram.BOT_WORKERS,
|
||||
max_concurrent_transmissions=1000
|
||||
)
|
||||
7
bot/__main__.py
Normal file
7
bot/__main__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from bot import TelegramBot, logger
|
||||
from bot.server import server
|
||||
|
||||
if __name__ == '__main__':
|
||||
logger.info('Initializing...')
|
||||
TelegramBot.loop.create_task(server.serve())
|
||||
TelegramBot.run()
|
||||
53
bot/config.py
Normal file
53
bot/config.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from os import environ as env
|
||||
|
||||
class Telegram:
|
||||
API_ID = env.get("TELEGRAM_API_ID", 1234)
|
||||
API_HASH = env.get("TELEGRAM_API_HASH", "xyz")
|
||||
OWNER_ID = int(env.get("OWNER_ID", 1234567890))
|
||||
ALLOWED_USER_IDS = env.get("ALLOWED_USER_IDS", "").split()
|
||||
BOT_USERNAME = env.get("TELEGRAM_BOT_USERNAME", "BotFather")
|
||||
BOT_TOKEN = env.get("TELEGRAM_BOT_TOKEN", "1234:abcd")
|
||||
CHANNEL_ID = int(env.get("TELEGRAM_CHANNEL_ID", -1001234567890))
|
||||
BOT_WORKERS = int(env.get("BOT_WORKERS", 10))
|
||||
SECRET_CODE_LENGTH = int(env.get("SECRET_CODE_LENGTH", 12))
|
||||
|
||||
class Server:
|
||||
BASE_URL = env.get("BASE_URL", "http://127.0.0.1:8080")
|
||||
BIND_ADDRESS = env.get("BIND_ADDRESS", "0.0.0.0")
|
||||
PORT = int(env.get("PORT", 8080))
|
||||
|
||||
# LOGGING CONFIGURATION
|
||||
LOGGER_CONFIG_JSON = {
|
||||
'version': 1,
|
||||
'formatters': {
|
||||
'default': {
|
||||
'format': '[%(asctime)s][%(name)s][%(levelname)s] -> %(message)s',
|
||||
'datefmt': '%d/%m/%Y %H:%M:%S'
|
||||
},
|
||||
},
|
||||
'handlers': {
|
||||
'file_handler': {
|
||||
'class': 'logging.FileHandler',
|
||||
'filename': 'event-log.txt',
|
||||
'formatter': 'default'
|
||||
},
|
||||
'stream_handler': {
|
||||
'class': 'logging.StreamHandler',
|
||||
'formatter': 'default'
|
||||
}
|
||||
},
|
||||
'loggers': {
|
||||
'uvicorn': {
|
||||
'level': 'INFO',
|
||||
'handlers': ['file_handler', 'stream_handler']
|
||||
},
|
||||
'uvicorn.error': {
|
||||
'level': 'WARNING',
|
||||
'handlers': ['file_handler', 'stream_handler']
|
||||
},
|
||||
'bot': {
|
||||
'level': 'INFO',
|
||||
'handlers': ['file_handler', 'stream_handler']
|
||||
}
|
||||
}
|
||||
}
|
||||
16
bot/modules/decorators.py
Normal file
16
bot/modules/decorators.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from pyrogram import Client
|
||||
from pyrogram.types import Message, CallbackQuery
|
||||
from typing import Union, Callable
|
||||
from functools import wraps
|
||||
from bot.config import Telegram
|
||||
|
||||
def verify_user(func: Callable):
|
||||
|
||||
@wraps(func)
|
||||
async def decorator(client: Client, update: Union[Message, CallbackQuery]):
|
||||
user_id = str(update.from_user.id)
|
||||
|
||||
if not Telegram.ALLOWED_USER_IDS or user_id in Telegram.ALLOWED_USER_IDS:
|
||||
return await func(client, update)
|
||||
|
||||
return decorator
|
||||
48
bot/modules/static.py
Normal file
48
bot/modules/static.py
Normal file
@@ -0,0 +1,48 @@
|
||||
WelcomeText = \
|
||||
"""
|
||||
Hi **%(first_name)s**, send me a file or add me as an admin to any channel to instantly generate file links.
|
||||
|
||||
Add me to your channel to instantly generate links for any downloadable media. Once received, I will automatically attach appropriate buttons to the post containing the URL. If you want me to ignore a given post, you can insert `#pass` in the post.
|
||||
|
||||
- /start to get this message.
|
||||
- /info to get user info.
|
||||
- /log to get bot logs. (admin only!)
|
||||
"""
|
||||
|
||||
FileLinksText = \
|
||||
"""
|
||||
**Download Link:**
|
||||
`%(dl_link)s`
|
||||
**Telegram File:**
|
||||
`%(tg_link)s`
|
||||
"""
|
||||
|
||||
MediaLinksText = \
|
||||
"""
|
||||
**Download Link:**
|
||||
`%(dl_link)s`
|
||||
**Stream Link:**
|
||||
`%(stream_link)s`
|
||||
**Telegram File:**
|
||||
`%(tg_link)s`
|
||||
"""
|
||||
|
||||
InvalidQueryText = \
|
||||
"""
|
||||
Query data mismatched.
|
||||
"""
|
||||
|
||||
MessageNotExist = \
|
||||
"""
|
||||
File revoked or not exist.
|
||||
"""
|
||||
|
||||
LinkRevokedText = \
|
||||
"""
|
||||
The link has been revoked. It may take some time for the changes to take effect.
|
||||
"""
|
||||
|
||||
InvalidPayloadText = \
|
||||
"""
|
||||
Invalid payload.
|
||||
"""
|
||||
56
bot/modules/telegram.py
Normal file
56
bot/modules/telegram.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from pyrogram.types import Message
|
||||
from datetime import datetime
|
||||
from mimetypes import guess_type
|
||||
from bot import TelegramBot
|
||||
from bot.config import Telegram
|
||||
from bot.server.error import abort
|
||||
|
||||
|
||||
async def get_message(message_id: int):
|
||||
message = None
|
||||
|
||||
try:
|
||||
message = await TelegramBot.get_messages(
|
||||
chat_id=Telegram.CHANNEL_ID,
|
||||
message_ids=message_id
|
||||
)
|
||||
if message.empty: message = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return message
|
||||
|
||||
async def get_file_properties(msg: Message):
|
||||
attributes = (
|
||||
'document',
|
||||
'video',
|
||||
'audio',
|
||||
'voice',
|
||||
'photo',
|
||||
'video_note'
|
||||
)
|
||||
|
||||
for attribute in attributes:
|
||||
media = getattr(msg, attribute, None)
|
||||
if media:
|
||||
file_type = attribute
|
||||
break
|
||||
|
||||
if not media: abort(400, 'Unknown file type.')
|
||||
|
||||
file_name = getattr(media, 'file_name', None)
|
||||
|
||||
if not file_name:
|
||||
file_format = {
|
||||
'video': 'mp4',
|
||||
'audio': 'mp3',
|
||||
'voice': 'ogg',
|
||||
'photo': 'jpg',
|
||||
'video_note': 'mp4'
|
||||
}.get(attribute)
|
||||
date = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||
file_name = f'{file_type}-{date}.{file_format}'
|
||||
|
||||
mime_type = guess_type(file_name)[0] or 'application/octet-stream'
|
||||
|
||||
return file_name, mime_type
|
||||
27
bot/plugins/callback.py
Normal file
27
bot/plugins/callback.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from pyrogram.types import CallbackQuery
|
||||
from bot import TelegramBot
|
||||
from bot.modules.static import *
|
||||
from bot.modules.decorators import verify_user
|
||||
from bot.modules.telegram import get_message
|
||||
|
||||
@TelegramBot.on_callback_query()
|
||||
@verify_user
|
||||
async def manage_callback(bot, q: CallbackQuery):
|
||||
query = q.data
|
||||
if query.startswith('rm_'):
|
||||
sq = query.split('_')
|
||||
|
||||
if len(sq) != 3:
|
||||
return await q.answer(InvalidQueryText, show_alert=True)
|
||||
|
||||
message = await get_message(int(sq[1]))
|
||||
|
||||
if not message:
|
||||
return await q.answer(MessageNotExist, show_alert=True)
|
||||
if sq[2] != message.caption:
|
||||
return await q.answer(InvalidQueryText, show_alert=True)
|
||||
|
||||
await message.delete()
|
||||
await q.answer(LinkRevokedText, show_alert=True)
|
||||
else:
|
||||
await q.answer(InvalidQueryText, show_alert=True)
|
||||
45
bot/plugins/commands.py
Normal file
45
bot/plugins/commands.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from pyrogram import filters
|
||||
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
|
||||
from aiofiles import open as async_open
|
||||
from aiofiles.os import remove as async_rm
|
||||
from bot import TelegramBot, logger
|
||||
from bot.config import Telegram
|
||||
from bot.modules.static import *
|
||||
from .deeplinks import deeplinks
|
||||
from bot.modules.decorators import verify_user
|
||||
|
||||
@TelegramBot.on_message(filters.command('start') & filters.private)
|
||||
@verify_user
|
||||
async def start(_, msg: Message):
|
||||
if len(msg.command) != 1:
|
||||
return await deeplinks(msg, msg.command[1])
|
||||
|
||||
await msg.reply(
|
||||
text=WelcomeText % {'first_name': msg.from_user.first_name},
|
||||
quote=True,
|
||||
reply_markup=InlineKeyboardMarkup(
|
||||
[
|
||||
[
|
||||
InlineKeyboardButton('Add to Channel', url=f'https://t.me/{Telegram.BOT_USERNAME}?startchannel&admin=post_messages+edit_messages+delete_messages')
|
||||
]
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
@TelegramBot.on_message(filters.command('info') & filters.private)
|
||||
@verify_user
|
||||
async def user_info(_, msg: Message):
|
||||
await msg.reply(text=f'`{msg.from_user}`', quote=True)
|
||||
|
||||
filename = f'{msg.from_user.id}.json'
|
||||
async with async_open(filename, "w") as file:
|
||||
await file.write(f'{msg.from_user}')
|
||||
|
||||
await msg.reply_document(filename)
|
||||
await async_rm(filename)
|
||||
|
||||
@TelegramBot.on_message(filters.private & filters.command('log') & filters.user(Telegram.OWNER_ID))
|
||||
async def send_log(_, msg: Message):
|
||||
await msg.reply_document('event-log.txt', quote=True)
|
||||
|
||||
logger.info('Bot is now started!')
|
||||
21
bot/plugins/deeplinks.py
Normal file
21
bot/plugins/deeplinks.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from pyrogram.types import Message
|
||||
from bot.modules.static import *
|
||||
from bot.modules.telegram import get_message
|
||||
|
||||
async def deeplinks(msg: Message, payload: str):
|
||||
if payload.startswith('file_'):
|
||||
sp = payload.split('_')
|
||||
|
||||
if len(sp) != 3:
|
||||
return await msg.reply(InvalidPayloadText, quote=True)
|
||||
|
||||
message = await get_message(int(sp[1]))
|
||||
|
||||
if not message:
|
||||
return await msg.reply(MessageNotExist)
|
||||
if sp[2] != message.caption:
|
||||
return await msg.reply(InvalidPayloadText, quote=True)
|
||||
|
||||
await message.copy(chat_id=msg.from_user.id, caption="")
|
||||
else:
|
||||
await msg.reply(InvalidPayloadText, quote=True)
|
||||
126
bot/plugins/files.py
Normal file
126
bot/plugins/files.py
Normal file
@@ -0,0 +1,126 @@
|
||||
from pyrogram import filters, errors
|
||||
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
|
||||
from secrets import token_hex
|
||||
from bot import TelegramBot
|
||||
from bot.config import Telegram, Server
|
||||
from bot.modules.decorators import verify_user
|
||||
from bot.modules.static import *
|
||||
|
||||
@TelegramBot.on_message(
|
||||
filters.private
|
||||
& (
|
||||
filters.document
|
||||
| filters.video
|
||||
| filters.video_note
|
||||
| filters.audio
|
||||
| filters.voice
|
||||
| filters.photo
|
||||
)
|
||||
)
|
||||
@verify_user
|
||||
async def handle_user_file(_, msg: Message):
|
||||
secret_code = token_hex(Telegram.SECRET_CODE_LENGTH)
|
||||
file = await msg.copy(
|
||||
chat_id=Telegram.CHANNEL_ID,
|
||||
caption=f'`{secret_code}`'
|
||||
)
|
||||
file_id = file.id
|
||||
|
||||
dl_link = f'{Server.BASE_URL}/dl/{file_id}?code={secret_code}'
|
||||
tg_link = f'{Server.BASE_URL}/file/{file_id}?code={secret_code}'
|
||||
deep_link = f'https://t.me/{Telegram.BOT_USERNAME}?start=file_{file_id}_{secret_code}'
|
||||
|
||||
if (msg.document and 'video' in msg.document.mime_type) or msg.video:
|
||||
stream_link = f'{Server.BASE_URL}/stream/{file_id}?code={secret_code}'
|
||||
await msg.reply(
|
||||
text=MediaLinksText % {'dl_link': dl_link, 'tg_link': tg_link, 'stream_link': stream_link},
|
||||
quote=True,
|
||||
reply_markup=InlineKeyboardMarkup(
|
||||
[
|
||||
[
|
||||
InlineKeyboardButton('Download', url=dl_link),
|
||||
InlineKeyboardButton('Stream', url=stream_link)
|
||||
],
|
||||
[
|
||||
InlineKeyboardButton('Get File', url=deep_link),
|
||||
InlineKeyboardButton('Revoke', callback_data=f'rm_{file_id}_{secret_code}')
|
||||
]
|
||||
]
|
||||
)
|
||||
)
|
||||
else:
|
||||
await msg.reply(
|
||||
text=FileLinksText % {'dl_link': dl_link, 'tg_link': tg_link},
|
||||
quote=True,
|
||||
reply_markup=InlineKeyboardMarkup(
|
||||
[
|
||||
[
|
||||
InlineKeyboardButton('Download', url=dl_link),
|
||||
InlineKeyboardButton('Get File', url=deep_link)
|
||||
],
|
||||
[
|
||||
InlineKeyboardButton('Revoke', callback_data=f'rm_{file_id}_{secret_code}')
|
||||
]
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
@TelegramBot.on_message(
|
||||
filters.channel
|
||||
& ~filters.forwarded
|
||||
& ~filters.media_group
|
||||
& (
|
||||
filters.document
|
||||
| filters.video
|
||||
| filters.video_note
|
||||
| filters.audio
|
||||
| filters.voice
|
||||
| filters.photo
|
||||
)
|
||||
)
|
||||
@verify_user
|
||||
async def handle_channel_file(_, msg: Message):
|
||||
if msg.caption and '#pass' in msg.caption:
|
||||
return
|
||||
|
||||
secret_code = token_hex(Telegram.SECRET_CODE_LENGTH)
|
||||
|
||||
try:
|
||||
file = await msg.copy(
|
||||
chat_id=Telegram.CHANNEL_ID,
|
||||
caption=f'`{secret_code}`'
|
||||
)
|
||||
except (errors.ChatForwardsRestricted, errors.MessageIdInvalid, errors.ChannelPrivate):
|
||||
return
|
||||
|
||||
file_id = file.id
|
||||
|
||||
dl_link = f'{Server.BASE_URL}/dl/{file_id}?code={secret_code}'
|
||||
tg_link = f'{Server.BASE_URL}/file/{file_id}?code={secret_code}'
|
||||
|
||||
if (msg.document and 'video' in msg.document.mime_type) or msg.video:
|
||||
stream_link = f'{Server.BASE_URL}/stream/{file_id}?code={secret_code}'
|
||||
await msg.edit_reply_markup(
|
||||
InlineKeyboardMarkup(
|
||||
[
|
||||
[
|
||||
InlineKeyboardButton('Download', url=dl_link),
|
||||
InlineKeyboardButton('Stream', url=stream_link)
|
||||
],
|
||||
[
|
||||
InlineKeyboardButton('Get File', url=tg_link)
|
||||
]
|
||||
]
|
||||
)
|
||||
)
|
||||
else:
|
||||
await msg.edit_reply_markup(
|
||||
InlineKeyboardMarkup(
|
||||
[
|
||||
[
|
||||
InlineKeyboardButton('Download', url=dl_link),
|
||||
InlineKeyboardButton('Get File', url=tg_link)
|
||||
]
|
||||
]
|
||||
)
|
||||
)
|
||||
30
bot/server/__init__.py
Normal file
30
bot/server/__init__.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from quart import Quart
|
||||
from uvicorn import Config, Server as UvicornServer
|
||||
from logging import getLogger
|
||||
from bot.config import Server, LOGGER_CONFIG_JSON
|
||||
|
||||
from . import main, error
|
||||
|
||||
logger = getLogger('uvicorn')
|
||||
instance = Quart(__name__)
|
||||
|
||||
@instance.before_serving
|
||||
async def before_serve():
|
||||
logger.info('Web server is started!')
|
||||
logger.info(f'Server running on {Server.BIND_ADDRESS}:{Server.PORT}')
|
||||
|
||||
instance.register_blueprint(main.bp)
|
||||
|
||||
instance.register_error_handler(400, error.invalid_request)
|
||||
instance.register_error_handler(404, error.not_found)
|
||||
instance.register_error_handler(405, error.invalid_method)
|
||||
instance.register_error_handler(error.HTTPError, error.http_error)
|
||||
|
||||
server = UvicornServer(
|
||||
Config(
|
||||
app = instance,
|
||||
host = Server.BIND_ADDRESS,
|
||||
port = Server.PORT,
|
||||
log_config = LOGGER_CONFIG_JSON
|
||||
)
|
||||
)
|
||||
31
bot/server/error.py
Normal file
31
bot/server/error.py
Normal file
@@ -0,0 +1,31 @@
|
||||
class HTTPError(Exception):
|
||||
status_code:int = None
|
||||
description:str = None
|
||||
def __init__(self, status_code, description):
|
||||
self.status_code = status_code
|
||||
self.description = description
|
||||
super().__init__(self.status_code, self.description)
|
||||
|
||||
error_messages = {
|
||||
400: 'Invalid request.',
|
||||
401: 'File code is required to download the file.',
|
||||
403: 'Invalid file code.',
|
||||
404: 'File not found.',
|
||||
500: 'Internal server error.'
|
||||
}
|
||||
|
||||
async def invalid_request(_):
|
||||
return 'Invalid request.', 400
|
||||
|
||||
async def not_found(_):
|
||||
return 'Resource not found.', 404
|
||||
|
||||
async def invalid_method(_):
|
||||
return 'Invalid request method.', 405
|
||||
|
||||
async def http_error(error: HTTPError):
|
||||
error_message = error_messages.get(error.status_code)
|
||||
return error.description or error_message, error.status_code
|
||||
|
||||
def abort(status_code: int = 500, description: str = None):
|
||||
raise HTTPError(status_code, description)
|
||||
41
bot/server/main.py
Normal file
41
bot/server/main.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from quart import Blueprint, Response, request, render_template, redirect
|
||||
from .error import abort
|
||||
from bot import version, TelegramBot
|
||||
from bot.config import Telegram, Server
|
||||
from bot.modules.telegram import get_message, get_file_properties
|
||||
|
||||
bp = Blueprint('main', __name__)
|
||||
|
||||
@bp.route('/')
|
||||
async def home():
|
||||
return redirect(f'https://t.me/{Telegram.BOT_USERNAME}')
|
||||
|
||||
@bp.route('/dl/<int:file_id>')
|
||||
async def transmit_file(file_id):
|
||||
file = await get_message(int(file_id)) or abort(404)
|
||||
code = request.args.get('code') or abort(401)
|
||||
|
||||
if code != file.caption:
|
||||
abort(403)
|
||||
|
||||
file_name, mime_type = await get_file_properties(file)
|
||||
headers = {
|
||||
'Content-Type': mime_type,
|
||||
'Content-Disposition': f'attachment; filename="{file_name}"'
|
||||
}
|
||||
|
||||
file_stream = TelegramBot.stream_media(file)
|
||||
|
||||
return Response(file_stream, headers=headers)
|
||||
|
||||
@bp.route('/stream/<int:file_id>')
|
||||
async def stream_file(file_id):
|
||||
code = request.args.get('code') or abort(401)
|
||||
|
||||
return await render_template('player.html', mediaLink=f'{Server.BASE_URL}/dl/{file_id}?code={code}')
|
||||
|
||||
@bp.route('/file/<int:file_id>')
|
||||
async def file_deeplink(file_id):
|
||||
code = request.args.get('code') or abort(401)
|
||||
|
||||
return redirect(f'https://t.me/{Telegram.BOT_USERNAME}?start=file_{file_id}_{code}')
|
||||
155
bot/server/templates/player.html
Normal file
155
bot/server/templates/player.html
Normal file
@@ -0,0 +1,155 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Play Files</title>
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-Frame-Options" content="deny">
|
||||
|
||||
<link rel="stylesheet" href="https://cdn.plyr.io/3.7.8/plyr.css" />
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
|
||||
|
||||
<script src="https://cdn.plyr.io/3.7.8/plyr.polyfilled.js"></script>
|
||||
|
||||
<style>
|
||||
html, body {
|
||||
margin: 0;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
#stream-media {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
#error-message {
|
||||
color: red;
|
||||
font-size: 24px;
|
||||
text-align: center;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.plyr__video-wrapper .plyr-download-button{
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
left: 10px;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
background-color: rgba(0, 0, 0, 0.7);
|
||||
border-radius: 50%;
|
||||
text-align: center;
|
||||
line-height: 30px;
|
||||
color: white;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.plyr__volume {
|
||||
max-width: initial;
|
||||
min-width: initial;
|
||||
width: auto;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
|
||||
.plyr__video-wrapper .plyr-share-button{
|
||||
position: absolute;
|
||||
top: 50px;
|
||||
left: 10px;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
background-color: rgba(0, 0, 0, 0.7);
|
||||
border-radius: 50%;
|
||||
text-align: center;
|
||||
line-height: 30px;
|
||||
color: white;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.plyr__video-wrapper .plyr-download-button:hover,
|
||||
.plyr__video-wrapper .plyr-share-button:hover{
|
||||
background-color: rgba(255, 255, 255, 0.7);
|
||||
color: black;
|
||||
}
|
||||
|
||||
.plyr__video-wrapper .plyr-download-button:before {
|
||||
font-family: "Font Awesome 5 Free";
|
||||
content: "\f019";
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.plyr__video-wrapper .plyr-share-button:before {
|
||||
font-family: "Font Awesome 5 Free";
|
||||
content: "\f064";
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.plyr, .plyr__video-wrapper, .plyr__video-embed iframe {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<video id="stream-media" controls preload="auto">
|
||||
<source src="" type="">
|
||||
<p class="vjs-no-js">
|
||||
To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video
|
||||
</p>
|
||||
</video>
|
||||
|
||||
<div id="error-message"></div>
|
||||
|
||||
<script>
|
||||
var player = new Plyr('#stream-media', {
|
||||
controls:['play-large', 'rewind', 'play', 'fast-forward', 'progress', 'current-time', 'mute', 'settings', 'pip', 'fullscreen'],
|
||||
settings:['speed','loop'],
|
||||
speed:{selected:1,options:[0.25,0.5,0.75,1,1.25,1.5,1.75,2]},
|
||||
seek: 10,
|
||||
keyboard: { focused: true, global: true },
|
||||
});
|
||||
|
||||
var mediaLink = "{{ mediaLink }}";
|
||||
|
||||
if (mediaLink) {
|
||||
document.querySelector('#stream-media source').setAttribute('src', mediaLink);
|
||||
player.restart();
|
||||
|
||||
var downloadButton = document.createElement('div');
|
||||
downloadButton.className = 'plyr-download-button';
|
||||
|
||||
downloadButton.onclick = function() {
|
||||
event.stopPropagation();
|
||||
var link = document.createElement('a');
|
||||
link.href = mediaLink;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
};
|
||||
|
||||
player.elements.container.querySelector('.plyr__video-wrapper').appendChild(downloadButton);
|
||||
|
||||
var shareButton = document.createElement('div');
|
||||
shareButton.className = 'plyr-share-button';
|
||||
|
||||
shareButton.onclick = function() {
|
||||
event.stopPropagation();
|
||||
if (navigator.share) {
|
||||
navigator.share({
|
||||
title: "Play",
|
||||
url: window.location.href
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
player.elements.container.querySelector('.plyr__video-wrapper').appendChild(shareButton);
|
||||
|
||||
} else {
|
||||
document.getElementById('error-message').textContent = 'Error: Media URL not provided';
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
5
requirements.txt
Normal file
5
requirements.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
pyrogram
|
||||
tgcrypto
|
||||
quart
|
||||
uvicorn
|
||||
aiofiles
|
||||
1
runtime.txt
Normal file
1
runtime.txt
Normal file
@@ -0,0 +1 @@
|
||||
python-3.11.6
|
||||
Reference in New Issue
Block a user