Answer the question
In order to leave comments, you need to log in
The bot sends messages endlessly. How to fix?
Good day! The question is the following: there is a bot that translates values. In our case, kg to pounds. When you select the /weight command, a menu appears in which we select the desired item. Next, enter the value and process. If after that the phone is blocked, then the bot starts to "spam" messages of the same type. I thought it was all about VPN, but transferred the bot to Heroka and the same garbage. Who will prompt in what there can be a reason?
import telebot
from telebot import types
TOKEN = '777'
bot = telebot.TeleBot(TOKEN)
#Weight
weight = types.InlineKeyboardMarkup()
kgtolbs = types.InlineKeyboardButton(text='КГ->ФУНТ', callback_data='kgtolbs')
backtomenu = types.InlineKeyboardButton(text='Назад', callback_data='backtomenu')
weight.add(kgtolbs)
weight.add(backtomenu)
backw = types.InlineKeyboardMarkup()
backw.add(types.InlineKeyboardButton(text='Назад', callback_data='backw'))
def kgtolbs(msg):
try:
chat_id = msg.chat.id
num = msg.text
if num.isdigit():
bot.send_message(chat_id, num + ' кг = ' + '{:.5}'.format(float(num) * 2.2046226) + ' фунта', parse_mode='markdown', reply_markup=backw)
else:
bot.send_message(chat_id, "*Вы ввели что-то не то:( \n Попробуйте еще раз, у Вас получится*", parse_mode='markdown', reply_markup=weight)
except Exception as e:
bot.send_message(chat_id, 'Ops')
#Welcome message
@bot.message_handler(commands=['start'])
def send_welcome(msg):
text = '*Здравствуй, ' + msg.from_user.first_name + '*\nЯ первый бот, который переводит различные величины прямо в Телеграме!\nДля того, чтобы видеть весь список используйте команду /help\n*По всем вопросам обращайтесь к* @ork821'
bot.send_message(msg.chat.id, text, parse_mode='Markdown')
#Send list of commands
@bot.message_handler(commands=['help'])
def list_commands(msg):
bot.send_message(msg.chat.id, "*Список всех команд:*\n------------------------\n_Перевод массы⚖:_ /weight\n_Перевод длин:_ /length\n_Перевод скорости:_ /speed\n_Перевод температуры:_ /tepm\n_Перевод времени:_ /time\n_Перевод валют:_ /money\n\n\n_Помните, что выражение E+04 = 10 в 4 степени = 10000_", parse_mode='markdown')
#Weight keyboard
@bot.message_handler(commands=['weight'])
def list_commands_weight(msg):
bot.send_message(msg.chat.id, "*Выберите нужный пункт из списка:*", reply_markup=weight, parse_mode='Markdown')
#Analysis message
@bot.callback_query_handler(func=lambda c: True)
def mess(c):
if (c.data == "backtomenu"):
list_commands(c.message)
bot.clear_step_handler_by_chat_id(c.message.chat.id)
elif (c.data == "backw"):
list_commands_weight(c.message)
bot.clear_step_handler_by_chat_id(c.message.chat.id)
#Weight
elif (c.data == "kgtolbs"):
text = bot.send_message(c.message.chat.id, "*Введите ваше число*", parse_mode='markdown')
bot.register_next_step_handler(text, kgtolbs)
else: pass
bot.polling()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question