update v0.1
This commit is contained in:
@@ -1,41 +1,59 @@
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from telegram import Message
|
||||
from telegram.constants import ParseMode
|
||||
from telegram.error import TelegramError
|
||||
import logging
|
||||
|
||||
async def send_video(message: Message, filepath: Path, connect_timeout: int, read_timeout: int):
|
||||
with filepath.open('rb') as f:
|
||||
await message.chat.send_video(
|
||||
video=f,
|
||||
supports_streaming=True,
|
||||
connect_timeout=connect_timeout,
|
||||
read_timeout=read_timeout
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
async def send_audio(message: Message, filepath: Path, title: str | None, connect_timeout: int, read_timeout: int):
|
||||
with filepath.open('rb') as f:
|
||||
await message.chat.send_audio(
|
||||
audio=f,
|
||||
title=title,
|
||||
connect_timeout=connect_timeout,
|
||||
read_timeout=read_timeout
|
||||
)
|
||||
|
||||
async def send_document(message: Message, filepath: Path, connect_timeout: int, read_timeout: int):
|
||||
with filepath.open('rb') as f:
|
||||
await message.chat.send_document(
|
||||
document=f,
|
||||
filename=filepath.name,
|
||||
connect_timeout=connect_timeout,
|
||||
read_timeout=read_timeout
|
||||
)
|
||||
|
||||
async def send_photo(message: Message, filepath: Path, connect_timeout: int, read_timeout: int):
|
||||
# Фото до ~20MB лучше отправлять как фото, большие — как документ
|
||||
if filepath.stat().st_size <= 20 * 1024 * 1024:
|
||||
with filepath.open('rb') as f:
|
||||
await message.chat.send_photo(
|
||||
photo=f,
|
||||
connect_timeout=connect_timeout,
|
||||
read_timeout=read_timeout
|
||||
async def send_video(message: Message, filepath: Path, title: Optional[str] = None, *args, **kwargs):
|
||||
"""Универсальная функция - принимает любые аргументы"""
|
||||
try:
|
||||
with open(filepath, 'rb') as video_file:
|
||||
await message.reply_video(
|
||||
video=video_file,
|
||||
caption=title or "Видео",
|
||||
supports_streaming=True,
|
||||
parse_mode=ParseMode.HTML,
|
||||
**kwargs # ✅ принимает любые доп. аргументы
|
||||
)
|
||||
else:
|
||||
await send_document(message, filepath, connect_timeout, read_timeout)
|
||||
except TelegramError as e:
|
||||
logger.error(f"Ошибка отправки видео: {e}")
|
||||
await message.reply_text("❌ Ошибка при отправке видео")
|
||||
|
||||
async def send_audio(message: Message, filepath: Path, title: Optional[str] = None, *args, **kwargs):
|
||||
try:
|
||||
with open(filepath, 'rb') as audio_file:
|
||||
await message.reply_audio(
|
||||
audio=audio_file,
|
||||
title=title or "Аудио",
|
||||
parse_mode=ParseMode.HTML,
|
||||
**kwargs
|
||||
)
|
||||
except TelegramError as e:
|
||||
logger.error(f"Ошибка отправки аудио: {e}")
|
||||
await message.reply_text("❌ Ошибка при отправке аудио")
|
||||
|
||||
async def send_document(message: Message, filepath: Path, *args, **kwargs):
|
||||
try:
|
||||
with open(filepath, 'rb') as doc_file:
|
||||
await message.reply_document(
|
||||
document=doc_file,
|
||||
**kwargs
|
||||
)
|
||||
except TelegramError as e:
|
||||
logger.error(f"Ошибка отправки документа: {e}")
|
||||
await message.reply_text("❌ Ошибка при отправке документа")
|
||||
|
||||
async def send_photo(message: Message, filepath: Path, *args, **kwargs):
|
||||
try:
|
||||
with open(filepath, 'rb') as photo_file:
|
||||
await message.reply_photo(
|
||||
photo=photo_file,
|
||||
**kwargs
|
||||
)
|
||||
except TelegramError as e:
|
||||
logger.error(f"Ошибка отправки фото: {e}")
|
||||
await message.reply_text("❌ Ошибка при отправке фото")
|
||||
|
||||
Reference in New Issue
Block a user