S
S
Sergey Afanasiev2020-06-29 16:54:34
Python
Sergey Afanasiev, 2020-06-29 16:54:34

Why is the bot not sending video in telegram?

I want to make a bot that sends a video to the user via a link from YouTube, but for some reason the bot is "silent" at first and then gives an error: A request to the Telegram API was unsuccessful. The server returned HTTP 504 Gateway Time-out
Maybe someone knows what to do? Bot code below

import requests
from bs4 import BeautifulSoup as BS
import telebot
import config
import pafy

bot = telebot.TeleBot(config.token)
@bot.message_handler(commands=['fantastic'])
def fantasic_message(message):
    v = pafy.new("https://www.youtube.com/watch?v=COwlqqErDbY")
    s = v.getbest()
    bot.send_message(message.chat.id, "Size is %s" % s.get_filesize())
    filename = s.download()
    bot.send_video(message.chat.id, filename)

if __name__ == '__main__':
    bot.infinity_polling()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-06-30
@keksss17

You, my friend, went the wrong way. You saved up and pasted the code, but did not understand it. I haven't worked with the pafy library, but I googled something in a couple of minutes.
The first thing I did was check what is stored in the "filename" variable. And there it turned out to be None. Further you send to the user again, None. Of course, the Telegram server did not like this and returned 504 to you
. Here I sketched it in a couple of minutes. The code is working, but there are a lot of crutches:

bot = telebot.TeleBot(config.token)
@bot.message_handler(commands=['fantastic'])
def fantasic_message(message):
    v = pafy.new("https://www.youtube.com/watch?v=COwlqqErDbY")
    s = v.getbest()
    bot.send_message(message.chat.id, "Size is %s" % s.get_filesize())

    videoid = v.videoid # Получаем id видео
    ext = s.extension # Получаем расширение видео
    filename = f'{videoid}.{ext}' # Формируем имя файла на основе id + расширения
    s.download(filename) # Скачиваем видео и сохраняем с именем videoid+расширение

    data = open(filename,'rb') 
    bot.send_video(message.chat.id, data)
    data.close()

if __name__ == '__main__':
    bot.infinity_polling()

PS: For the file name, I chose not the name of the video, but videoid (any random value is possible), because the video name may contain "bad" characters that drivers may not like (NTFS, ext4, etc)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question