I
I
ITpie2020-12-12 19:02:54
Python
ITpie, 2020-12-12 19:02:54

What to do if it gives an error 400 for a telegram bot in python?

Excuse me for such a stupid question, I'm just a beginner)
I want to make a regular python telegram bot for a group.
I made it so that when I send the /start command, a message pops up.
If you write for the first time, then it works, but the next time it doesn’t. Throws Error 400.
Here is the code

import telebot
import telebot
from telebot import types


bot = telebot.TeleBot('1459835204:AAE2AO9nUSaBSIWTz5Rf9nnsmlyg5oyfOkc')

Text = 'Привет, я роздаю котов :) \nНапиши в чат "Хочу кота", чтобы получить его'

img = open('Ржомба.png','rb')

@bot.message_handler(commands=['start'])
def welcome_message(message):
    bot.send_photo(message.chat.id, img, caption = Text)
bot.polling(none_stop = True)


Here is the error:

2020-12-12 17:51:58,760 (util.py:66 WorkerThread2) ERROR - TeleBot: "ApiException occurred, args=('A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:\n[b\'{"ok":false,"error_code":400,"description":"Bad Request: file must be non-empty"}\']',)
Traceback (most recent call last ):
File "C:\Users\Marina\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\util.py", line 60, in run
task(*args, **kwargs)
File "C:/Users/Marina/Desktop/telebot_for_group.py", line 14, in welcome_message
bot.send_photo(message.chat.id, img, caption = Text)
File "C:\Users\Marina\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\__init__.py", line 638, in send_photo
apihelper.send_photo(self.token, chat_id, photo , caption, reply_to_message_id, reply_markup,
File "C:\Users\Marina\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\apihelper.py", line 276, in send_photo
return _make_request(token , method_url, params=payload, files=files, method='post')
File "C:\Users\Marina\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\apihelper.py" , line 60, in _make_request
return _check_result(method_name, result)['result']
File "C:\Users\Marina\AppData\Local\Programs\Python\Python38-32\lib\site-packages\telebot\apihelper.py", line 79, in _check_result
raise ApiException(msg, method_name, result)
telebot.apihelper.ApiException: A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
[b'{"ok":false,"error_code":400,"description":"Bad Request: file must be non-empty"}']
"
2020-12-12 17:51:58,804 (__init__ .py:420 MainThread) ERROR - TeleBot: "A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
[b'{"ok":false,"error_code":400,"description":"Bad Request: file must be non-empty"}']

"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-12-12
@ITpie

The file was opened once, when sending for the first time it is read to the end, the pointer remains at the end of the file. On subsequent sending, the script also tries to read the file, but since the pointer is at the end of the file, it does not receive any data, and when sending an empty file, telegram issues a warning.
Better do this - remove your open() at the beginning of the script and paste it directly on the send line:

bot.send_photo(message.chat.id, img = open('Ржомба.png','rb'), caption = Text)

or
with open('Ржомба.png','rb') as f:
    bot.send_photo(message.chat.id, img = f, caption = Text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question