Answer the question
In order to leave comments, you need to log in
How to send all files from a directory when pressing a button in a Telegram bot?
There is a Telegram bot uploaded to the server. JPG files are stored in a certain directory on the server.
It is necessary that when you click on the Photo button in the bot, all files with the .jpg extension are sent to the chat feed. There are a lot of photos. Making a link to each photo in the code is crazy.
Now I have this:
if message.text == "Photo":
bot.send_photo(message.chat.id, open("Link to photo1", 'rb'),
bot.send_photo(message.chat.id, open( "Link to photo2", 'rb'),
bot.send_photo(message.chat.id, open("Link to photo3", 'rb'),
..... etc.
I need to make a million of such links "But this is stupid. Is it possible to somehow register a link to the directory as a whole? And not to each file separately.
Thank you."
Answer the question
In order to leave comments, you need to log in
I wrote blindly, it should work like
import os
# допустим все фото лежат в текущей директории в папке photos
photos_folder = os.path.join(os.getcwd(), ‘photos’)
if message.text == ‘photo’:
photos_to_send = []
for file in os.listdir(photos_folder):
if file.endswith(‘.jpg’): # можно убрать при желании
photo_path = os.join(photos_folder, file)
photos_to_send.append(telebot.types.InputMediaPhoto(open(photo_path, ‘rb’)))
if len(photos_to_send) == 10: # лимит на одно сообщение
bot.send_media_group(message.chat.id, media=photos_to_send)
photos_to_send.clear()
# дослать остатки, которых меньше 10, если есть
if photos_to_send:
bot.send_media_group(message.chat.id, media=photos_to_send)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question