Search code examples
pythontelegramtelegram-bottelebot

How can i send many videos from list in telebot python


So I was writing a python script using telebot and got an error

A request to the Telegram API was unsuccessful. Error code: 400. 
Description: Bad Request: file must be non-empty

I have tried different methods from many forums, but nothing helps

import telebot

import random

import time

token = #token here

bot = telebot.TeleBot(token)

shit = ["C:\\Users\\glebc\\Documents\\source(bot)\\3wZ3.gif.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\65216814_456719028224290_7745639790787166208_n.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\doc_2022-03-10_16-41-49.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\doc_2022-03-10_16-42-04.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\doc_2022-03-10_16-42-39.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\giphy.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_0080.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_0835.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_1362.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_4698.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_4962.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_6359.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_7497.MOV", "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_7909.MOV", "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_9540.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\mp4.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\video.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\комочек тьмы.mp4", "C:\\Users\\glebc\\Documents\\source(bot)\\кот.mp4"]

video = open(shit[random.randint(0, len(shit)-1)], 'rb')

@bot.message_handler(commands=['start'])

def start_message(message):

    bot.send_message(message.chat.id, 'hello message 1')

@bot.message_handler(commands=['haha'])

def haha_message(message):

    while True:

            bot.send_video(message.chat.id, vidos)

            time.sleep(3600) #1 hour

@bot.message_handler(commands=['hehe'])

def shit_message(message):

bot.send_video(message.chat.id, vidos)   

bot.infinity_polling()

Also i dont understand error cause i dont close file only open


Solution

  • Problem can be because you open file only once and you never close it and open again.

    When it reads then it move special pointer which shows where to read next time. When it reads to the end of file then this pointer is moved to the end of file and when it tries to read again then it trires to read from the end of file and there is nothing to read and it may say that you have empty file.

    After reading you may have to use vidoe.seek(0) to move pointer to the beginnig of file.

    Or you should close and open it again. And this can be even more useful because at this moment you select random file only once and later it would use always the same path. You should use random inside loop.

    @bot.message_handler(commands=['haha'])
    def haha_message(message):
    
        while True:
    
            video = open( random.choice(shit), 'rb')
        
            bot.send_video(message.chat.id, video)
        
            video.close()
            
            time.sleep(3600)  # 1 hour
    

    and the same in other functions

    @bot.message_handler(commands=['hehe'])
    def shit_message(message):
    
        video = open( random.choice(shit), 'rb')
    
        bot.send_video(message.chat.id, video)   
    
        video.close()
    

    BTW:

    telegram may has some methods to execute tasks periodically.

    For example module python-telegram-bot has telegram.ext.jobqueue for this.


    Full working code

    For tests I set logging.DEBUG to see all error messages.
    Normally telebot catchs all errors and hides them.

    I also used with open() as video so it automatically closes file.

    import os
    import random
    import logging
    import telebot
    
    # display errors 
    telebot.logger.setLevel(logging.DEBUG) 
    
    TOKEN = os.getenv('TELEGRAM_TOKEN')
    
    bot = telebot.TeleBot(TOKEN)
    
    all_videos = [
        "C:\\Users\\glebc\\Documents\\source(bot)\\3wZ3.gif.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\65216814_456719028224290_7745639790787166208_n.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\doc_2022-03-10_16-41-49.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\doc_2022-03-10_16-42-04.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\doc_2022-03-10_16-42-39.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\giphy.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_0080.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_0835.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_1362.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_4698.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_4962.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_6359.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_7497.MOV",
        "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_7909.MOV",
        "C:\\Users\\glebc\\Documents\\source(bot)\\IMG_9540.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\mp4.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\video.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\комочек тьмы.mp4",
        "C:\\Users\\glebc\\Documents\\source(bot)\\кот.mp4"
    ]
    
    @bot.message_handler(commands=['start'])
    def start_message(message):
        bot.send_message(message.chat.id, 'hello message 1')
    
    @bot.message_handler(commands=['haha'])
    def haha_message(message):
        while True:
            with open(random.choice(all_videos), 'rb') as video:
                bot.send_video(message.chat.id, video)
            time.sleep(3600)  # 1 hour
    
    @bot.message_handler(commands=['hehe'])
    def shit_message(message):
        with open(random.choice(all_videos), 'rb') as video:
            bot.send_video(message.chat.id, video)   
    
    bot.infinity_polling()