Answer the question
In order to leave comments, you need to log in
How to restart the bot with the code, when pressing the inline button (pytelegrambotapi)?
Hello! There is a Telegram bot, code:
import telebot
from telebot import types
bot = telebot.TeleBot("*****")
@bot.message_handler(commands=["start"])
def send_welcome(message):
global markup, DELETEuserName, DELETEuserName1 #делаю переменную глобальной, чтобы она была видна в остальных функциях. Переменные с приставкой DELETE нужны для того, чтобы удалить сообщения от бота
markup = types.InlineKeyboardMarkup()
resetDataKey = types.InlineKeyboardButton("Сбросить", callback_data="resetData")
markup.add(resetDataKey)
userName = bot.send_message(message.chat.id, "Здравствуйте, введите имя")
DELETEuserName = userName.chat.id
DELETEuserName1 = userName.message_id
bot.register_next_step_handler(userName, userSurNameFUNC)
def userSurNameFUNC(message):
userName1 = message.text
global userName, DELETEuserSurName, DELETEuserSurName1 #проделываю эту махинацию, чтобы сообщения превращались в тип String и конкатенировались
userName = userName1
userSurName = bot.send_message(message.chat.id, "Теперь введите фамилию", reply_markup=markup)
DELETEuserSurName = userSurName.chat.id
DELETEuserSurName1 = userSurName.message_id
bot.delete_message(message.chat.id, message.message_id)
bot.delete_message(DELETEuserName, DELETEuserName1)
bot.register_next_step_handler(userSurName, endProgrammFUNC)
def endProgrammFUNC(message):
userSurName1 = message.text
global userSurName, DELETEendProgramm, DELETEendProgramm1
userSurName = userSurName1
endProgramm = bot.send_message(message.chat.id, "Вас зовут " + userName + " " + userSurName)
DELETEendProgramm = endProgramm.chat.id
DELETEendProgramm1 = endProgramm.message_id
bot.delete_message(message.chat.id, message.message_id)
bot.delete_message(DELETEuserSurName, DELETEuserSurName1)
@bot.callback_query_handler(func=lambda call: True)
def inline_handler(call):
if call.data == "resetData":
bot.delete_message(call.message.chat.id, call.message.message_id)
userName = bot.send_message(call.message.chat.id, "Здравствуйте, введите имя")
DELETEuserName = userName.chat.id
DELETEuserName1 = userName.message_id
try:
try:
pass
except:
try:
bot.delete_message(DELETEuserSurName, DELETEuserSurName1)
except:
bot.delete_message(DELETEendProgramm, DELETEendProgramm1)
except:
pass
bot.register_next_step_handler(userName, userSurNameFUNC)
bot.infinity_polling()
2021-10-31 21:53:34,798 (__init__.py:663 MainThread) ERROR - TeleBot: "A request
to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request
: message to delete not found"
Answer the question
In order to leave comments, you need to log in
You can repeatedly call the function send_welcome
directly from inline_handle
which catches the reset button.
And in the function itself, send_welcome
reset all registered handlers by calling bot.clear_step_handler(message)
.
Then if it is send_welcome
called for the first time, no handlers have been set yet and nothing will happen, and if it is called again, they will be reset, as required.
Here is a working code example:
@bot.message_handler(commands=["start"])
def send_welcome(message):
global DELETEuserName1
bot.clear_step_handler(message)
userName = bot.send_message(message.chat.id, "Здравствуйте, введите имя")
DELETEuserName1 = userName.message_id
bot.register_next_step_handler(userName, userSurNameFUNC)
def userSurNameFUNC(message):
global userName, DELETEuserSurName, DELETEuserSurName1
userName = message.text
markup = types.InlineKeyboardMarkup()
resetDataKey = types.InlineKeyboardButton("Сбросить", callback_data="resetData")
markup.add(resetDataKey)
userSurName = bot.send_message(message.chat.id, "Теперь введите фамилию", reply_markup=markup)
DELETEuserSurName = userSurName.chat.id
DELETEuserSurName1 = userSurName.message_id
bot.delete_message(message.chat.id, message.message_id)
bot.delete_message(message.chat.id, DELETEuserName1)
bot.register_next_step_handler(userSurName, endProgrammFUNC)
def endProgrammFUNC(message):
global userSurName, DELETEendProgramm, DELETEendProgramm1
userSurName = message.text
endProgramm = bot.send_message(message.chat.id, "Вас зовут " + userName + " " + userSurName)
DELETEendProgramm = endProgramm.chat.id
DELETEendProgramm1 = endProgramm.message_id
bot.delete_message(message.chat.id, message.message_id)
bot.delete_message(DELETEuserSurName, DELETEuserSurName1)
@bot.callback_query_handler(func=lambda call: True)
def inline_handler(call):
if call.data == "resetData":
bot.delete_message(call.message.chat.id, call.message.message_id)
send_welcome(call.message)
bot.infinity_polling()
Too many try..except... In general, try to write return after you reset the work.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question