我如何从 telebot python 中的列表中发送许多视频
How can i send many videos from list in telebot python
所以我正在使用 telebot 编写 python 脚本并遇到错误
A request to the Telegram API was unsuccessful. Error code: 400.
Description: Bad Request: file must be non-empty
我尝试了很多论坛的不同方法,但没有任何帮助
import telebot
import random
import time
token = #token here
bot = telebot.TeleBot(token)
shit = ["C:\Users\glebc\Documents\source(bot)\3wZ3.gif.mp4", "C:\Users\glebc\Documents\source(bot)\65216814_456719028224290_7745639790787166208_n.mp4", "C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-41-49.mp4", "C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-42-04.mp4", "C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-42-39.mp4", "C:\Users\glebc\Documents\source(bot)\giphy.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_0080.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_0835.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_1362.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_4698.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_4962.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_6359.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_7497.MOV", "C:\Users\glebc\Documents\source(bot)\IMG_7909.MOV", "C:\Users\glebc\Documents\source(bot)\IMG_9540.mp4", "C:\Users\glebc\Documents\source(bot)\mp4.mp4", "C:\Users\glebc\Documents\source(bot)\video.mp4", "C:\Users\glebc\Documents\source(bot)\комочек тьмы.mp4", "C:\Users\glebc\Documents\source(bot)\кот.mp4"]
video = open(shit[random.randint(0, len(shit)-1)], 'rb')
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'hello message 1')
@bot.message_handler(commands=['haha'])
def haha_message(message):
while True:
bot.send_video(message.chat.id, vidos)
time.sleep(3600) #1 hour
@bot.message_handler(commands=['hehe'])
def shit_message(message):
bot.send_video(message.chat.id, vidos)
bot.infinity_polling()
我也不明白错误是因为我不关闭文件只打开
问题可能是因为您只打开文件一次,而您永远不会关闭它并再次打开。
当它读取时,它会移动特殊指针,指示下次读取的位置。当它读取到文件末尾时,该指针将移至文件末尾,当它尝试再次读取时,它会尝试从文件末尾读取,但没有可读取的内容,它可能会说您有空文件.
阅读后您可能需要使用 vidoe.seek(0)
将指针移动到文件的开头。
或者你应该 close
和 open
再一次。这可能会更有用,因为此时您 select 随机文件只有一次,以后它将始终使用相同的路径。您应该在循环中使用 random
。
@bot.message_handler(commands=['haha'])
def haha_message(message):
while True:
video = open( random.choice(shit), 'rb')
bot.send_video(message.chat.id, video)
video.close()
time.sleep(3600) # 1 hour
其他功能同理
@bot.message_handler(commands=['hehe'])
def shit_message(message):
video = open( random.choice(shit), 'rb')
bot.send_video(message.chat.id, video)
video.close()
顺便说一句:
telegram
可能有一些定时执行任务的方法
例如模块 python-telegram-bot
为此有 telegram.ext.jobqueue。
完整的工作代码
对于测试,我设置 logging.DEBUG
以查看所有错误消息。
通常 telebot
捕获所有错误并隐藏它们。
我也用过with open() as video
所以它会自动关闭文件。
import os
import random
import logging
import telebot
# display errors
telebot.logger.setLevel(logging.DEBUG)
TOKEN = os.getenv('TELEGRAM_TOKEN')
bot = telebot.TeleBot(TOKEN)
all_videos = [
"C:\Users\glebc\Documents\source(bot)\3wZ3.gif.mp4",
"C:\Users\glebc\Documents\source(bot)\65216814_456719028224290_7745639790787166208_n.mp4",
"C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-41-49.mp4",
"C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-42-04.mp4",
"C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-42-39.mp4",
"C:\Users\glebc\Documents\source(bot)\giphy.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_0080.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_0835.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_1362.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_4698.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_4962.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_6359.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_7497.MOV",
"C:\Users\glebc\Documents\source(bot)\IMG_7909.MOV",
"C:\Users\glebc\Documents\source(bot)\IMG_9540.mp4",
"C:\Users\glebc\Documents\source(bot)\mp4.mp4",
"C:\Users\glebc\Documents\source(bot)\video.mp4",
"C:\Users\glebc\Documents\source(bot)\комочек тьмы.mp4",
"C:\Users\glebc\Documents\source(bot)\кот.mp4"
]
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'hello message 1')
@bot.message_handler(commands=['haha'])
def haha_message(message):
while True:
with open(random.choice(all_videos), 'rb') as video:
bot.send_video(message.chat.id, video)
time.sleep(3600) # 1 hour
@bot.message_handler(commands=['hehe'])
def shit_message(message):
with open(random.choice(all_videos), 'rb') as video:
bot.send_video(message.chat.id, video)
bot.infinity_polling()
所以我正在使用 telebot 编写 python 脚本并遇到错误
A request to the Telegram API was unsuccessful. Error code: 400.
Description: Bad Request: file must be non-empty
我尝试了很多论坛的不同方法,但没有任何帮助
import telebot
import random
import time
token = #token here
bot = telebot.TeleBot(token)
shit = ["C:\Users\glebc\Documents\source(bot)\3wZ3.gif.mp4", "C:\Users\glebc\Documents\source(bot)\65216814_456719028224290_7745639790787166208_n.mp4", "C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-41-49.mp4", "C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-42-04.mp4", "C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-42-39.mp4", "C:\Users\glebc\Documents\source(bot)\giphy.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_0080.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_0835.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_1362.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_4698.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_4962.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_6359.mp4", "C:\Users\glebc\Documents\source(bot)\IMG_7497.MOV", "C:\Users\glebc\Documents\source(bot)\IMG_7909.MOV", "C:\Users\glebc\Documents\source(bot)\IMG_9540.mp4", "C:\Users\glebc\Documents\source(bot)\mp4.mp4", "C:\Users\glebc\Documents\source(bot)\video.mp4", "C:\Users\glebc\Documents\source(bot)\комочек тьмы.mp4", "C:\Users\glebc\Documents\source(bot)\кот.mp4"]
video = open(shit[random.randint(0, len(shit)-1)], 'rb')
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'hello message 1')
@bot.message_handler(commands=['haha'])
def haha_message(message):
while True:
bot.send_video(message.chat.id, vidos)
time.sleep(3600) #1 hour
@bot.message_handler(commands=['hehe'])
def shit_message(message):
bot.send_video(message.chat.id, vidos)
bot.infinity_polling()
我也不明白错误是因为我不关闭文件只打开
问题可能是因为您只打开文件一次,而您永远不会关闭它并再次打开。
当它读取时,它会移动特殊指针,指示下次读取的位置。当它读取到文件末尾时,该指针将移至文件末尾,当它尝试再次读取时,它会尝试从文件末尾读取,但没有可读取的内容,它可能会说您有空文件.
阅读后您可能需要使用 vidoe.seek(0)
将指针移动到文件的开头。
或者你应该 close
和 open
再一次。这可能会更有用,因为此时您 select 随机文件只有一次,以后它将始终使用相同的路径。您应该在循环中使用 random
。
@bot.message_handler(commands=['haha'])
def haha_message(message):
while True:
video = open( random.choice(shit), 'rb')
bot.send_video(message.chat.id, video)
video.close()
time.sleep(3600) # 1 hour
其他功能同理
@bot.message_handler(commands=['hehe'])
def shit_message(message):
video = open( random.choice(shit), 'rb')
bot.send_video(message.chat.id, video)
video.close()
顺便说一句:
telegram
可能有一些定时执行任务的方法
例如模块 python-telegram-bot
为此有 telegram.ext.jobqueue。
完整的工作代码
对于测试,我设置 logging.DEBUG
以查看所有错误消息。
通常 telebot
捕获所有错误并隐藏它们。
我也用过with open() as video
所以它会自动关闭文件。
import os
import random
import logging
import telebot
# display errors
telebot.logger.setLevel(logging.DEBUG)
TOKEN = os.getenv('TELEGRAM_TOKEN')
bot = telebot.TeleBot(TOKEN)
all_videos = [
"C:\Users\glebc\Documents\source(bot)\3wZ3.gif.mp4",
"C:\Users\glebc\Documents\source(bot)\65216814_456719028224290_7745639790787166208_n.mp4",
"C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-41-49.mp4",
"C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-42-04.mp4",
"C:\Users\glebc\Documents\source(bot)\doc_2022-03-10_16-42-39.mp4",
"C:\Users\glebc\Documents\source(bot)\giphy.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_0080.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_0835.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_1362.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_4698.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_4962.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_6359.mp4",
"C:\Users\glebc\Documents\source(bot)\IMG_7497.MOV",
"C:\Users\glebc\Documents\source(bot)\IMG_7909.MOV",
"C:\Users\glebc\Documents\source(bot)\IMG_9540.mp4",
"C:\Users\glebc\Documents\source(bot)\mp4.mp4",
"C:\Users\glebc\Documents\source(bot)\video.mp4",
"C:\Users\glebc\Documents\source(bot)\комочек тьмы.mp4",
"C:\Users\glebc\Documents\source(bot)\кот.mp4"
]
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'hello message 1')
@bot.message_handler(commands=['haha'])
def haha_message(message):
while True:
with open(random.choice(all_videos), 'rb') as video:
bot.send_video(message.chat.id, video)
time.sleep(3600) # 1 hour
@bot.message_handler(commands=['hehe'])
def shit_message(message):
with open(random.choice(all_videos), 'rb') as video:
bot.send_video(message.chat.id, video)
bot.infinity_polling()