A
A
Asriel2021-06-05 21:47:12
Python
Asriel, 2021-06-05 21:47:12

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

2 answer(s)
O
o5a, 2021-06-06
@Asriel

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

PS although I missed that it swears at 'NoneType', i.e. there is no information about the photo at all (msg.photo = None), the reason is something else

S
stasersmailov, 2021-06-05
@stasersmailov

try msg.media_group_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question