Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- from contextlib import ExitStack
- from pathlib import Path
- from telegram import InputFile, InputMediaPhoto, InputMediaVideo, Update
- from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
- logging.basicConfig(
- format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
- level=logging.DEBUG,
- )
- TOKEN = "TOKEN"
- async def test_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
- if not update.effective_message:
- raise RuntimeError
- image1, video1 = files = (
- Path("./files/image1.jpg").resolve(),
- Path("./files/video1.mp4").resolve(),
- )
- for file in files:
- if not file.exists():
- raise RuntimeError
- image1, video1 = files
- try:
- with ExitStack() as stack:
- image1_input = InputFile(stack.enter_context(image1.open("rb")), filename="image1.jpg", read_file_handle=False)
- video1_input = InputFile(stack.enter_context(video1.open("rb")), filename="video1.mp4", read_file_handle=False)
- media_group = [
- InputMediaPhoto(media=image1_input),
- InputMediaVideo(media=video1_input),
- ]
- logging.info("Attempting to send media group...")
- await update.effective_message.reply_media_group(media=media_group)
- except Exception as exception:
- logging.error("Failed to send media group: %s", exception)
- await update.effective_message.reply_text(f"Caught exception: {exception}")
- if __name__ == "__main__":
- application = ApplicationBuilder().token(TOKEN).build()
- application.add_handler(CommandHandler("test", test_command))
- logging.info("Bot is polling. Send /test in Telegram.")
- application.run_polling()
Advertisement
Add Comment
Please, Sign In to add comment