@daniilletov

Как убрать ошибку в боте?

При отправке текста в бота выдаёт ошибку, как можно исправить?

def handle_message(message):
    if message.text == "/start":
        bot.send_message(message.from_user.id, "Привет, я бот для оброботки фото. Пришли фото для его обработки")
    elif message.text == "/help":
        bot.send_message(message.from_user.id, "Нажми /start")
    else:                                                    
        photo=message.photo[-1]                                 
        file_info = bot.get_file(photo.file_id)             
        downloaded_file = bot.download_file(file_info.file_path)
        save_path = 'photo.png'                                 
        with open(save_path, 'wb') as new_file:               
            new_file.write(downloaded_file)                     
        bot.reply_to(message, 'Фотография сохранена.')         
        im = Image.open("photo.png")                            
        out = im.resize(size)                                  
        out.save('photoresize.png')                             
        with open('photoresize.png', 'rb') as f:                
            bot.send_photo(message.from_user.id, f)
  • Вопрос задан
  • 76 просмотров
Решения вопроса 1
Mike_Ro
@Mike_Ro
Python, JS, WordPress, SEO, Bots, Adversting
TypeError
'NoneType' object is not subscriptable
File "C:\Users\Administrator\Desktop\Bot\Bot.py", line 21, in handle_message
photo=message.photo[-1] # Сохранение фото на ваш компьютер
File "C:\Users\Administrator\Desktop\Bot\Bot.py", line 35, in
bot.polling()
TypeError: 'NoneType' object is not subscriptable

Грешно обращаться к Null, как к списку или словарю. Добавьте проверку:
if message.photo:
    photo = message.photo[-1]
    # other code
else:
    bot.reply_to(message, 'Гони фотку.')
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы