U
U
uyrsr2020-06-01 13:21:52
Python
uyrsr, 2020-06-01 13:21:52

Telegram bot keeps crashing, how to solve?

The bot crashes after some time from this error: Bot 5ed4d611d9cac583563464.jpeg
code:

import telebot
from telebot import types
import random
import config

bot = telebot.TeleBot(config.TOKEN)

@bot.message_handler(commands=['start'])
def welcome(message):
    markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
    item1 = types.KeyboardButton("случайная картинка")
    item2 = types.KeyboardButton("создатель")
    markup.add(item1, item2)
    bot.send_message(message.chat.id, "добро пожаловать!\nя - бот лошара".format(message.from_user, bot.get_me()), reply_markup=markup,
    parse_mode='html')
@bot.message_handler(content_types=['text'])
def gu(message):
    if message.chat.type == 'private':
        if message.text == 'случайная картинка':
            number = str(random.randint(1, 5))
            if number == '1':
                photo = open('C:\матвей\c.jpg', 'rb')
                bot.send_photo(message.chat.id, photo)
                bot.send_message(message.chat.id, 'Лукака')
            elif number == '2':
                photo2 = open('C:\матвей\wow.jpg', 'rb')
                bot.send_photo(message.chat.id, photo2)
                bot.send_message(message.chat.id, 'уха')
            elif number == '3':
                photo3 = open('C:\матвей\gori.jpg', 'rb')
                bot.send_photo(message.chat.id, photo3)
                bot.send_message(message.chat.id, 'gorrilaz')
            elif number == '4':
                photo4 = open('C:\матвей\phototest.jpg', 'rb')
                bot.send_photo(message.chat.id, photo4)
                bot.send_message(message.chat.id, 'Мой рисунок')
            elif number == '5':
                photo5 = open('C:\матвей\onime.png', 'rb')
                bot.send_photo(message.chat.id, photo5)
                bot.send_message(message.chat.id, 'Аниме женщина')
bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
del4pp, 2020-06-01
@uyrsr

Try in at the end instead of bot.poling it

if __name__ == '__main__':
    while True:
        try:
            bot.polling(none_stop=True)
        except Exception as e:
            time.sleep(3)
            print(e)

Then even when it falls - after 3 seconds it will start again, and the print will display an error in the console

S
ScriptKiddo, 2020-06-01
@ScriptKiddo

while True:
    try:
        *code*
    except Exception as e:
        print(str(e))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question