import telebot
bot = telebot.TeleBot(config.token)
@bot.message_handler(commands=['test'])
def find_file_ids(message):
for file in os.listdir('music/'):
if file.split('.')[-1] == 'ogg':
f = open('music/'+file, 'rb')
msg = bot.send_voice(message.chat.id, f, None)
# А теперь отправим вслед за файлом его file_id
bot.send_message(message.chat.id, msg.voice.file_id, reply_to_message_id=msg.message_id)
time.sleep(3)
if __name__ == '__main__':
bot.infinity_polling()
更新。对于特定的文件类型,使用适当的函数:
# All send_xyz functions which can take a file as an argument, can also take a file_id instead of a file.
# sendPhoto
photo = open('/tmp/photo.png', 'rb')
tb.send_photo(chat_id, photo)
tb.send_photo(chat_id, "FILEID")
# sendAudio
audio = open('/tmp/audio.mp3', 'rb')
tb.send_audio(chat_id, audio)
tb.send_audio(chat_id, "FILEID")
## sendAudio with duration, performer and title.
tb.send_audio(CHAT_ID, file_data, 1, 'eternnoir', 'pyTelegram')
# sendVoice
voice = open('/tmp/voice.ogg', 'rb')
tb.send_voice(chat_id, voice)
tb.send_voice(chat_id, "FILEID")
# sendDocument
doc = open('/tmp/file.txt', 'rb')
tb.send_document(chat_id, doc)
tb.send_document(chat_id, "FILEID")
# sendSticker
sti = open('/tmp/sti.webp', 'rb')
tb.send_sticker(chat_id, sti)
tb.send_sticker(chat_id, "FILEID")
# sendVideo
video = open('/tmp/video.mp4', 'rb')
tb.send_video(chat_id, video)
tb.send_video(chat_id, "FILEID")
# sendVideoNote
videonote = open('/tmp/videonote.mp4', 'rb')
tb.send_video_note(chat_id, videonote)
tb.send_video_note(chat_id, "FILEID")
这是完整的代码:
这是课程
更新。对于特定的文件类型,使用适当的函数:
虽然我是通过 aiogram 编写的,但我认为它可以与其他库一起使用,只需将 ID 本身作为媒体 / 音频 / 等参数传递即可,它可以工作,并且您不需要像 InputFile、InputMediaAudio 这样的手鼓跳舞