Перейти к содержимому

Обработка медиа

EdgeBot поддерживает все основные типы медиа Telegram. Для каждого типа есть декоратор-обработчик и метод отправки в Context.

@bot.on_photo
async def handle_photo(ctx: Context):
# ctx.photo — список объектов PhotoSize (от маленького к большому)
best = ctx.photo[-1]
file_id = best["file_id"]
width = best.get("width", 0)
height = best.get("height", 0)
await ctx.send_photo(photo=file_id, caption=f"📸 {width}×{height}")
@bot.on_video
async def handle_video(ctx: Context):
file_id = ctx.video["file_id"]
duration = ctx.video.get("duration", 0)
await ctx.send_video(video=file_id, caption=f"🎥 {duration}с")
@bot.on_voice
async def handle_voice(ctx: Context):
file_id = ctx.voice["file_id"]
duration = ctx.voice.get("duration", 0)
await ctx.send_voice(voice=file_id, caption=f"🎤 {duration}с")
@bot.on_audio
async def handle_audio(ctx: Context):
file_id = ctx.audio["file_id"]
title = ctx.audio.get("title", "")
performer = ctx.audio.get("performer", "")
await ctx.send_audio(
audio=file_id,
caption="🎵 Аудио",
title=title,
performer=performer,
)
@bot.on_sticker
async def handle_sticker(ctx: Context):
file_id = ctx.sticker["file_id"]
await ctx.send_sticker(sticker=file_id)
@bot.on_animation
async def handle_animation(ctx: Context):
file_id = ctx.animation["file_id"]
await ctx.send_animation(animation=file_id, caption="🎬 GIF")
@bot.on_document
async def handle_document(ctx: Context):
file_id = ctx.document["file_id"]
file_name = ctx.document.get("file_name", "")
await ctx.send_document(document=file_id, caption=f"📄 {file_name}")

Все методы отправки медиа доступны в Context и принимают:

МетодАргументы
ctx.send_photophoto, caption, reply_markup, parse_mode
ctx.send_videovideo, caption, reply_markup, parse_mode
ctx.send_voicevoice, caption, reply_markup, parse_mode
ctx.send_audioaudio, caption, reply_markup, parse_mode, title, performer
ctx.send_stickersticker, reply_markup
ctx.send_animationanimation, caption, reply_markup, parse_mode
ctx.send_documentdocument, caption, reply_markup, parse_mode

Первый аргумент — file_id (полученный от Telegram) или URL файла.

from edgebot import InlineKeyboard
@bot.on_photo
async def photo_with_buttons(ctx: Context):
kb = InlineKeyboard()
kb.button("👍 Нравится", callback_data="like")
kb.button("👎 Не нравится", callback_data="dislike")
file_id = ctx.photo[-1]["file_id"]
await ctx.send_photo(
photo=file_id,
caption="Оцените фото:",
reply_markup=kb.build(),
)