The telegram bot does not send a message

44 Views Asked by At
from pyrogram import Client
from pytube import YouTube
import os
import asyncio

app = Client("qaserd_ll", api_id=..., api_hash='...', bot_token='...')

@app.on_message()
async def send_video(client, message):
    URL = message.text
    yt = YouTube(URL)
    ys = yt.streams.get_highest_resolution()
    video_path = 'video.mp4'
    try:
        ys.download(filename='video.mp4')  # Download the video using the specified filename
        print("Video downloaded successfully")
    except Exception as e:
        print(f"An error occurred while downloading the video: {e}")
        return
    attempts = 0
    while not os.path.exists('video.mp4') and attempts < 10:  # Wait for the video to be downloaded
        await asyncio.sleep(1)
        attempts += 1
    if attempts == 10:
        print("Video download timed out")
        return
    duration = yt.length
    print(duration)
    try:
        await client.send_video(message.chat.id, video_path, duration=duration)
        print("Video sent successfully")
    except Exception as e:
        print(f"An error occurred while sending the video: {e}")

app.run()

the video is downloaded, the time is also displayed correctly, but the last lines of the code are not executed. the bot token, api_id, api_hash is written correctly

1

There are 1 best solutions below

0
shadow On

sample video

What's your file size ? It took me about 20s to send a 1.5 mb video I downloaded from the link above. This was my MRE and it worked -

from pyrogram import Client
import os
import asyncio

app = Client("qaserd_ll", api_id=123, api_hash='123', bot_token='123')

@app.on_message()
async def send_video(client, message):

    try:
        x= await client.send_video(message.chat.id, "file_example_MP4_480_1_5MG.mp4", duration=39)
        print("Video sent successfully")
    except Exception as e:
        print(f"An error occurred while sending the video: {e}")

app.run()

Use import traceback and put traceback.print_exc()in the except block . Update me about the result .