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()
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.