Answer the question
In order to leave comments, you need to log in
How do I send all pictures from a folder through a python bot?
I am writing this code and it gives me an error 'NoneType' object has no attribute 'file_id'
I just copied the code from the guide and pasted it to myself. I want to know the id of the files so that I don’t send them again, but it gives me an error. What am I doing no no? Help
@bot.message_handler(commands=['test'])
def find_file_ids(message):
for file in os.listdir('photo/'):
if file.split('.')[-1] == 'jpg':
f = open('photo/'+file, 'rb')
msg = bot.send_photo(message.chat.id, f, None)
# А теперь отправим вслед за файлом его file_id
bot.send_message(message.chat.id, msg.photo.file_id, reply_to_message_id=msg.message_id)
time.sleep(3)
Answer the question
In order to leave comments, you need to log in
Because msg.photo contains not one, but a list of options with different resolutions. So first you need to choose the right one. For example like this
photo = max(msg.photo, key=lambda x: x.height)
file_id = photo.file_id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question