A
A
AlexAlex2292021-10-31 22:09:21
Python
AlexAlex229, 2021-10-31 22:09:21

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()


When I start the bot, everything works stably the first time, but when I click reset and I want to start over (start entering a name), the bot works on the previous request, and thinks that I entered the last name.

Accordingly, first the bot writes "Enter your first name", and then it thinks that I am entering the last name, and gives the result "Your name is" + the first and last name entered by the user, then this error:
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"


How can I reset the program when pressing the inline button, restart the bot and start over? So that every time you click on the inline button, the script seems to be closed and reopened. I don't know how to make it code so that the bot works non-stop 24/7.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AlexAlex229, 2021-11-02
@AlexAlex229

You can repeatedly call the function send_welcomedirectly from inline_handlewhich catches the reset button.
And in the function itself, send_welcomereset all registered handlers by calling bot.clear_step_handler(message).
Then if it is send_welcomecalled 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()

The answer has been given here .

L
LXSTVAYNE, 2021-11-01
@lxstvayne

Too many try..except... In general, try to write return after you reset the work.

G
Garnish386, 2021-12-11
@Garnish386

Is it possible to do the same, only on aiogram ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question