0

I'm trying to make a telegram bot that sends media from local storage and i got this error.

Also if there is a list with over 10 items on it and you try to send as a album does telegram automatically seperates them to different album to send it?

A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: wrong HTTP URL specified

import telebot
import glob
import os
from telebot.types import InputMediaPhoto, InputMediaVideo

bot = telebot.TeleBot("")

@bot.message_handler(commands=['test'])
def test(message):
    id = message.chat.id
    path = "./vid/*.mp4"
    vid_media = []
    #i might remove this i think this is not needed 
    for files in glob.glob(path):
        print(files)

    for i in os.listdir("./vid/"):
        vid_media.append(InputMediaVideo(i))
    bot.send_message(id, "Sending videos...")
    for i in vid_media:
        with open(i, 'rb') as f:
            bot.send_media_group(id, vid_media)
bot.polling()
Steven Kanberg
  • 6,078
  • 2
  • 16
  • 35
ciumai
  • 21
  • 1
  • 6
  • 1
    always put full error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information. – furas Jun 11 '22 at 12:30
  • I don't understand why you `open()` file if you don't use it but you try to send `vid_media`> And you run it in loop `for i in vid_media` - so you try to send the same `vid_media` many times. – furas Jun 11 '22 at 12:32
  • I don't understand why you use `listdir()` if you can use values from `glob()` - `for i in files:` – furas Jun 11 '22 at 12:34

1 Answers1

1

It can't use directly path to local file. You have to send content as bytes:

with open(filename, 'rb') as fh:   # open in `byte mode`
    data = fh.read()               # read bytes
    media = InputMediaVideo(data)  # put bytes

    vid_media.append(media)

Full working code:

import os
import glob
import telebot
from telebot.types import InputMediaPhoto, InputMediaVideo

TOKEN = os.getenv('TELEGRAM_TOKEN')
#print('TOKEN:', TOKEN)

bot = telebot.TeleBot(TOKEN)

@bot.message_handler(commands=['test'])
def test(message):
    chat_id = message.chat.id
    
    path = "./vid/*.mp4"
    
    vid_media = []
    
    for filename in glob.glob(path):
        print('read:', filename)
        with open(filename, 'rb') as fh:
            data = fh.read()
            media = InputMediaVideo(data)
            vid_media.append(media)
        
    bot.send_message(chat_id, "Sending videos...")
    
    bot.send_media_group(chat_id, vid_media)
            
bot.polling()

EDIT:

Different modules may have different functionalities.

This code uses module telebot (pyTelegramBotAPI) and it can't use local path
but it seems module telegram can use pathlib.Path with local path in its InputMediaVideo.

furas
  • 134,197
  • 12
  • 106
  • 148