Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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)
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 questionAsk a Question
731 491 924 answers to any question