E
E
Emil Timerbaev2020-11-29 20:04:19
Python
Emil Timerbaev, 2020-11-29 20:04:19

Photo upload error?

Here is my program:

def adc_2(message):
    bot.send_photo(config.worker, message.photo[0].file_id)
    bot.send_message(message.chat.id, mess1, reply_markup=keyboard)
    bot.register_next_step_handler(message, menu)

Here is the error
Mistake
2020-11-29 21:52:18,659 (util.py:75 WorkerThread1) ERROR - TeleBot: "TypeError occurred, args=("'NoneType' object is not subscriptable",)
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\site-packages\telebot\util.py", line 69, in run
task(*args, **kwargs)
File "D:/Emil Programm/Telegram_bot/main.py", line 328, in adc_2
bot.send_photo(config.worker, message.photo[0].file_id)
TypeError: 'NoneType' object is not subscriptable
"
Traceback (most recent call last):
File "D:/Emil Programm/Telegram_bot/main.py", line 944, in
bot.polling()
File "C:\Program Files\Python38\lib\site-packages\telebot\__init__.py", line 427, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "C:\Program Files\Python38\lib\site-packages\telebot\__init__.py", line 451, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Program Files\Python38\lib\site-packages\telebot\util.py", line 118, in raise_exceptions
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
File "C:\Program Files\Python38\lib\site-packages\six.py", line 703, in reraise
raise value
File "C:\Program Files\Python38\lib\site-packages\telebot\util.py", line 69, in run
task(*args, **kwargs)
File "D:/Emil Programm/Telegram_bot/main.py", line 328, in adc_2
bot.send_photo(config.worker, message.photo[0].file_id)
TypeError: 'NoneType' object is not subscriptable

Process finished with exit code 1

This program is like an echo bot, it only sends the photo not to the person who just sent it, but to the person whose ID is config.worker what is the error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-11-30
@MrDlop

There is nothing in message.photo, so this error occurs. Wrap this line in the condition that message.photo is a list.

Explanation
Скорее всего, эта функция вызывается при каждом событии "Получение сообщения", содержит ли тело сообщения фотографии или нет. И, если фотографий нет, то в message.photo тоже, разумеется, ничего нет и его тип соответствует NoneType. При попытке взять значение из такой переменной, как из списка, выполняется ошибка TypeError.

if type(message.photo) is list

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question