A
A
Andrey2020-05-16 13:01:03
Python
Andrey, 2020-05-16 13:01:03

How to optimize the code and make it stable?

I uploaded a bot to www.pythonanywhere.com, but it works strangely, sometimes it doesn't write "I'm hungry", sometimes it doesn't respond to commands. I don't know what the problem is, in pythonanywhere (because if you reload it there, it will start working fine) or in the code. How can it be improved so that everything works stably?

ps
делал его по урокам с хабра и ютуба, надеюсь понятен мой уровень программирования. всем снобам которые ненавидят вопросы от новичков привет

the code

import telebot
import config
import time
from telebot import types
from time import time as timer

bot = telebot.TeleBot(config.TG_TOKEN)

@bot.message_handler(commands=['meow'])
def answer(message):
    meow1 = open('audio/meow.ogg', 'rb')
    bot.send_voice(message.chat.id, meow1)

@bot.message_handler(commands=['start'])
def welcome(message):
    stick1 = open('sticker/stick1.webp','rb')
    bot.send_sticker(message.chat.id,stick1)


@bot.message_handler(commands=['feed'])
def ask(message):
    markup = types.InlineKeyboardMarkup(row_width=2)
    item1 = types.InlineKeyboardButton("покормить", callback_data='korm')
    item2 = types.InlineKeyboardButton("погладить", callback_data='glad')
    markup.add(item1, item2)
    bot.send_message(message.chat.id,'что хочешь со мной сделать?', reply_markup=markup)


@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    try:
        if call.message:
            if call.data == 'korm':

                bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="спасибо, очень вкусно :)",
                                      reply_markup=None)
            elif call.data == 'glad':
                bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="мур мяу",
                                      reply_markup=None)

    except Exception as e:
        print(repr(e))

@bot.message_handler(content_types=['text'])
def echo_text(message):
    if 'мяу' in message.text:
        bot.reply_to(message, "покорми меня пожалуйста".format(message.text))
        return

    interval = 7200
    while True:
        time.sleep(interval - timer() % interval)
        bot.send_message(message.chat.id, 'я голодный!!!!')


#RUN
bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question