如何从Telebot上的本地存储中发送媒体专辑?

发布于 2025-02-07 01:23:59 字数 812 浏览 2 评论 0原文

我正在尝试制作一个电报机器人,该机器人从本地存储中发送媒体,我遇到了这个错误。

另外,如果其中有10个项目的列表,并且您尝试发送,因为专辑会自动将它们分离到其他专辑中以发送它吗?

对电报API的请求不成功。错误代码:400。描述:不良请求:错误的HTTP URL指定

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2025-02-14 01:23:59

它不能直接使用到本地文件的路径。
您必须将内容发送为字节:

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

    vid_media.append(media)

完整的工作代码:

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

编辑:

不同的模块可能具有不同的功能。

此代码使用模块telebot pytelegrambotapi 路径
但是似乎模块电报可以使用pathlib.path在其 inputMediaVideo

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文